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.