[英]Android WorkManager Worker can not be injected using Dagger Hilt `@WorkerInject`
I am trying to follow guide from https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager and encountered following error我正在尝试遵循https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager的指南并遇到以下错误
E/WM-WorkerFactory: Could not instantiate com.example.android.hilt.ExampleWorker
java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
To reproduce the issue, I have added the example code from the gude in the Dagger Hilt Example Repo为了重现该问题,我在Dagger Hilt Example Repo中添加了来自 gude 的示例代码
class ExampleWorker @WorkerInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters,
val workerDependency: AppNavigator
) : Worker(appContext, workerParams) {
override fun doWork(): Result {
Log.d("WORKER", "I am the worker, got dependency: $workerDependency")
return Result.success()
}
}
NOTE: The
AppNavigator
is provided in NavigationModule as@Binds abstract fun bindNavigator(impl: AppNavigatorImpl): AppNavigator
.注意:
AppNavigator
在NavigationModule中以@Binds abstract fun bindNavigator(impl: AppNavigatorImpl): AppNavigator
的形式提供。
Also note, replacingAppNavigator
withAppDatabase
which is@Singleton
does not help.另请注意,将
AppNavigator
替换为@Singleton
的AppDatabase
并没有帮助。
And this is how I start the worker from MainActivity
这就是我从
MainActivity
启动工作人员的方式
override fun onStart() {
super.onStart()
enqueueWorker(applicationContext)
}
private fun enqueueWorker(context: Context) {
val request = OneTimeWorkRequestBuilder<ExampleWorker>().build()
WorkManager.getInstance(context).enqueue(request)
}
Not sure what exactly is wrong.不确定到底出了什么问题。
UPDATE: I have created a brand new Android project to reproduce it.更新:我创建了一个全新的 Android 项目来重现它。 The project is attached to the issue#158843197 .
该项目附加到 issue#158843197 。 All the key file source code snapshot is available at GitHub Gist (if you want to do a quick review).
GitHub Gist上提供了所有关键文件源代码快照(如果您想快速查看)。
UPDATE#2: The solution更新#2:解决方案
On top of what Ian mentioned below , the issue was I missed following Gradle dependency in app/build.gradle
(mentioned in aosp#158843197 )除了下面提到的 Ian 之外,问题是我错过了
app/build.gradle
中的 Gradle 依赖项(在aosp#158843197中提到)
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
The dependency injection for Worker
is now working. Worker
的依赖注入现在正在工作。
Since androidx.work-*
version 2.6.0-alpha01
, WorkManager
uses androidx.startup
to initialize WorkManager.从
androidx.work-*
版本2.6.0-alpha01
, WorkManager
使用androidx.startup
来初始化 WorkManager。
For the new required changes to AndroidManifest.xml
, check this answer.对于
AndroidManifest.xml
所需的新更改,请查看此答案。
As per the WorkManager Configuration and Initialization documentation , to use the Configuration.Provider
interface on your Application
, you must remove the default initializer :根据WorkManager 配置和初始化文档,要在您的
Application
上使用Configuration.Provider
接口,您必须删除默认初始化程序:
<!-- In your AndroidManifest.xml -->
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />
Otherwise, the default initializer will still run, wiping out your custom intialization and its HiltWorkerFactory
.否则,默认初始化程序仍将运行,清除您的自定义初始化及其
HiltWorkerFactory
。
I had a similar issue but in my case, I had to use Hilt modules with @Provides annotation instead of @Binds annotation.我遇到了类似的问题,但就我而言,我必须使用带有 @Provides 注释而不是 @Binds 注释的 Hilt 模块。 I couldn't inject Hilt modules with @Binds annotation.
我无法使用 @Binds 注释注入 Hilt 模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.