flutter_secure_storage: App built in release mode does not start

My app built in release mode does not get past the launch screen. When built in debug mode, everything works fine.

In my main method, I have the following code that will check if a previous login session exists so the user does not need to log in again each time.

Future<null> main(List<String> args) async {
    // ...
    final storage = FlutterSecureStorage();
    final hasExisting = await storage.read(key: "isExisting");
    // ...
    runApp(MyApp(hasExisting: hasExisting));
}

And I can confirm that this plugin is the one causing the problem because when I change the code to the following

Future<null> main(List<String> args) async {
    // ...
    // final storage = FlutterSecureStorage();
    // final hasExisting = await storage.read(key: "isExisting");
    final hasExisting = false;
    // ...
    runApp(MyApp(hasExisting: hasExisting));
}

Everything works, but of course the desired function is not the same.

I can confirm it was working as of March 5, but it no longer does.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 16
  • Comments: 23 (1 by maintainers)

Most upvoted comments

I fixed this issue by executing await storage.deleteAll(); only once, after setting backups off:

<application
        ...
            android:allowBackup="false"
            android:fullBackupContent="false">

I was having the same problem as above. The easy fix for me was to clear storage for the app.

Settings -> Apps -> Your App -> Storage -> Clear Storage

I have a similar issue. After installing .apk file and launching application on a device, it uses FlutterSecureStorage to get information if user is logged in or not, along with other values. I expect null during first run, of course, but storage.read(key) seams to fail before returning any value.

Added backup tags in main/AndroidManifest.xml

<application
       ...
        android:allowBackup="false"
        android:fullBackupContent="false">

Added deleting keys on startup

final FlutterSecureStorage storage = FlutterSecureStorage();

Future<void> checkIfUserIsSignedIn() async {
    storage.deleteAll();

    try{
      isUserSignedIn = await storage.read(key: _isLoggedIn);
    }
    catch (e){
      print(e);
    }
    setState(() {});
  }

Tried with FlutterSecureStorage v.3.3.3 and FlutterSecureStorage v.3.2.0, but there was no change. I checked SDK version of my device (Honor 10) - 29. Also checked on other device (Samsung Galaxy S9+), but the issue also occurs.

Running flutter run --release causes warnings:

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: C:\Users\karo\flutter\.pub-cache\hosted\pub.dartlang.org\flutter_secure_storage-3.2.0\android\src\main\java\com\it_nomads\fluttersecurestorage\FlutterSecureStoragePlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

and error:

Installing build\app\outputs\flutter-apk\app.apk...                376ms
Error: ADB exited with exit code 1
Performing Streamed Install

adb: failed to install C:\Users\karo\idom_mobile\build\app\outputs\flutter-apk\app.apk: Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE: Package com.project.idom signatures do not match previously installed version; ignoring!]
Uninstalling old version...
Installing build\app\outputs\flutter-apk\app.apk...                 6.9s

but successfully installs application on emulator and everything works as expected. The problem is when I install application on physical device. To generate .apk I use flutter build apk --release and install using app-release.apk file as stated in build log:

flutter build apk --release
You are building a fat APK that includes binaries for android-arm, android-arm64, android-x64.
If you are deploying the app to the Play Store, it's recommended to use app bundles or split the APK to reduce the APK size.
    To generate an app bundle, run:
        flutter build appbundle --target-platform android-arm,android-arm64,android-x64
        Learn more on: https://developer.android.com/guide/app-bundle
    To split the APKs per ABI, run:
        flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi
        Learn more on:  https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done                      69.9s
√ Built build\app\outputs\flutter-apk\app-release.apk (48.9MB).

My flutter doctor -v

flutter doctor -v
[√] Flutter (Channel dev, 1.24.0-10.2.pre, on Microsoft Windows [Version 10.0.18362.1198], locale en-GB)
    • Flutter version 1.24.0-10.2.pre at C:\Users\karo\flutter
    • Framework revision 022b333a08 (27 hours ago), 2020-11-18 11:35:09 -0800
    • Engine revision 07c1eed46b
    • Dart version 2.12.0 (build 2.12.0-29.10.beta)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\karo\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (1 available)
    • Android SDK built for x86 64 (mobile) • emulator-5554 • android-x64 • Android 10 (API 29) (emulator)

• No issues found!

I tried to make sure my project is migrated to AndroidX and got a message: No Usages Found in the Project.

I don’t have any more ideas, could you help me?

more info: running flutter run --release starts the app and it gets stuck on the launch screen with the following output to the console:

E/flutter (27083): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Unsupported value: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT, null)
E/flutter (27083): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564)
E/flutter (27083): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302)
E/flutter (27083): <asynchronous suspension>
E/flutter (27083): #2      FlutterSecureStorage.read (package:flutter_secure_storage/flutter_secure_storage.dart:16)
E/flutter (27083): <asynchronous suspension>
E/flutter (27083): #3      User.fetchExistingFromStorage (package:edataquick_invoice_capture/models/user.dart:144)
E/flutter (27083): <asynchronous suspension>
E/flutter (27083): #4      main (package:edataquick_invoice_capture/main.dart:23)
E/flutter (27083): <asynchronous suspension>
E/flutter (27083): #5      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:197)
E/flutter (27083): #6      _rootRun (dart:async/zone.dart:1124)
E/flutter (27083): #7      _CustomZone.run (dart:async/zone.dart:1021)
E/flutter (27083): #8      _runZoned (dart:async/zone.dart:1516)
E/flutter (27083): #9      runZoned (dart:async/zone.dart:1500)
E/flutter (27083): #10     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:190)
E/flutter (27083): #11     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300)
E/flutter (27083): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171)
E/flutter (27083): 

@juliansteenbakker I am also facing problem of PlatformException when I am downloading installing aab from market. Yes, the clear storage do the thing, but that’s incorrect behavior. Huawei Mate 20 light.

What is still unclear to me is, if this is happening “in the wild” or has something to do with development. Because on one of my new devices i am developing on this was happening again. But i am unable to reproduce it. It just suddenly happened. But i can imagine it has something to do with rapid deployment where the app is starting and all of the sudden a new version is deployed right in between where something with the en/decryption is happening and some keys get only stored half way because the app is restarting/redeploying. It never occurred on devices that are there only for testing not developing.

One thing i do now is to make a “fake” call to the storage (read some useless key) at the start of the application and .deleteAll() if an exception is thrown. This would potentially delete all the user data, but it is better than the other way, because the app is unusable if this happens. We “just” store credentials inside it, that could easily be reentered – its just for convenience. But its still unfortunate because i don’t know if our client plans to use it for something else in the future.

Having the same Problem, but not only with release, also in “debug”. One major thing i can remember is, that i updated the toolchain to (Android toolchain - develop for Android devices (Android SDK version 28.0.3) very recently and not much else.

Unhandled Exception: PlatformException(error, Unsupported value: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT, null)