Skip to content

fix: Improve Android lifecycle compatibility detection for RN 0.76.x with targetSdk 35#3922

Merged
mfazekas merged 1 commit into
mainfrom
android-lifecycle-fix-rn076
Aug 9, 2025
Merged

fix: Improve Android lifecycle compatibility detection for RN 0.76.x with targetSdk 35#3922
mfazekas merged 1 commit into
mainfrom
android-lifecycle-fix-rn076

Conversation

@mfazekas

@mfazekas mfazekas commented Aug 9, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR fixes the Android build failure when using React Native 0.76.x with targetSdkVersion 35, which resulted in the error:

Unresolved reference: setViewTreeLifecycleOwner

The issue occurs because the current logic only checks targetSdkVersion to decide which lifecycle API to use, but doesn't consider the actual React Native version. RN 0.76.x doesn't support the newer setViewTreeLifecycleOwner API even when targeting SDK 35.

Changes

  • ✅ Add automatic React Native version detection from package.json
  • ✅ Default to new lifecycle API (v26) for future compatibility
  • ✅ Use old API (v25) only when RN < 0.78 is detected
  • ✅ Provide user override via RNMapboxMapsLifecycleCompat flag

Implementation Details

The solution uses a three-tier priority system:

  1. User Override (highest priority)

    ext {
        RNMapboxMapsLifecycleCompat = "v25"  // or "v26"
    }
  2. Automatic Detection - Reads RN version from node_modules/react-native/package.json

    • RN < 0.78: Uses v25 (old API: ViewTreeLifecycleOwner.set)
    • RN >= 0.78: Uses v26 (new API: setViewTreeLifecycleOwner)
  3. Default - Uses v26 (new API) for forward compatibility

This approach is similar to react-native-screens' version-based source selection, but simplified to read from package.json instead of gradle.properties for better reliability.

Test Results

Tested with a fresh React Native 0.76.9 project:

  • Before fix: Build fails with targetSdkVersion = 35
  • After fix: Build succeeds, correctly detects RN 0.76.9 and uses v25 compatibility

Related Issues

Fixes #3909

🤖 Generated with Claude Code

…with targetSdk 35

- Adopt react-native-screens' resolveReactNativeDirectory approach for consistency
- Add automatic React Native version detection (gradle.properties then package.json)
- Default to new lifecycle API (v26) for future compatibility
- Use old API (v25) only when RN < 0.78 is detected
- Provide user override via RNMapboxMapsLifecycleCompat flag
- Fixes build failures with RN 0.76.x and targetSdkVersion 35

This resolves the 'Unresolved reference: setViewTreeLifecycleOwner' error
reported in #3909 by properly detecting which lifecycle API to use based
on the React Native version rather than just targetSdkVersion.

Implementation uses the same approach as react-native-screens:
https://github.com/software-mansion/react-native-screens/blob/main/android/build.gradle#L83-L127
This ensures consistency across the ecosystem and allows us to benefit
from their battle-tested implementation for RN directory resolution.

Users can override the automatic detection by setting:
ext {
  RNMapboxMapsLifecycleCompat = 'v25' // or 'v26'
  // Or specify RN location:
  REACT_NATIVE_NODE_MODULES_DIR = '/path/to/react-native'
}

Fixes #3909

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
@mfazekas mfazekas force-pushed the android-lifecycle-fix-rn076 branch from 0db71bf to 0b159c6 Compare August 9, 2025 11:29
@mfazekas mfazekas merged commit c271f4a into main Aug 9, 2025
11 checks passed
@mfazekas mfazekas deleted the android-lifecycle-fix-rn076 branch August 9, 2025 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 10.1.40 Android build fails on RN 0.76 due to lifecycle compatibility issues in v26 LifecycleCompat.kt

1 participant