24

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.

Hossain Khan
  • 6,332
  • 7
  • 41
  • 55

2 Answers2

23

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.

Ace
  • 2,108
  • 22
  • 29
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    Thanks Ian, I wasn't aware of this. I have applied the rule in Manifest, and validated the record is removed from merged Manifest by using "Analyze APK". However, I am still getting same error. Sorry, I may be missing something silly I can't see. – Hossain Khan Jun 13 '20 at 06:41
  • 1
    I was facing the same issue and this answer solve it, thanks. Maybe it would be helpful if this snnipet (or some indication) had been included in [the Hilt+WorkManager guide](https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager)? – jorgeavilae Jun 13 '20 at 17:27
  • @jorgeavilae thanks for the update here. Strangely it's still not working for me. As a workaround, the solution mentioned in [Inject dependencies in classes not supported by Hilt](https://developer.android.com/training/dependency-injection/hilt-android#not-supported) worked for me. However, I would prefer to use Hilt's provided `@WorkerInject` solution. – Hossain Khan Jun 13 '20 at 17:37
  • I debugged source, looks like when `HiltWorkerFactory.createWorker()` is invoked, the `HiltWorkerFactory.mWorkerFactories: Map` has 0 items. Hence it's likely failing with the error mentioned above. I will start with brand new project to reproduce this issue. It's likely I am doing something wrong. – Hossain Khan Jun 13 '20 at 18:13
  • 3
    @jorgeavilae - I'd strongly encourage you to file [an issue against the documentation](https://issuetracker.google.com/issues/new?component=192697&template=845603) so that a link can be added. – ianhanniballake Jun 13 '20 at 18:29
  • 2
    I filed the documentation confusion issue at https://issuetracker.google.com/issues/158891026 – Hossain Khan Jun 14 '20 at 01:32
  • Got response from AOSP issue, I was missing `kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'` in my `app/build.gradle` ‍♂️. Working now! – Hossain Khan Jun 15 '20 at 19:17
  • nothing changed - https://stackoverflow.com/questions/73634284/could-not-instantiate-could-not-create-worker – user924 Sep 07 '22 at 11:43
0

I had a similar issue but in my case, I had to use Hilt modules with @Provides annotation instead of @Binds annotation. I couldn't inject Hilt modules with @Binds annotation.