Skip to content

[shared_preferences] shared_preferences_android 2.5.0 crashes release builds at start: NoSuchMethodError in SharedPreferencesAsyncApiRegistrar.getAll (R8) #194649

Closed
flutter/packages
#13311

Description

@ybeshkarov

Steps to reproduce

  1. flutter create --platforms android repro && cd repro
  2. flutter pub add shared_preferences (resolves shared_preferences 2.5.5 and shared_preferences_android 2.5.0)
  3. Replace lib/main.dart with the code sample below.
  4. flutter build apk --release (R8 is on by default for release builds) and install/run the APK on a device or emulator, e.g. flutter run --release.

Expected results

The app opens the preferences, shows the first frame and counts launches, as it does with shared_preferences_android 2.4.28 and as it does in debug and profile builds with 2.5.0.

Actual results

SharedPreferencesWithCache.create() throws at start in the release build, so runApp() is never reached and the app stays on the splash screen. Debug and profile builds work, and iOS is not affected.

shared_preferences_android 2.5.0 calls its Kotlin code through JNI (flutter/packages#12329). The JNI bindings of the plugin and package:jni 1.1.0 (lib/src/kotlin.dart) look up Kotlin classes by name: kotlin.coroutines.Continuation, kotlin.Unit, kotlin.Result, kotlin.Result$Failure, kotlin.coroutines.intrinsics.CoroutineSingletons and kotlin.coroutines.intrinsics.IntrinsicsKt. R8 renames them, and neither the plugin (it has no consumer ProGuard rules) nor jni (its consumer-rules.pro keeps only com.github.dart_lang.jni.**) keeps them. SharedPreferencesAsyncApiRegistrar itself survives thanks to @Keep (flutter/packages#13151), but R8 rewrites the descriptor of its getAll with the renamed Continuation, so the descriptor that the Dart bindings use no longer exists. From build/app/outputs/mapping/release/mapping.txt:

kotlin.coroutines.Continuation -> o1.d:

Workarounds that make the release build work:

  • constrain the Android implementation: flutter pub add 'shared_preferences_android:>=2.4.28 <2.5.0', or
  • keep the Kotlin classes in android/app/proguard-rules.pro:
-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 { *; }

Related: #193880 (closed; the generated classes got @Keep, and its PR notes that the Kotlin rules stay needed until jni ships them) and dart-lang/native#3732 (open; jni should keep these classes).

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final preferences = await SharedPreferencesWithCache.create(
    cacheOptions: const SharedPreferencesWithCacheOptions(),
  );
  final launches = (preferences.getInt('launches') ?? 0) + 1;
  await preferences.setInt('launches', launches);
  debugPrint('launches=$launches');
  runApp(MaterialApp(home: Scaffold(body: Center(child: Text('$launches')))));
}

Screenshots or Video

No response

Logs

Logs
E flutter: Unhandled Exception: PlatformException(PlatformException, java.lang.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;"
...
#3      SharedPreferencesWithCache.create

Seen on an Android 17 (API 37) arm64 emulator with an arm64 release APK.

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.44.2, on macOS 27.0.1 26A434 darwin-arm64, locale en-US)
    • Framework revision c9a6c48423 (4 months ago), 2026-06-10 15:52:41 -0700
    • Engine revision 77e2e94772
    • Dart version 3.12.2
[✓] Android toolchain - develop for Android devices (Android SDK version 37.0.0)
    • Platform android-37.0, build-tools 37.0.0
    • Java version OpenJDK Runtime Environment (build 21.0.10+-117844308-b1163.108)
[✓] Xcode - develop for iOS and macOS (Xcode 27.1)

Activity

  1. saurabh-mirajkar commented on Oct 19, 2026

    @saurabh-mirajkar
    Contributor

    @ybeshkarov Thanks for the report. I was able to reproduce this on a Pixel 6 running Android 17 (API 37) and latest Flutter stable (3.47.7).

    With shared_preferences_android 2.5.0, debug and profile builds work, but the release build stays on the splash screen and throws the reported NoSuchMethodError.

    Pinning shared_preferences_android to 2.4.28 makes the release build work in the same reproduction, confirming the regression between these versions in my test environment.

    Labelling this and routing it to the Android team for evaluation.

    Related: #193880 and dart-lang/native#3732.

  2. added
    c: regressionIt was better in the past than it is now
    c: crashStack traces logged to the console
    platform-androidAndroid applications specifically
    p: shared_preferencesPlugin to read and write Shared Preferences
    packageflutter/packages repository. See also p: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-androidOwned by Android platform team
    fyi-ecosystemFor the attention of Ecosystem team
    and removed
    in triagePresently being triaged by the triage team
    on Oct 19, 2026
  3. ybeshkarov commented on Oct 19, 2026

    @ybeshkarov
    Author

    Thanks for confirming, @saurabh-mirajkar.

    I've opened a fix for this: flutter/packages#13311. It adds android/consumer-rules.pro to shared_preferences_android with the six keep rules from Step 5 of Pigeon's native interop guide and lists it in consumerProguardFiles, so R8 applies them in every app that uses the plugin. With a dependency_overrides path to that branch, the reproduction above starts in a release build, and both SharedPreferencesAsync and the legacy SharedPreferences API work. The rules have a TODO to remove them once jni ships them (dart-lang/native#3732).

    The PR has the missing-tests label. As far as I can tell, no CI target in flutter/packages runs a release build of a plugin, so I asked for a test exemption there. If a release-mode test fits somewhere, I’m happy to add one.

  4. 13 remaining items

  5. ybeshkarov commented on Oct 21, 2026

    @ybeshkarov
    Author

    The fix is merged as 2.5.2, but the release run for it failed, so it isn't on pub.dev yet. Until it is, pin shared_preferences_android: 2.4.28 in dependency_overrides or add the keep rules from flutter/packages#13311 to android/app/proguard-rules.pro.

  6. nimr77 commented on Oct 21, 2026

    @nimr77

    This issue has been closed but it still in the package, will be any new update

  7. ybeshkarov commented on Oct 21, 2026

    @ybeshkarov
    Author

    @nimr77 The issue closed automatically when the fix (flutter/packages#13311) was merged, since issues here close on merge rather than on release. The job that publishes 2.5.2 to pub.dev failed on a CI infrastructure problem, and it's still early morning in the US for the team, so we're waiting for someone to re-run it. Until 2.5.2 is out, the workarounds from my comment above still work: pin shared_preferences_android: 2.4.28 in dependency_overrides, or add the keep rules from flutter/packages#13311 to android/app/proguard-rules.pro.

  8. nimr77 commented on Oct 21, 2026

    @nimr77

    @ybeshkarov
    Thanks, yes I used the work around its working, perfectly for our apps.

  9. ybeshkarov commented on Oct 22, 2026

    @ybeshkarov
    Author

    shared_preferences_android 2.5.2 with the fix is now on pub.dev. If you pinned 2.4.28 in dependency_overrides, remove the override, run flutter pub upgrade, and check that pubspec.lock has 2.5.2. The keep rules in proguard-rules.pro aren't needed anymore either.

  10. github-actions commented on Nov 5, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  11. locked as resolved and limited conversation to collaborators on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

c: crashStack traces logged to the consolec: regressionIt was better in the past than it is nowfyi-ecosystemFor the attention of Ecosystem teamhas reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyteam-androidOwned by Android platform team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions