22

Everything works fine in debug mode and release on emulator, but reading values from FlutterSecureStorage fails on a physical device. Other functionalities in application work properly, when I comment using FlutterSecureStorage.

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?

EDIT

I tried using SharedPreferences as a temporary workaround, but it still didn't work - as I guess, the package was not initialized.

I built my app with flutter build apk --release --no-shrink after reading this thread https://github.com/flutter/flutter/issues/65334.

Then tried flutter build apk --release --no-shrink with the version of my application that uses FlutterSecureStorage and it worked.

Should I really consider it as a valid fix and always build my application this way?

krlxen
  • 341
  • 2
  • 6
  • I am having the same issue but I found out that it does not work only on few devices (my Samsung Galaxy S20 FE Android Version 11, One UI 3.1 and my friends OnePlus 8T Android 11). But it works on many of other devices running on Android 11. So, the actual problem is not clear yet. I'll try your fix and let know if it fixes mine. – Prabesh Apr 14 '21 at 12:55
  • nope did not work but I have posted solution as answer – Prabesh Apr 15 '21 at 07:03

3 Answers3

2

How are you initializing the package? The issue seems to occur while the app runs in the background, A workaround posted in this thread is by registering the plugins on Android's MainActivity - this adds FlutterSecureStoragePlugin.

class MainActivity: FlutterActivity() {
  override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine)
  }
}
Omatt
  • 8,564
  • 2
  • 42
  • 144
1

You can fix this like I did

Check in pubspec.yml

environment:
  sdk: ">=2.16.2 <3.0.0"

and

flutter_secure_storage: ^4.2.1

Check in android/app/build.gradle

compileSdkVersion 32
laxminarayan1998
  • 838
  • 8
  • 13
-2

did you add the permission to your manifest file?

 <uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission  android:name="android.permission.READ_EXTERNAL_STORAGE"/>