5

I am using android app startup, hilt and room for my project. I am getting an error when trying to enqueue work:

com.test E/WM-WorkerFactory: Could not instantiate com.test.RefreshWorker
    java.lang.NoSuchMethodException: com.test.RefreshWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters] 

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <meta-data
        android:name="androidx.work.WorkManagerInitializer"
        android:value="androidx.startup"
        tools:node="remove" />
    <meta-data
        android:name="com.test.RefreshInitializer"
        android:value="androidx.startup" />
</provider>

My Application class:

@HiltAndroidApp
class TestApplication: Application(), Configuration.Provider {
   @Inject
   lateinit var workerFactory: HiltWorkerFactory

   override fun getWorkManagerConfiguration(): Configuration {
      return Configuration.Builder()
         .setWorkerFactory(workerFactory)
         .build()
   }
}

My worker:

@HiltWorker
class RefreshWorker @AssistedInject constructor(
   @Assisted context: Context,
   @Assisted params: WorkerParameters,
   private val repository: Repository,
) : CoroutineWorker(context, params) {
   override suspend fun doWork(): Result = try {
      repository.fetch()
      Result.success()
   } catch (error: Throwable) {
      Result.failure()
   }
}

And lastly my dependencies for work and hilt: def hilt_version = '2.41' def work_version = '2.7.1' 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' annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0' implementation "androidx.work:work-runtime-ktx:$work_version"

If I don't inject my own dependency everything is fine. And I have double checked my dependencies. Any idea what I am missing?

EDIT:

If it helps I noticed that getWorkManagerConfiguration() is not called in my application class. To verify my manifest is correctly using the right class I do see onCreate() called in my application class. Debugging into the WorkManager init code, it still looks like WorkManager is going through android startup to initialize work manager. I have checked my manifest a couple times now and I believe it is correctly removing work manager from app startup.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262

1 Answers1

1

SOLUTION:

check your gradle: Injecting CoroutineWorker with Hilt : Could not instantiate woker

check it here: https://github.com/google/dagger/issues/2601 https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration

Add in gradle exactly this one:

    kapt("androidx.hilt:hilt-compiler:1.0.0")

previously I had the same without () but spaces instead.

in manifest

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

application

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

    @Inject
    lateinit var workerFactory: HiltWorkerFactory

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

my gradle versions:

    hilt_version = "2.37"
    hilt_deps = [
            classpath_plugin: "com.google.dagger:hilt-android-gradle-plugin:$hilt_version",
            hilt            : "com.google.dagger:hilt-android:$hilt_version",
            compiler        : "com.google.dagger:hilt-android-compiler:$hilt_version"
    ]


    kapt hilt_deps.compiler
    kapt("androidx.hilt:hilt-compiler:1.0.0")
    implementation hilt_deps.hilt
    implementation 'androidx.hilt:hilt-work:1.0.0'
    lifecycle_version = "2.4.0-alpha02"
    lifecycle_deps = [
            viewmodel: "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version",
            lifecycle: "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version",
            compiler : "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    ]

Check if you have @Inject constructor() with class you want to provide.

Takeshi567
  • 31
  • 5