question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HiltWorkerFactory.createWorker is missing my HitlWorker class resulting in NoSuchMethodException

See original GitHub issue

I am using 1.0.0-alpha03 with

androidx.hilt:hilt-work
com.google.dagger:hilt-android-compiler
com.google.dagger:hilt-compiler

I am experiencing the following issue:

 java.lang.NoSuchMethodException: com.nicolasmilliard.socialcatsaws.imageupload.ImageUploadWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
        at java.lang.Class.getConstructor0(Class.java:2332)
        at java.lang.Class.getDeclaredConstructor(Class.java:2170)
        at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
        at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:244)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)

While debugging I was able to identify than my app does use HiltWorkerFactory but it’s createWorker method is not able to retrieve my @HiltWorker worker annotated class. It then delegate the creation to androidx.work.WorkerFactory.createWorkerWithDefaultFallback() which in turn fails to create my HiltWorker. I can confirm that my build folder contains a ImageUploadWorker_Factory generated class.

The test project I work on is open source.

The Worker code is available here

The Android Application code is available here

Let me know if you need anything else.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
danysantiagocommented, Feb 8, 2021

@niqo01, you want to add kapt("androidx.hilt:hilt-compiler:_") in the library module (image-upload:android) where the Worker source is, notice the difference in the group id, there are two compiler, the AndroidX one for the Jetpack extensions, including for Workers androidx.hilt:hilt-compiler and Hilt’s com.google.dagger:hilt-compiler.

Specifically in build.gradle.kts#L38 update the line to kapt("androidx.hilt:hilt-compiler:_")

To be clear, whenever you use the androidx.hilt annotations, apply androidx.hilt:hilt-compiler. Meanwhile, wherever you use dagger.hilt or just dagger annotations, apply com.google.dagger.hilt:hilt-compiler. If a combination of both are used, then apply both processors.

3reactions
niqo01commented, Feb 8, 2021

thank you @danysantiago, that was it. I did not realized they were two different hilt-compiler

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hilt and WorkManager error : java.lang ...
Hilt and WorkManager error : java.lang.NoSuchMethodException:<init> [class android.content.Context, class androidx.work.WorkerParameters] ; @ ...
Read more >
Android WorkManager Worker can not be injected using ...
NoSuchMethodException: <init> [class android.content. ... a sample app in comment#4 , looking at your build.gradle it seems you are missing a dependency:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found