Search code examples
androidandroid-workmanagerdagger-hilt

Injecting CoroutineWorker with Hilt : Could not instantiate woker


Same questions have been asked but they didn't work for me , In the start I was using the latest version of work manager which is 2.7 alpha 3 but I downgraded since its only compatible to android 12 preview sdk , The error still remained there !
It cannot instantiate the worker because those dependencies are included in the constructor of the worker , It was working before I added them but there's no benefit of hilt if I can't add them so here's the situation :

Manifest does not contain any configuration related to work manager !

Application Class :

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

@Inject
lateinit var workerFactory: HiltWorkerFactory

override fun getWorkManagerConfiguration() =
    Configuration.Builder()
        .setWorkerFactory(workerFactory)
        .build()

}

Worker :

@HiltWorker
class DriveSyncWorker @AssistedInject constructor(
    @Assisted val context: Context,
    @Assisted workerParams: WorkerParameters,
    val dependency: StorageHelper,
)

Noticed : its not using HiltWorkFactory !

Error:

E/WM-WorkerFactory: Could not instantiate com.wakaztahir.timeline.utils.workers.DriveSyncWorker
    java.lang.NoSuchMethodException: com.wakaztahir.timeline.utils.workers.DriveSyncWorker.<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.DriveSyncWorker

Gradle :

implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

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

Linked :

Cannot Inject coroutine worker using hilt
Injecting coroutine worker using hilt


Solution

  • Since you are using work manager version above than 2.6.0-alpha01 , Work Manager version above 2.6.0-alpha01 uses startup initializer Read Here

    Add this provider to you app AndroidManifest.xml:

    <application ... >
    
     <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove">
     </provider>
    
    </application>