Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.
Also note, replacing AppNavigator with AppDatabase which is @Singleton does not help.

And this is how I start the worker from 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. The project is attached to the issue#158843197. All the key file source code snapshot is available at GitHub Gist (if you want to do a quick review).


UPDATE#2: The solution

On top of what Ian mentioned below, the issue was I missed following Gradle dependency in app/build.gradle (mentioned in aosp#158843197)

kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

The dependency injection for Worker is now working.

like image 465
Hossain Khan Avatar asked Jun 13 '20 03:06

Hossain Khan


1 Answers

Update (March 24, 2021):

Since androidx.work-* version 2.6.0-alpha01, WorkManager uses androidx.startup to initialize WorkManager.
For the new required changes to AndroidManifest.xml, check this answer.

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:

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

like image 121
ianhanniballake Avatar answered Nov 14 '22 08:11

ianhanniballake