Can not inject workmanager constructor with Hilt

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


Solution 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

Solution 2:

The answer provided by @Attila works for WorkManager versions 2.6 and below.

For versions 2.6+, you need to remove the WorkManagerInitializer node as follows:

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