15

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

Waqas Tahir
  • 7,171
  • 5
  • 25
  • 47

3 Answers3

31

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>
Waqas Tahir
  • 7,171
  • 5
  • 25
  • 47
xatok
  • 905
  • 5
  • 20
  • 1
    But you are overriding `getWorkManagerConfiguration` from the application (since Hilt needs it), thus you need to add that provider to the manifest. – xatok May 10 '21 at 11:36
  • I did it , It still gives the error ! It says in the docs : Note: Because this customizes the WorkManager configuration, you also must remove the default initializer from the AndroidManifest.xml file as specified in the WorkManager docs. https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager – Waqas Tahir May 10 '21 at 11:45
  • Thanks for trying to help though ! – Waqas Tahir May 10 '21 at 11:47
  • It fix my issues using it with workmanager version `2.7.0-rc01` – Oscar Gallon Oct 01 '21 at 09:56
29

You are missing an extra kapt definition in your gradle file:

// When using Kotlin.
kapt("androidx.hilt:hilt-compiler:1.0.0")

// When using Java.
annotationProcessor("androidx.hilt:hilt-compiler:1.0.0")

see documentation

petin
  • 629
  • 5
  • 13
  • I am using ``kapt "com.google.dagger:hilt-compiler:$hilt_version"`` but no need to worry , The problem was solved with the answer above , check the edits – Waqas Tahir Aug 28 '21 at 09:07
  • 9
    I was writing this answer, as I have faced the same issue yesterday, and having the `kapt "com.google.dagger:hilt-compiler:$hilt_version"` only was not enough. After adding the extra `kapt("androidx.hilt:hilt-compiler:1.0.0")` everything was fine. So my working solution is to have both kapt definitons + the accepted manifest setting – petin Aug 28 '21 at 10:36
  • 1
    Good answer. It's worth mentioning that in a multi module project this should be added to the module `build.gradle` which contains the `Worker` implementations – Ivan Wooll May 20 '22 at 16:18
2

I am using WorkManager 2.7.1 with hilt. According to doc, since WorkManager 2.6, you should remove [androidx.work.WorkManagerInitializer] from start-up provider instead of [androidx.work.impl.WorkManagerInitializer].

    <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        android:exported="false"
        tools:node="merge">
        <!-- If you are using androidx.startup to initialize other components -->
        <meta-data
            android:name="androidx.work.WorkManagerInitializer"
            android:value="androidx.startup"
            tools:node="remove" />
    </provider>
Simsim
  • 121
  • 1
  • 2