Repository navigation
Commit 1383427
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
1 | 5 | | |
2 | 6 | | |
3 | 7 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
0 commit comments