Repository navigation
fix(android): use reactApplicationContext.currentActivity to compile with Kotlin 2.2 (React Native 0.87+) - #228
Open
kmy-n wants to merge 1 commit into
Conversation
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.
Use
reactApplicationContext.currentActivityto compile with Kotlin 2.2 (React Native 0.87+)🔄 Type of change:
✏️ Description:
The Android module fails to compile on React Native 0.87 and later. From 0.87, the React Native Gradle plugin brings in Kotlin 2.2.0, and the compiler rejects line 94 of
LineLoginModule.kt:currentActivityon that line resolves toReactContextBaseJavaModule.getCurrentActivity(). It is a Kotlin function, but the module reaches it through the Java codegen classNativeLineLoginSpec, so Kotlin treats it as a synthetic property. Kotlin 2.1 reports this only as a warning (output from Kotlin 2.1.20 with the currentmain):Kotlin 2.2 turns it into the error above (KT-72272). The getter has also been deprecated since React Native 0.80:
This PR uses
reactApplicationContext.currentActivity, the replacement named in that deprecation message. In React Native, the deprecated getter is implemented asreturn reactApplicationContext.currentActivity, so the returned activity is the same and the login flow does not change.ReactContext#getCurrentActivity()existed before the 0.80 deprecation, so the minimum supported React Native version does not change either.🎥 Screen record:
N/A — build fix with no UI change. The login flow was checked on an emulator (see Notes).
📌 Notes:
Verified locally with the example app on top of the current
main(8e3a127): Expo SDK 57, React Native 0.86.3, AGP 8.12.0, Gradle 9.3.1, JDK 17../gradlew :xmartlabs_react-native-line:compileDebugKotlin:LineLoginModule.ktFunction invocation 'getCurrentActivity()' expectedLineLoginModule.ktTo build the example with Kotlin 2.2.0, I temporarily changed the generated
example/android/build.gradletoclasspath('org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.0'). This change is not part of the PR../gradlew assembleRelease -PreactNativeArchitectures=arm64-v8asucceeds with the default Kotlin 2.1.20.With that release build on a Pixel 9a emulator (Android 16, API 36, LINE app not installed):
LineAuthenticationActivityfrom the activity obtained on the changed line, and the web login opens in Chrome.Line.login()rejects withLOGIN_CANCELLED, and the example shows its "Failed to log in" alert, the same as before this change.I also checked an app on React Native 0.88.0 (Kotlin 2.2.0, AGP 9.2.1, Gradle 9.4.1). Without this change the module fails with the same error, and with it the module compiles.
Until a release includes this fix, apps on React Native 0.87+ can work around the error by compiling only this module with the Kotlin 2.1 language version:
✔️Tasks:
currentActivitywithreactApplicationContext.currentActivityinLineLoginModule.login()(android/src/main/java/com/xmartlabs/line/LineLoginModule.kt).kotlin-androidplugin and AGP 9's built-in Kotlin. With the React Native template, which setsandroid.builtInKotlin=false, fix(android): skip explicit Kotlin plugin when AGP registers the kotlin extension #225 is not needed today, but this compile error still happens. This PR does not depend on fix(android): skip explicit Kotlin plugin when AGP registers the kotlin extension #225.