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.

Cannot Inject Coroutine Worker With Hilt

See original GitHub issue

I am using Hilt With Coroutine Worker , I am getting this error when work is being dispatched ! I have searched for this issue but couldn’t find a solution , any help would be appreciated !

Error

E/WM-WorkerFactory: Could not instantiate com.wakaztahir.timeline.utils.workers.NotificationSetupWorker
    java.lang.NoSuchMethodException: com.wakaztahir.timeline.utils.workers.NotificationSetupWorker.<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)
        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:923)
E/WM-WorkerWrapper: Could not create Worker com.wakaztahir.timeline.utils.workers.NotificationSetupWorker

Application Class

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

    @Inject
    lateinit var workerFactory: HiltWorkerFactory
    
    override fun getWorkManagerConfiguration() =
        Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .build()

}

Here’s the actual Worker constructor

@HiltWorker
class NotificationSetupWorker @AssistedInject constructor(
    @Assisted val context: Context,
    @Assisted workerParams: WorkerParameters,
    val reminderDao: ReminderDao
) : CoroutineWorker(context, workerParams)

Gradle

Project Level :

classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version" // hilt_version = 2.35.1 & kotlin_version = 1.4.32

App Level :

plugins {
    id 'kotlin-kapt'
    //Hilt Plugin
    id 'dagger.hilt.android.plugin'
}
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
implementation 'androidx.hilt:hilt-work:1.0.0'
kapt 'androidx.hilt:hilt-compiler:1.0.0'

//Work Manager
def work_version = "2.6.0-alpha02"
implementation "androidx.work:work-runtime-ktx:$work_version"
implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'

https://stackoverflow.com/questions/67388068/injecting-coroutineworker-with-hilt-could-not-instantiate-woker

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:27 (1 by maintainers)

github_iconTop GitHub Comments

50reactions
jforatiercommented, Sep 8, 2021

Hi, thanks for sharing. Think i had same issue there. I am interested in hearing your feedback @forntoh 😃

On my side i follow this stackoverflow answer and that’s correct, for me it was a dependencies issue.

I forgot to add

kapt("androidx.hilt:hilt-compiler:1.0.0")

only have

kapt("com.google.dagger:hilt-android-compiler:2.38.1")

So, here the complete depencies part fort Hilt, Work and Coroutines

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'

implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation("androidx.hilt:hilt-work:1.0.0")
kapt("androidx.hilt:hilt-compiler:1.0.0")

implementation "androidx.work:work-runtime:2.6.0"
implementation "androidx.work:work-runtime-ktx:2.6.0"

It can be a little messy to deal with all those depencies but we are targeting many purpose that worth the effort. 😊

15reactions
mbahgojolcommented, Nov 22, 2021

Hi, I solve this problem workmanager version 2.7.1 hilt version 2.38.1

in my solution, i create custom workerFactory

class MyWorkerFactory @Inject constructor(private val repository: Repository) : WorkerFactory() {
    override fun createWorker(
        appContext: Context,
        workerClassName: String,
        workerParameters: WorkerParameters
    ): ListenableWorker = MyWorker(appContext, workerParameters, repository)
}

in Application class

@HiltAndroidApp
class MainApplication : Application(), Configuration.Provider {
    @Inject
    lateinit var myWorkerFactory: MyWorkerFactory

   override fun getWorkManagerConfiguration(): Configuration = Configuration.Builder()
        .setMinimumLoggingLevel(android.util.Log.DEBUG)
        .setWorkerFactory(myWorkerFactory)
        .build()
}

in MyWorker

@HiltWorker
class MyWorker @AssistedInject constructor(
    @Assisted appContext: Context,
    @Assisted workerParameters: WorkerParameters,
    private val repository: Repository
) : RxWorker(appContext, workerParameters) {
    override fun createWork(): Single<Result> {
        val id = inputData.getInt(PaymentLimitReceiver.ID_ORDER, 0)
        return repository.cancelOrder(id)
            .map {
                Result.success()
            }
            .doOnError {
                Result.retry()
            }
    }

}

in Manifest add this provider

<provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            tools:node="remove" />
Read more comments on GitHub >

github_iconTop Results From Across the Web

Injecting CoroutineWorker with Hilt : Could not instantiate woker
It cannot instantiate the worker because those dependencies are included in the constructor of the worker , It was working before I added...
Read more >
Use Hilt with other Jetpack libraries - Android Developers
Provide a ViewModel by annotating it with @HiltViewModel and using the @Inject annotation in the ViewModel object's constructor. Kotlin Java ...
Read more >
Android WorkManager Worker can not be injected using ...
Android WorkManager Worker can not be injected using Dagger Hilt `@WorkerInject` [158843197] - Visible to Public - Issue Tracker.
Read more >
Customize WorkManager with AppStartup & Hilt - Droidcon
Worker classes. As the last step, we'll use Hilt annotations on the BlurWorker class to complete the dependency-injection process and to get the ......
Read more >
[Solved]-Injecting CoroutineWorker using Hilt-kotlin
[Solved]-Injecting CoroutineWorker using Hilt-kotlin ... Please check your Hilt Modules for bad imports. PS: I've included imports for you. AndroidManifest.xml. < ...
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