I'm trying to inject WorkManager with Hilt. first I implement the documentation:
Inject a Worker using the @HiltWorker annotation in the class and @AssistedInject in the Worker object's constructor. You can use only @Singleton or unscoped bindings in Worker objects. You must also annotate the Context and WorkerParameters dependencies with @Assisted:
@HiltWorker
class RetreiveQuestionWorkManager @AssistedInject constructor(
@Assisted val appContext : Context,
@Assisted val workerParameters: WorkerParameters,
val questionDao: QuestionDao,
val questionCacheMapper: QuestionCacheMapper)
: CoroutineWorker(appContext, workerParameters) {
...
}
then I applied this from documentation:
Then, have your Application class implement the Configuration.Provider interface, inject an instance of HiltWorkFactory, and pass it into the WorkManager configuration as follows:
@HiltAndroidApp
class MyApp : Application(), Configuration.Provider {
@Inject lateinit var workerFactory: HiltWorkerFactory
override fun getWorkManagerConfiguration() =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
}
finally, I take care of this note from the documentation:
Note: Because this customizes the WorkManager configuration, you also must remove the default initializer from the AndroidManifest.xml file as specified in the WorkManager docs.
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>
but i get this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{mohalim.contest.alarm/mohalim.contest.alarm.ui.splash.SplashActivity}: kotlin.UninitializedPropertyAccessException: lateinit property workerFactory has not been initialized
...
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property workerFactory has not been initialized
...