Skip to content

Commit 1383427

Browse files
authored
[shared_preferences] Keep Kotlin classes used through JNI in Android release builds (#13311)
`shared_preferences_android` 2.5.0 calls its Kotlin code through JNI. The generated Dart bindings and `package:jni` look up some Kotlin standard library classes by name, but nothing keeps them, so R8 renames them in release builds. From `mapping.txt` of a release build with 2.5.0: ``` kotlin.Result -> l1.d: kotlin.Result$Failure -> l1.c: kotlin.Unit -> l1.g: kotlin.coroutines.Continuation -> o1.d: kotlin.coroutines.intrinsics.CoroutineSingletons -> p1.a: ``` The generated classes are kept by `@Keep`, but R8 rewrites their method descriptors with the renamed `Continuation`. So every call fails with a `NoSuchMethodError`, for example: ``` NoSuchMethodError: no non-static method "Lio/flutter/plugins/sharedpreferences/SharedPreferencesAsyncApiRegistrar;.getAll(Ljava/util/List;Lio/flutter/plugins/sharedpreferences/SharedPreferencesPigeonOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;" ``` and `SharedPreferences.getInstance()`, `SharedPreferencesAsync` and `SharedPreferencesWithCache.create()` all throw. Debug and profile builds work. This PR adds `android/consumer-rules.pro` with the six keep rules from Pigeon's native interop guide ("Step 5: Android Release Build Configuration (JNI)") and lists it in `consumerProguardFiles`, the same way `packages/pigeon/platform_tests/test_plugin` does. R8 then applies the rules to every app that uses the plugin. The six classes are the ones looked up by name: `kotlin.coroutines.Continuation` in the descriptors of the `suspend` methods and `kotlin.Unit` in `messages*.g.dart`, plus `kotlin.Unit`, `kotlin.Result`, `kotlin.Result$Failure`, `kotlin.coroutines.Continuation`, `CoroutineSingletons` and `IntrinsicsKt` in `lib/src/kotlin.dart` of `jni` 1.1.0. The other Kotlin types in the bindings (`DefaultConstructorMarker`, `EnumEntries`) only appear in synthetic constructors and `getEntries`, which the plugin never calls. A TODO in `consumer-rules.pro` and `build.gradle.kts` points to dart-lang/native#3732, so the rules can be removed once `jni` ships them. Fixes flutter/flutter#194649 How I verified it: a fresh `flutter create --platforms android` app with `shared_preferences: ^2.5.5` that calls `SharedPreferencesWithCache.create()` in `main()` and counts launches. I built it with `flutter build apk --release --target-platform android-arm64` (Flutter 3.44.2, AGP 9.0.1, Gradle 9.1.0, R8 on by default) and ran it on an API 37 arm64 emulator. - With `shared_preferences_android` 2.5.0 from pub.dev, the app stays on the splash screen and logcat shows the `NoSuchMethodError` above from `SharedPreferencesWithCache.create`. - With a `dependency_overrides` path to this branch, `configuration.txt` of the build lists the rules from the plugin's consumer file, `mapping.txt` maps each class to itself (for example `kotlin.coroutines.Continuation -> kotlin.coroutines.Continuation:`), and the app starts. It logs `launches=1`, and `launches=2` after a restart. - A second entry point that round-trips every value type, `getKeys`, `getAll`, `remove` and `clear` through `SharedPreferencesAsync` and the legacy `SharedPreferences` API fails for both APIs with 2.5.0 and passes for both with this branch. I also ran `flutter test` in the package (39 tests pass) and `validate --check-for-missing-changes`, `analyze`, `format`, `dart-test`, `publish-check` and `license-check` of the repo tooling for the package. None of them reported an issue. Test exemption: I'd like to request one. The change is build configuration that only affects R8 in an app's release build, and as far as I can tell no CI target runs a release build of a plugin. `drive-examples`, the native integration tests and Firebase Test Lab use debug builds, and `android_build_all_packages` builds the all_packages app in release but doesn't run it. If there is a place where a release-mode test would fit, I'm happy to add one. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 82c9ad5 commit 1383427

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎packages/shared_preferences/shared_preferences_android/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.5.2
2+
3+
* Fixes a `NoSuchMethodError` at startup in Android release builds by adding consumer R8 rules that keep the Kotlin classes looked up through JNI.
4+
15
## 2.5.1
26

37
* Updates `androidx.datastore:datastore` to 1.2.1.

‎packages/shared_preferences/shared_preferences_android/android/build.gradle.kts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ android {
4949
defaultConfig {
5050
minSdk = 24
5151
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
52+
// TODO(https://github.com/dart-lang/native/issues/3732): Remove, along with
53+
// consumer-rules.pro, once package:jni ships the Kotlin keep rules.
54+
consumerProguardFiles("consumer-rules.pro")
5255
}
5356

5457
lint {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Kotlin classes that package:jni and the generated Dart code look up by name
2+
# through JNI. Classes generated by Pigeon are annotated with @Keep, but library
3+
# classes can't be annotated, so without these rules R8 renames them in release
4+
# builds and the JNI calls fail with NoSuchMethodError.
5+
#
6+
# TODO(https://github.com/dart-lang/native/issues/3732): Delete this file and
7+
# its consumerProguardFiles entry in build.gradle.kts once package:jni ships
8+
# these rules.
9+
-keep class kotlin.Unit { *; }
10+
-keep class kotlin.Result { *; }
11+
-keep class kotlin.Result$Failure { *; }
12+
-keep class kotlin.coroutines.Continuation { *; }
13+
-keep class kotlin.coroutines.intrinsics.CoroutineSingletons { *; }
14+
-keep class kotlin.coroutines.intrinsics.IntrinsicsKt { *; }

‎packages/shared_preferences/shared_preferences_android/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: shared_preferences_android
22
description: Android implementation of the shared_preferences plugin
33
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
5-
version: 2.5.1
5+
version: 2.5.2
66

77
environment:
88
sdk: ^3.12.0

0 commit comments

Comments
 (0)