简体   繁体   English

Android WorkManager Worker 无法使用 Dagger Hilt `@WorkerInject` 注入

[英]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 .注意: AppNavigatorNavigationModule中以@Binds abstract fun bindNavigator(impl: AppNavigatorImpl): AppNavigator的形式提供。
Also note, replacing AppNavigator with AppDatabase which is @Singleton does not help.另请注意,将AppNavigator替换为@SingletonAppDatabase并没有帮助。

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的依赖注入现在正在工作。

Update (March 24, 2021):更新(2021 年 3 月 24 日):

Since androidx.work-* version 2.6.0-alpha01 , WorkManager uses androidx.startup to initialize WorkManager.androidx.work-*版本2.6.0-alpha01WorkManager使用androidx.startup来初始化 WorkManager。
For the new required changes to AndroidManifest.xml , check this answer.对于AndroidManifest.xml所需的新更改,请查看答案。

Original Answer:原答案:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM