flutter_secure_storage icon indicating copy to clipboard operation
flutter_secure_storage copied to clipboard

Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)'

Open tahitiangabriel opened this issue 3 years ago • 20 comments

PlatformException(Exception encountered, read, java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' on a null object reference at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$b.run(SourceFile:24) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.os.HandlerThread.run(HandlerThread.java:67) , null)

This issue was supposed to be fixed with #332 , but is still hapenning in 5.0.2

Only Android devices are affected by this issue.

tahitiangabriel avatar Mar 15 '22 00:03 tahitiangabriel

I encountered a similar error. What I did was disabling backup in my AndroidManifest.xml:

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

dustincatap avatar Mar 27 '22 02:03 dustincatap

Hi everyone! I've been having this issue as well. As @tahitiangabriel say, it's mentioned in #332 but also in #53. This last one is the most similar to my error, as the people updating the app are getting this error that leaves the APP totally broken.

It seems that 5.0.2 is not resolving these issues as it appears that there are people reopening and commenting on these issues. In my case, not everybody gets the error, it's like 50/50, don't know if it may be linked to SO versions, iOS, Android or what...

I will try to add a button to "Clear cache" or automatically deleteKeys on error to see if it works but definitely is not the best option... Let's see if we find a solution soon 😢

@dustincatap Could you explain your solution and why should work? Don't know why backup could be causing trouble in here. Thanks!

AlgoritmoDelCambio avatar Apr 13 '22 11:04 AlgoritmoDelCambio

E/FlutterSecureStoragePl( 6611): EncryptedSharedPreferences initialization failed
E/FlutterSecureStoragePl( 6611): com.google.crypto.tink.shaded.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
E/FlutterSecureStoragePl( 6611): 	at com.google.crypto.tink.shaded.protobuf.y.K(GeneratedMessageLite.java:10)
E/FlutterSecureStoragePl( 6611): 	at com.google.crypto.tink.shaded.protobuf.y.H(GeneratedMessageLite.java:2)
E/FlutterSecureStoragePl( 6611): 	at p4.c0.X(Keyset.java:1)
E/FlutterSecureStoragePl( 6611): 	at m4.d.a(SharedPrefKeysetReader.java:1)
E/FlutterSecureStoragePl( 6611): 	at i4.b.a(CleartextKeysetHandle.java:1)
E/FlutterSecureStoragePl( 6611): 	at m4.a$b.e(AndroidKeysetManager.java:4)
E/FlutterSecureStoragePl( 6611): 	at m4.a$b.f(AndroidKeysetManager.java:1)
E/FlutterSecureStoragePl( 6611): 	at m4.a$b.d(AndroidKeysetManager.java:3)
E/FlutterSecureStoragePl( 6611): 	at z0.a.b(EncryptedSharedPreferences.java:8)
E/FlutterSecureStoragePl( 6611): 	at z0.a.a(EncryptedSharedPreferences.java:1)
E/FlutterSecureStoragePl( 6611): 	at g7.d.s(FlutterSecureStoragePlugin.java:7)
E/FlutterSecureStoragePl( 6611): 	at g7.d.p(FlutterSecureStoragePlugin.java:8)
E/FlutterSecureStoragePl( 6611): 	at g7.d.b(FlutterSecureStoragePlugin.java:1)
E/FlutterSecureStoragePl( 6611): 	at g7.d$b.run(FlutterSecureStoragePlugin.java:23)
E/FlutterSecureStoragePl( 6611): 	at android.os.Handler.handleCallback(Handler.java:938)
E/FlutterSecureStoragePl( 6611): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/FlutterSecureStoragePl( 6611): 	at android.os.Looper.loopOnce(Looper.java:201)
E/FlutterSecureStoragePl( 6611): 	at android.os.Looper.loop(Looper.java:288)
E/FlutterSecureStoragePl( 6611): 	at android.os.HandlerThread.run(HandlerThread.java:67)
E/flutter ( 6611): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(Exception encountered, read, java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' on a null object reference
E/flutter ( 6611): 	at g7.d$b.run(FlutterSecureStoragePlugin.java:24)
E/flutter ( 6611): 	at android.os.Handler.handleCallback(Handler.java:938)
E/flutter ( 6611): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter ( 6611): 	at android.os.Looper.loopOnce(Looper.java:201)
E/flutter ( 6611): 	at android.os.Looper.loop(Looper.java:288)
E/flutter ( 6611): 	at android.os.HandlerThread.run(HandlerThread.java:67)
E/flutter ( 6611): , null)
E/flutter ( 6611): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607)
E/flutter ( 6611): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177)
E/flutter ( 6611): <asynchronous suspension>
E/flutter ( 6611): #2      main (package:ddv_loyalty/main.dart:37)
E/flutter ( 6611): <asynchronous suspension>
E/flutter ( 6611): 

