I have a project with dagger setup with following provider method:
@Module(...)
abstract class AppModule {
@Module
companion object {
...
@Provides
@Singleton
@JvmStatic
fun provideSharedPreferences(@AppContext context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
}
@Binds
@AppContext
@Singleton
abstract fun provideAppContext(application: Application): Context
}
And here's a code from application's onCreate()
:
override fun onCreate() {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build())
Timber.plant(Timber.DebugTree())
}
...
super.onCreate()
}
Running the project on API 27 emulator results in following behavior:
With following logs:
D/StrictMode: StrictMode policy violation; ~duration=275 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=196671 violation=2 at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440) at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251) at java.io.File.exists(File.java:807) at android.app.ContextImpl.getDataDir(ContextImpl.java:2197) at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:517) at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:714) at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:368) at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167) at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:526) at com.some.package.di.module.AppModule$Companion.provideSharedPreferences(AppModule.kt:112) ...
What this means, is that following res.exists()
reads from disk:
if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
new Throwable());
}
And because that's happening on UI thread - StrictModeDiskReadViolation
results.
Afaik, there does not exist an API to exclude some chunk of code (e.g. by package name) from StrictMode configuration. Practically, I'm ok to leave SharedPreferences
related stuff to read from disk on UI thread.
I do not want to turn off read/write StrictMode rule because of this problem.
Question
What is the correct way to gracefully recover from this scenario?
StrictMode.allowThreadDiskReads()
I had to performStrictMode.allowThreadDiskWrites()
also. – Delegation