segmentio / analytics-android

The hassle-free way to add analytics to your Android app.
https://segment.com/docs/sources/mobile/android/
MIT License
377 stars 214 forks source link

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

Open kozyrevsergey89 opened 3 years ago

kozyrevsergey89 commented 3 years ago

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 commented 3 years ago

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

aloz77 commented 11 months ago

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?