From efd0b4d4e3b4d5f49dd29ac11fc46c44861cd33b Mon Sep 17 00:00:00 2001 From: Yevhen Beshkarov Date: Sat, 17 Oct 2026 23:12:23 +0300 Subject: [PATCH 1/2] [shared_preferences] Keep Kotlin classes used through JNI in release builds shared_preferences_android 2.5.0 calls its Kotlin code through JNI. The generated Dart bindings and package:jni look up several Kotlin standard library classes by name, such as kotlin.coroutines.Continuation. R8 renames them in release builds, so every call fails with a NoSuchMethodError and SharedPreferences.getInstance() and SharedPreferencesWithCache.create() throw at startup. Adds consumer R8 rules that keep those classes, as Pigeon's native interop guide describes for plugins, until package:jni ships them (https://github.com/dart-lang/native/issues/3732). --- .../shared_preferences_android/CHANGELOG.md | 4 ++++ .../android/build.gradle.kts | 3 +++ .../android/consumer-rules.pro | 14 ++++++++++++++ .../shared_preferences_android/pubspec.yaml | 2 +- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro diff --git a/packages/shared_preferences/shared_preferences_android/CHANGELOG.md b/packages/shared_preferences/shared_preferences_android/CHANGELOG.md index 3dfba860a506..6c4a5366cc66 100644 --- a/packages/shared_preferences/shared_preferences_android/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_android/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.5.1 + +* Fixes a `NoSuchMethodError` at startup in Android release builds by adding consumer R8 rules that keep the Kotlin classes looked up through JNI. + ## 2.5.0 * Updates internal implementation to use Pigeon JNI. diff --git a/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts b/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts index 4cd5b69c5c98..6bedf48e5509 100644 --- a/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts +++ b/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts @@ -49,6 +49,9 @@ android { defaultConfig { minSdk = 24 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + // TODO(ybeshkarov): Remove, along with consumer-rules.pro, once package:jni + // ships the Kotlin keep rules: https://github.com/dart-lang/native/issues/3732 + consumerProguardFiles("consumer-rules.pro") } lint { diff --git a/packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro b/packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro new file mode 100644 index 000000000000..67e8abe2d8ca --- /dev/null +++ b/packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro @@ -0,0 +1,14 @@ +# Kotlin classes that package:jni and the generated Dart code look up by name +# through JNI. Classes generated by Pigeon are annotated with @Keep, but library +# classes can't be annotated, so without these rules R8 renames them in release +# builds and the JNI calls fail with NoSuchMethodError. +# +# TODO(ybeshkarov): Delete this file and its consumerProguardFiles entry in +# build.gradle.kts once package:jni ships these rules: +# https://github.com/dart-lang/native/issues/3732 +-keep class kotlin.Unit { *; } +-keep class kotlin.Result { *; } +-keep class kotlin.Result$Failure { *; } +-keep class kotlin.coroutines.Continuation { *; } +-keep class kotlin.coroutines.intrinsics.CoroutineSingletons { *; } +-keep class kotlin.coroutines.intrinsics.IntrinsicsKt { *; } diff --git a/packages/shared_preferences/shared_preferences_android/pubspec.yaml b/packages/shared_preferences/shared_preferences_android/pubspec.yaml index 86b2d2f72636..b922807aa958 100644 --- a/packages/shared_preferences/shared_preferences_android/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_android/pubspec.yaml @@ -2,7 +2,7 @@ name: shared_preferences_android description: Android implementation of the shared_preferences plugin repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_android issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22 -version: 2.5.0 +version: 2.5.1 environment: sdk: ^3.12.0 From edb35a3a193b4d2f7e5566b3adc1379d853f3861 Mon Sep 17 00:00:00 2001 From: Yevhen Beshkarov Date: Tue, 20 Oct 2026 20:58:35 +0300 Subject: [PATCH 2/2] Point the TODOs at the jni issue instead of a person --- .../shared_preferences_android/android/build.gradle.kts | 4 ++-- .../shared_preferences_android/android/consumer-rules.pro | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts b/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts index 8034c0b10ae1..a205cdd91fea 100644 --- a/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts +++ b/packages/shared_preferences/shared_preferences_android/android/build.gradle.kts @@ -49,8 +49,8 @@ android { defaultConfig { minSdk = 24 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" - // TODO(ybeshkarov): Remove, along with consumer-rules.pro, once package:jni - // ships the Kotlin keep rules: https://github.com/dart-lang/native/issues/3732 + // TODO(https://github.com/dart-lang/native/issues/3732): Remove, along with + // consumer-rules.pro, once package:jni ships the Kotlin keep rules. consumerProguardFiles("consumer-rules.pro") } diff --git a/packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro b/packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro index 67e8abe2d8ca..c7f463fd296e 100644 --- a/packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro +++ b/packages/shared_preferences/shared_preferences_android/android/consumer-rules.pro @@ -3,9 +3,9 @@ # classes can't be annotated, so without these rules R8 renames them in release # builds and the JNI calls fail with NoSuchMethodError. # -# TODO(ybeshkarov): Delete this file and its consumerProguardFiles entry in -# build.gradle.kts once package:jni ships these rules: -# https://github.com/dart-lang/native/issues/3732 +# TODO(https://github.com/dart-lang/native/issues/3732): Delete this file and +# its consumerProguardFiles entry in build.gradle.kts once package:jni ships +# these rules. -keep class kotlin.Unit { *; } -keep class kotlin.Result { *; } -keep class kotlin.Result$Failure { *; }