Cannot Inject Coroutine Worker With Hilt
See original GitHub issueI 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'
Issue Analytics
- State:
- Created 2 years ago
- Comments:27 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
It can be a little messy to deal with all those depencies but we are targeting many purpose that worth the effort. 😊
Hi, I solve this problem workmanager version 2.7.1 hilt version 2.38.1
in my solution, i create custom workerFactory
in Application class
in MyWorker
in Manifest add this provider