analytics-android icon indicating copy to clipboard operation
analytics-android copied to clipboard

Android 11 IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked

Open kozyrevsergey89 opened this issue 5 years ago • 2 comments

Hi,

I'm using segment version 4.8.2 and got a crash on Android 11 (no issues on previous OS versions).

java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:494) at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:479) at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:188) at com.segment.analytics.internal.Utils.getSegmentSharedPreferences(Utils.java:309) at com.segment.analytics.ValueMap$Cache.<init>(ValueMap.java:395) at com.segment.analytics.ProjectSettings$Cache.<init>(ProjectSettings.java:76) at com.segment.analytics.Analytics$Builder.build(Analytics.java:1378)

This happens when we schedule some work with WorkManager and it executes after device is booted but user hasn't entered credentials for the first time. Job execution can not be moved to a later stage, and we still need to send analytics.

kozyrevsergey89 avatar Dec 14 '20 05:12 kozyrevsergey89

there is workaround suggested by Google: https://github.com/googlearchive/android-DirectBoot/blob/737ab9a214debbf3c940360b834077ccc3035ff3/Application/src/main/java/com/example/android/directboot/alarms/AlarmStorage.java

kozyrevsergey89 avatar Dec 22 '20 05:12 kozyrevsergey89

Still running into this issue with ACRA 5.11.3 when the Application is starting on LOCKED_BOOT_COMPLETED and calling ACRA.init(). If there is no general solution, is there a chance to add a method to ACRA.init() to set the context for the preference storage manually?

aloz77 avatar Dec 17 '23 04:12 aloz77