androidkotlinandroid-workmanagerdagger-hilt

Hilt and WorkManager error : lateinit property WorkerFactory has not been initialized


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
...

Solution

  • The way you initialized your workmanager and workmanagerfactory is only working till workmanager version 2.5.X. With the update of Workmanager Version 2.6.x-alphaX this changed and now workmanager is using androidx.startup to initialize WorkManager.

    You have two options here: Either downgrade back to 2.5.0 which I would suggest, because this is the current stable version OR change the way you initialize your workmanager.

    If you want to keep your version, then change your Android Manifest the following:

    <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities=\"${applicationId}.androidx-startup"
        android:exported="false"
        tools:node=\"merge">
        <!-- If you are using androidx.startup to initialize other components -->
        <meta-data
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:value="androidx.startup"
            tools:node="remove" />
     </provider>
    

    OR

     <!-- If you want to disable android.startup completely. -->
     <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove">
     </provider>
    

    Furthermore, make sure you have the following dependencies:

    implementation "com.google.dagger:hilt-android:$dagger_hilt_version"
    kapt "com.google.dagger:hilt-compiler:$dagger_hilt_version"
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    implementation "androidx.hilt:hilt-work:1.0.0"
    
    implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha05'