[英]Hilt and WorkManager error : lateinit property WorkerFactory has not been initialized
I'm trying to inject WorkManager with Hilt.我正在尝试使用 Hilt 注入 WorkManager。 first I implement the documentation:首先我实现了文档:
Inject a Worker using the @HiltWorker annotation in the class and @AssistedInject in the Worker object's constructor.使用类中的@HiltWorker 注释和 Worker 对象的构造函数中的 @AssistedInject 注入一个 Worker。 You can use only @Singleton or unscoped bindings in Worker objects.您只能在 Worker 对象中使用 @Singleton 或无作用域绑定。 You must also annotate the Context and WorkerParameters dependencies with @Assisted:您还必须使用 @Assisted 注释 Context 和 WorkerParameters 依赖项:
@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:然后,让您的 Application 类实现 Configuration.Provider 接口,注入 HiltWorkFactory 的实例,并将其传递到 WorkManager 配置中,如下所示:
@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.注意:因为这自定义了 WorkManager 配置,所以您还必须从 WorkManager 文档中指定的 AndroidManifest.xml 文件中删除默认初始值设定项。
<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
...
The way you initialized your workmanager and workmanagerfactory is only working till workmanager version 2.5.X.您初始化 workmanager 和 workmanagerfactory 的方式仅适用于 workmanager 版本 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.随着 Workmanager 版本2.6.x-alphaX的更新,这种情况发生了变化,现在 workmanager 使用 androidx.startup 来初始化 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.您在这里有两个选择:要么降级回我建议的 2.5.0,因为这是当前的稳定版本,要么更改您初始化 workmanager 的方式。
If you want to keep your version, then change your Android Manifest the following:如果您想保留您的版本,请更改您的 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>
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'
resolving 2 issues made it work for me:解决 2 个问题使它对我有用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.