I'm using Android work manager with a custom initialization. to do that i disable auto-initialization in the manifest like this
<provider
tools:replace="android:authorities"
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.work_manager_init"
android:enabled="false"
android:exported="false" />
And in application code I use this code
private fun initWorkManager() {
WorkManager.initialize(this, Configuration.Builder().run {
setWorkerFactory(appComponent.daggerWorkerFactory())
build()
})
}
And it works fine when I run my application. But when I'm testing with roboletric any class that needs the context by RuntimeEnvironment.application
throws this exception
java.lang.IllegalStateException: WorkManager is already initialized.
Did you try to initialize it manually without disabling
WorkManagerInitializer? See WorkManager#initialize(Context,
Configuration) or the class levelJavadoc for more information.
The initWorkManager()
get calls and throw this beacuse it doesn't know auto-init is already disabled in the manifest and somehow my test cannot read the values from the manifest file.
Any help or suggestion will be appritiated.
WorkManager.initialize()
as above I get the same error. If I don't call initialize(), I instead get ```java.lang.IllegalStateException: WorkManager is not initialized properly. The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.`` It seems like there's not a good way out of this. It complains whether or not initialize() is called. Catch 22. – Sheaves