Hey guys hitting an issue trying to inject into a ...
# android
b
Hey guys hitting an issue trying to inject into a Worker with Hilt 🧵
Copy code
E/WM-WorkerFactory: Could not instantiate am.planogr.planogram.editor.work.DownloadUseWorker
constructor
Copy code
@HiltWorker
class DownloadUseWorker @AssistedInject constructor(
    @Assisted appContext: Context,
    @Assisted workerParams: WorkerParameters,
    private val api: DynamicUrlHitService,
) : CoroutineWorker(appContext, workerParams) {
work related deps update to 2.6.0-beta01 and hilt work is at 1.0.0
also have this in my manifest
Copy code
<meta-data
                android:name="androidx.work.impl.WorkManagerInitializer"
                android:value="androidx.startup"
                tools:node="remove" />
injected dependency is also marked as a singleton
Copy code
@Provides
    @Singleton
    fun providesDynamicUrlHitService(
        @Named("simple-retrofit") retrofit: Retrofit
    ): DynamicUrlHitService = retrofit.create(DynamicUrlHitService::class.java)
n
Have your 
Application
 class implement the 
Configuration.Provider
 interface, inject an instance of 
HiltWorkFactory
, and pass it into the 
WorkManager
 configuration as follows:
Copy code
@HiltAndroidApp
class ExampleApplication : Application(), Configuration.Provider {

  @Inject lateinit var workerFactory: HiltWorkerFactory

  override fun getWorkManagerConfiguration() =
      Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .build()
}
b
yep forgot to mention that; that is present as well
good call out tho
n
would like to see your factory class
z
I had this issue before. I was missing a hilt dependency in my build.gradle
This change made it work for me
b
Ah my metadata tag is wrong! Sweet I'll test it out on Monday. Thanks!