fix: Improve Android lifecycle compatibility detection for RN 0.76.x with targetSdk 35#3922
Merged
Merged
Conversation
…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>
0db71bf to
0b159c6
Compare
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the Android build failure when using React Native 0.76.x with
targetSdkVersion35, which resulted in the error:The issue occurs because the current logic only checks
targetSdkVersionto decide which lifecycle API to use, but doesn't consider the actual React Native version. RN 0.76.x doesn't support the newersetViewTreeLifecycleOwnerAPI even when targeting SDK 35.Changes
package.jsonRNMapboxMapsLifecycleCompatflagImplementation Details
The solution uses a three-tier priority system:
User Override (highest priority)
Automatic Detection - Reads RN version from
node_modules/react-native/package.jsonViewTreeLifecycleOwner.set)setViewTreeLifecycleOwner)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.jsoninstead ofgradle.propertiesfor better reliability.Test Results
Tested with a fresh React Native 0.76.9 project:
targetSdkVersion = 35Related Issues
Fixes #3909
🤖 Generated with Claude Code