-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot Inject Coroutine Worker With Hilt #2601
Comments
Hmm, this all looks fine... Double check your import statements and that you have your If possible if you attach a debugger in |
I've fixed the issue Since using work manager version above than 2.6.0-alpha01 , Work Manager version above 2.6.0-alpha01 uses startup initializer Read Here <provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider> https://stackoverflow.com/questions/67388068/injecting-coroutineworker-with-hilt-could-not-instantiate-woker did the trick ! |
It stopped working in my Compose app |
Make sure your Gradle version and kotlin version match and updated , proceed with by providing this provider in manifest and see if it fixes |
I use compose and it works well |
@wakaztahir I'm using Android Gradle Plugin 7.1.0-alpha09 and Kotlin 1.5.21
Despite all that I still get |
This provider is only for WorkManager version above than 2.6.0-alpha01 so I would suggest you to update WorkManger and see if it works Wrong :
Correct : <provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider> |
try |
Hi @forntoh did you find anything to make it work? |
@abhriyaroy What error are you getting & which version of Work Manager are you using ? |
Hi @wakaztahir, I am using workmanager 2.6.0 and I was using Hilt
|
@forntoh I have edited my answer because your provider in the manifest was incorrect , Please update it , I didn't notice it before and also update your work manager version to greater than 2.6.0-alpha 01 @abhriyaroy Please check your work manager provider , does it exactly match with this ?
|
@abhriyaroy It would help if you could post your work manager configuration if it does not work |
I fixed it, but I can't even remember what I did, I will take a look at my code and update this thread later. |
I actually switched to what you see here because the correct one wasn't working 😃. Anyways, I have fixed the issue. I'll post my solution soon. |
Hi, thanks for sharing. On my side i follow this stackoverflow answer and that's correct, for me it was a dependencies issue. I forgot to add
only have
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 in my solution, i create custom workerFactory
in Application class
in MyWorker
in Manifest add this provider
|
i have the same error the solution for me was add
and too this librery implementation "androidx.startup:startup-runtime:1.0.0" |
For those who still have trouble despite following the steps above, the line below apparently solved the problem:
|
None of the solutions helped me, still seeing the same problem. Dagger version 2.42.3, using these dependencies:
The following in AndroidManifest.xml, inside application: <provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" /> My application class: @HiltAndroidApp
class App : Application(), Configuration.Provider {
@Inject
lateinit var workerFactory: HiltWorkerFactory
override fun getWorkManagerConfiguration(): Configuration {
return Configuration.Builder() // This does get called, breakpoints here stop
.setWorkerFactory(workerFactory)
.build()
}
} The worker: @HiltWorker
class EnrolWorker @AssistedInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters,
private val getBestFacility: GetBestFacilityUseCase,
) : CoroutineWorker(appContext, workerParams) This happens using compose 1.3.0-beta02, kotlin 1.7.10, gradle plugin 7.3.0-rc01 Perhaps also worthy of note, I have the app package moved to build.gradle inside the android scope, but adding it back to AndroidManifest.xml did not make a difference, replacing ${applicationId} with my actual package name did not help either |
After a day of trying all alternative options, changing versions, I too cannot resolve issue. I can also replicate this same issue on googles sunflower app using latest versions. Versions:
Provider in manifest:
Worker:
App class:
|
Same situation here @davidhowe were you able to resolve this? |
Make sure you initialize your WorkManager get getting an instance and not use
|
So I was able to resolve this by changing my app module to this
|
Wow man, after 3 days of struggling with why my solution doesn't work, your approach fits like a glove. Thanks for sharing it! |
Just going to share 1 thing I encountered but haven't seen mentioned in all of the places I found while googling this issue. While migrating my app from Dagger 2 to Hilt, I followed the instructions very closely and still ran into a similar issue. While debugging I noticed that my injected workerFactory was null (along with all of my injected fields) at the time getWorkManagerConfiguration was called within my application class. Might be worth explicitly documenting somewhere that it may be safer for existing applications with a number of @injects to make sure they put @Inject HiltWorkerFactory at the top to avoid unexpected behaviour :) |
==> AndroidManifest:
==> Application Class: @entrypoint
=> Application onCreate() { Finally getting success above code. Now initialize workmanager perfectly. |
Creating CUSTOM WORKER-FACTORY was the only thing that worked for me. |
I have followed all of the steps and answers provided but this answer from this question saved my day! |
The answer #2601 (comment) helped me. The only solution on the whole internet. |
If everything else fails, and it still doesn't work, make sure you do not call WorkManager.initialize yourself (Got from this S/O answer) @Provides
@Singleton
override fun create(@ApplicationContext context: Context): WorkManager {
// val configuration = Configuration.Builder().build()
// WorkManager.initialize(context, configuration)
return WorkManager.getInstance(context)
} |
For those who still have this problem. |
For those who are using ksp, make sure to add both of these compilers. This fixed it for me.
|
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
Application Class
Here's the actual Worker constructor
Gradle
Project Level :
App Level :
https://stackoverflow.com/questions/67388068/injecting-coroutineworker-with-hilt-could-not-instantiate-woker
The text was updated successfully, but these errors were encountered: