4

Due to background service limitations, we implemented our app with the help of WorkManager and replaced the AsyncTask. The app supports Direct-boot-mode, so we are storing our required SharedPreferences in Device encrypted storage. App was working fine before WorkManager implementation but now it is crashing after rebooting the device. logcat:

java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked
        at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:387)
        at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:372)
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167)
        at androidx.work.impl.utils.Preferences.getSharedPreferences(Unknown Source:12)
        at androidx.work.impl.utils.Preferences.needsReschedule(Unknown Source:0)
                                                setNeedsReschedule
        at androidx.work.impl.utils.ForceStopRunnable.shouldRescheduleWorkers(Unknown Source:6)
        at androidx.work.impl.utils.ForceStopRunnable.run(Unknown Source:18)
        at androidx.work.impl.utils.SerialExecutor$Task.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

We are providing device protected storage context while initializing the WorkManager: ContextCompat.createDeviceProtectedStorageContext(appContext); Please help, how to handle it.

PPB
  • 151
  • 1
  • 14

1 Answers1

0

This is the same question as - java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked

If you see the stack trace, it says, sharedpreferences is credential encrypted which means it will not work until the user has unlocked the device and the context that you are providing is for device encrypted storage.

To fix this, you need to use device encrypted storage instead of the credential encypted storage.

See the documentation here - https://developer.android.com/training/articles/direct-boot

Intkhab Ahmed
  • 118
  • 1
  • 8
  • for accessing sharedPreferences I'm using device encrypted storage only. I'm sure workmanger doing something internally. – PPB Jan 27 '20 at 05:51
  • We are neither using LOCKED_BOOT_COMPLETED nor android:directBootAware="true" but still getting this crash, any idea why? – Astha Garg Aug 23 '22 at 06:53