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

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

我正在尝试使用 Hilt 注入 WorkManager。 首先我实现文档:

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, 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()

}

最后,我处理了文档中的这条注释:

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>

但是我得到这个错误:

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

您初始化 workmanager 和 workmanagerfactory 的方式只适用于 workmanager 版本 2。5.X。随着 Workmanager 版本 2.6.x-alphaX 的更新,这发生了变化,现在 workmanager 使用 androidx.startup 来初始化 WorkManager。

您在这里有两个选择:我建议降级回 2.5.0,因为这是当前的稳定版本,或者更改您初始化 workmanager 的方式。

如果您想保留您的版本,请更改您的 Android 清单如下:

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

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

此外,请确保您具有以下依赖项:

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'

解决了 2 个问题使它对我有用:

  1. Remove the default initializer
  2. 通过@EntryPoint 重构 HiltWorkerFactory 注入,如所述here