简体   繁体   English

无法使用 Hilt 注入 workmanager 构造函数

[英]Can not inject workmanager constructor with Hilt

I'm developing an Android app.我正在开发一个 Android 应用程序。 and I'm trying to use hilt with workmanager constructor but it does not work and gives me this error:我正在尝试将 hilt 与 workmanager 构造函数一起使用,但它不起作用并给我这个错误:

2020-08-18 19:01:09.989 18125-18759/com. E/WM-WorkerFactory: Could not instantiate example.android.app.database.DeleteNotesWorker
    java.lang.NoSuchMethodException: example.android.app.database.DeleteNotesWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
        at java.lang.Class.getConstructor0(Class.java:2328)
        at java.lang.Class.getDeclaredConstructor(Class.java:2167)
        at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
        at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:242)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

this is the worker class:这是工人 class:

class DeleteNotesWorker @WorkerInject constructor(
    @Assisted context: Context,
    @Assisted workerParams: WorkerParameters,
    private val dao : NotesDao
) : CoroutineWorker(context, workerParams) {

    override suspend fun doWork(): Result {
        dao.deleteNotesInTrash()
         return Result.success()
    }


}

app module:应用模块:

@Module
@InstallIn(ApplicationComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun getNotesDatabase(@ApplicationContext context: Context) =
        Room.databaseBuilder(context, NotesDatabase::class.java, NotesDatabase.DB_NAME).build()

    @Singleton
    @Provides
    fun getDoa(db: NotesDatabase) = db.notesDao()

}

application class:申请 class:

@HiltAndroidApp
class App : Application(), Configuration.Provider {

    @Inject
    lateinit var workerFactory: HiltWorkerFactory

    override fun getWorkManagerConfiguration(): Configuration {
        return Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .build()
    }


}

build.gradle (app): build.gradle(应用):

 dependencies {

.......

 //Hilt
    implementation "com.google.dagger:hilt-android:2.28-alpha"
    kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
    
    //Hilt with workManager
    implementation 'androidx.hilt:hilt-work:1.0.0-alpha02'
    kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02'
}

build.gradle (project): build.gradle(项目):

 dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    ............

        //Hilt
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'

        ..........
    }

Did I do anything wrong in this case?在这种情况下我做错了什么吗? if yes please help me with it如果是,请帮助我

According to this documentation, you need to paste this code into your AndroidManifest to get Hilt's WorkManager Inject working.根据文档,您需要将此代码粘贴到您的 AndroidManifest 中,以使 Hilt 的 WorkManager Inject 正常工作。

<provider
    android:name="androidx.work.impl.WorkManagerInitializer"
    android:authorities="${applicationId}.workmanager-init"
    tools:node="remove" />

I had the same problem as you some moment ago and this fixed it我刚才和你有同样的问题,这解决了它

The answer provided by @Attila works for WorkManager versions 2.6 and below. @Attila提供的答案适用于WorkManager 2.6 及以下版本。

For versions 2.6+, you need to remove the WorkManagerInitializer node as follows:对于 2.6+ 版本,您需要删除WorkManagerInitializer节点,如下所示:

 <provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">

    <meta-data
        android:name="androidx.work.WorkManagerInitializer"
        android:value="androidx.startup"
        tools:node="remove" />
 </provider>

The WorkManager documentation provides more information about this. WorkManager 文档提供了更多相关信息。

In case you're a dummy like me, don't forgot to include kapt 'androidx.hilt:hilt-compiler:1.0.0' to your dependencies.如果您像我一样是个笨蛋,请不要忘记将kapt 'androidx.hilt:hilt-compiler:1.0.0'添加到您的依赖项中。

You will get the same exception if the androidx hilt compiler extension is missing and you will also not get any build warnings or errors.如果缺少 androidx hilt 编译器扩展,您将遇到相同的异常,并且您也不会收到任何构建警告或错误。

Since work version 2.6 it works using below androidmanifest.xml自工作版本 2.6 起,它使用以下 androidmanifest.xml

AndroidManifest.xml AndroidManifest.xml

<provider
     android:name="androidx.startup.InitializationProvider"
     android:authorities="${applicationId}.androidx-startup"
     tools:node="remove">
</provider>

and in case of Worker class some annotation was changed.在 Worker class 的情况下,某些注释已更改。 @HiltWorker, @AssistedInject @HiltWorker,@AssistedInject

@HiltWorker
class RefreshWorker @AssistedInject constructor(
    @Assisted appContext: Context,
    @Assisted workerParams: WorkerParameters,
    val myRepository: MyRepository
) : CoroutineWorker(appContext, workerParams) {

    override suspend fun doWork(): Result {
        return Result.success()
    }

}

Developer site ( worker api ) https://developer.android.com/jetpack/androidx/releases/hilt开发者网站(工人 api) https://developer.android.com/jetpack/androidx/releases/hilt

Replace @WorkerInject with @HiltWorker.将@WorkerInject 替换为@HiltWorker。 @HiltWorker is now a type annotation and requires the usage of @AssistedInject in the constructor. @HiltWorker 现在是类型注释,需要在构造函数中使用 @AssistedInject。 (Ic2f15) (IC2F15)

Developer site ( remove default worker initializer ) https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#remove-default开发人员站点(删除默认工作初始化程序) https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#remove-default

remove the default initializer To provide your own configuration, you must first remove the default initializer.移除默认初始化器 要提供您自己的配置,您必须首先移除默认初始化器。 To do so, update AndroidManifest.xml using the merge rule tools:node="remove".为此,请使用合并规则工具更新 AndroidManifest.xml:node="remove"。

Since WorkManager 2.6, App Startup is used internally within WorkManager.从 WorkManager 2.6 开始,App Startup 在 WorkManager 内部使用。 To provide a custom initializer you need to remove the androidx.startup node.要提供自定义初始化程序,您需要删除 androidx.startup 节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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