Can not inject workmanager constructor with Hilt

我是研究僧i 提交于 2021-01-28 05:48:14

问题


I'm developing an Android app. and I'm trying to use hilt with workmanager constructor but it does not work and gives me this error :

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

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

    @Inject
    lateinit var workerFactory: HiltWorkerFactory

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


}

build.gradle (app) :

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

 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


回答1:


According to this documentation, you need to paste this code into your AndroidManifest to get Hilt's WorkManager Inject working.

<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



来源:https://stackoverflow.com/questions/63472624/can-not-inject-workmanager-constructor-with-hilt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!