version 5.0.2 still error when build app release mode

kzjn10 avatar May 07 '22 14:05 kzjn10

You are getting

E/FlutterSecureStoragePl( 6611): EncryptedSharedPreferences initialization failed

Initialization failed does not seem to be the same case that we are exposing here. Are you sure that you've reloaded properly your app after installing the library?

AlgoritmoDelCambio avatar May 08 '22 11:05 AlgoritmoDelCambio

i happen when i build to physical device in Release mode

kzjn10 avatar May 08 '22 15:05 kzjn10

i happen when i build to physical device in Release mode

+1

Yotam124 avatar May 11 '22 08:05 Yotam124

I am also facing similar issue, issue: flutter PlatformException(Exception encountered, write, java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference

JapneetSingh-28 avatar May 26 '22 06:05 JapneetSingh-28

Any further update on this issue?

Kang-Kim1 avatar Jun 21 '22 14:06 Kang-Kim1

I had this same error appear to me in a very specific scenario. I hope this report will bring some light in the issue.

My app was working fine during development until I tried to uninstall and install it again to be sure that everything was going to work properly. Then the error occurred. I was using the --release flag on build.

It seems to only happen on Android 11 (tried on two Xiaomi Mi 10T and on Samsung A31) on my code. I tried to reproduce this scenario on other Android versions (6, 9 and 12) but everything is running smoothly, even after reinstalling the app.

Firebase Crashlytics reports the error happening when I call .containsKey or .read when trying to read previously stored user data to determine whether to show Login Page or the main route.

lucianosb avatar Jul 06 '22 18:07 lucianosb

@juliansteenbakker can someone please take a closer look at this? This is affecting a ton of users and really hard to debug.

acoutts avatar Aug 23 '22 14:08 acoutts

I downgraded to 4.2.1 and disabling encryptedSharedPreferences fixed this for me.

acoutts avatar Aug 24 '22 13:08 acoutts

I have the same problem. Occurs continuously in release mode. It's a complicated environment to debug, so it's hard to deal with... 😢

curogom-ksd avatar Sep 05 '22 02:09 curogom-ksd

@lucianosb

I solved this problem.

If your versions upper 5.0.0 & it was build to release mode, try to this code.

storage.read(key: 'key') to storage.read(key: 'key', aOption: AndroidOptions(encryptedSharedPreferences: true,)

curogom-ksd avatar Sep 05 '22 06:09 curogom-ksd

I've tried https://github.com/mogol/flutter_secure_storage/issues/380#issuecomment-1236577186 and also an update to 6.0.0 but the main issue is still pressent.

@mogol For now this is blocker for our app :(

henry2man avatar Sep 09 '22 08:09 henry2man

@mogol Same thing. allowBackup is false, fullBackupContent is false, aOption: AndroidOptions(encryptedSharedPreferences: true,) is on every call. The exception is happening on devices using Android 10 and above. I'm mostly getting it when accessing EncryptedSharedPreferences during background work (using workmanager).

This issue is completely preventing users from using the app. Could share some thoughts on what the issue could be ? It clearly has to do with getting the app's context.

TheCarpetMerchant avatar Sep 10 '22 09:09 TheCarpetMerchant

One of my users unblocked himself by Force stopping the app then clearing data & cache. Didn't work for others but I have no way of knowing if they proceeded correctly or if this is really what unlocked that one user.

So it seems forcing a deletion solves the problem. Any way of doing this reliably @mogol ?

TheCarpetMerchant avatar Sep 12 '22 18:09 TheCarpetMerchant

I will check once more. But my previous attempts to reproduce and fix reliably were not very successful. So no promises 🤷‍♂️

mogol avatar Sep 12 '22 20:09 mogol

I have put resetOnError to true on all accesses, and the error still occurs.

TheCarpetMerchant avatar Sep 14 '22 18:09 TheCarpetMerchant

Alright, seem I'm getting contacted at least once a day by a user with this issue, I'm going to have to drop this package.

@mogol Please put a warning for the use of EncryptedSharedPreferences with this package. It is not stable and such a warning would have saved me a whole lot of trouble. Best of luck man.

TheCarpetMerchant avatar Sep 15 '22 06:09 TheCarpetMerchant

I solved this problem with

static AndroidOptions _getAndroidOptions() => const AndroidOptions( keyCipherAlgorithm: KeyCipherAlgorithm.RSA_ECB_OAEPwithSHA_256andMGF1Padding, storageCipherAlgorithm: StorageCipherAlgorithm.AES_GCM_NoPadding, );

hungnm1908 avatar Sep 19 '22 02:09 hungnm1908

Any update in that topic? We are having few crashes on the daily basis. The error message looks similar:

java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference

We are using encryptedSharedPreferences option enabled.

kamilzajacdeviniti avatar Sep 26 '22 06:09 kamilzajacdeviniti

Any update?

indraseptama avatar Oct 03 '22 05:10 indraseptama

Our crash analysis tool shows us, that this situation mostly occurs on Android 6, especially on Huawei P8 Lite device. Maybe that will help?

kamilzajacdeviniti avatar Oct 03 '22 06:10 kamilzajacdeviniti

Hello, any update? I'm still getting this error.

How to reproduce:

  1. Install apps from play store.
  2. Uninstall apps
  3. Install apps from play store again.
  4. Open apps, and when apps call the "read" method will catch this error.

indraseptama avatar Oct 06 '22 07:10 indraseptama

I think this is related to this https://developer.apple.com/forums/thread/36442 and https://stackoverflow.com/questions/4747404/delete-keychain-items-when-an-app-is-uninstalled

techouse avatar Oct 06 '22 09:10 techouse

We are having this issue on the Android platform only - it's not related to iOS. As far as I know, the Android removes the data from the encrypted preferences once app is uninstalled.

kamilzajacdeviniti avatar Oct 06 '22 09:10 kamilzajacdeviniti

We are having this issue on the Android platform only - it's not related to iOS. As far as I know, the Android removes the data from the encrypted preferences once app is uninstalled.

Not sure. What if the manifest has android:allowBackup="true"?

techouse avatar Oct 06 '22 09:10 techouse

I'm wondering, do using both FlutterSecureStorage and SharedPreferences in the same app can cause this kind of errors ?

jeff-odopass avatar Oct 27 '22 10:10 jeff-odopass

@jeff-odopass That's a great question. We had the same combination in our app... Do you have some additional info as to why you suspect that that combination could cause problems?

jfahrenkrug avatar Oct 27 '22 10:10 jfahrenkrug

@jfahrenkrug I have no clue and my app also have both plugins working simultaneously.
What is strange is that both plugins triggers the same error, which makes me think it may not be plugin-specific. Both plugins may also not have been designed to work together so I will give it a try by replacing SharedPreferences by FlutterSecureStorage. Keeping you updated with that

jeff-odopass avatar Oct 27 '22 11:10 jeff-odopass