Unable To Set A Custom Worker Factory In Workmanager
Solution 1:
From the documentation of WorkerManager.initialize()
By default, this method should not be called because
WorkManageris automatically initialized. To initializeWorkManageryourself, please follow these steps:Disable
androidx.work.impl.WorkManagerInitializerin your manifest InApplication#onCreateor aContentProvider, call this method before callinggetInstance()
So what you need is to disable WorkManagerInitializer in your Manifest file:
<application
//...
android:name=".MyApplication">
//...
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="your-packagename.workmanager-init"
android:enabled="false"
android:exported="false" />
</application>
And in your custom Application class, initialize your WorkerManager:
classMyApplication : Application() {
overridefunonCreate() {
super.onCreate()
val daggerWorkerFactory = DaggerWorkerFactory(toInjectInWorker)
val configuration = Configuration.Builder()
.setWorkerFactory(daggerWorkerFactory)
.build()
WorkManager.initialize(context, configuration)
}
}
Note:
By default, WorkerManager will add a ContentProvider called WorkerManagerInitializer with authorities set to my-packagename.workermanager-init.
If you pass wrong authorities in your Manifest file while disabling the WorkerManagerInitializer, Android will not be able to compile your manifest.
Post a Comment for "Unable To Set A Custom Worker Factory In Workmanager"