fix(android): use reactApplicationContext.currentActivity for RN 0.87 - #16
Merged
Merged
Conversation
ReactContextBaseJavaModule was converted to Kotlin, so getCurrentActivity() is now a fun rather than a Java getter and Kotlin synthetic property access no longer resolves, failing the build with: e: RNSpeechModule.kt:733:22 Function invocation 'getCurrentActivity()' expected. It has also been deprecated since 0.80.0 in favour of reactApplicationContext.currentActivity, which is what its ReplaceWith names. Both accessors it goes through are still Java getters, so this compiles on old and new React Native alike and drops a deprecation warning. Fixes mhpdev-com#15
Contributor
|
LGTM! Thanks for the PR. |
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.
Fixes #15.
On React Native 0.87 the Android build fails with:
Cause.
ReactContextBaseJavaModulewas converted to Kotlin in 0.87, sogetCurrentActivity()is now declared as aprotected funrather than a Java getter. Kotlin's synthetic property access only applies to Java getters, socurrentActivityno longer resolves and the compiler demands an explicit call.Fix. The method has also been deprecated since 0.80.0, and its annotation names the replacement directly:
Using that replacement fixes the build and removes the deprecation warning at the same time, which calling
getCurrentActivity()explicitly would not.Compatibility. This should be safe across React Native versions rather than trading one break for another. Both accessors it goes through are still Java getters —
BaseJavaModule.getReactApplicationContext()isprotected finalandReactContext.getCurrentActivity()ispublic @Nullable— so synthetic property access remains valid on older releases too.reactApplicationContextis also what the deprecated method itself delegates to, so behaviour is unchanged.Verification. Built against React Native 0.87.1 with the New Architecture on Android (
compileSdk 37, AGP 9, Gradle 9.4.1)::mhpdev_react-native-speech:compileDebugKotlinand a full:app:assembleDebugboth succeed, with no deprecation warning. Text-to-speech works on device. OnlyopenVoiceDataInstalleris touched.