Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work 2.8.0 + Hilt not initializing workers

There's a bunch of questions like this, but I haven't found a solution that helps me.

app/build.gradle

    implementation 'androidx.work:work-runtime-ktx:2.8.0'

    implementation "com.google.dagger:hilt-android:2.44"
    implementation "androidx.hilt:hilt-navigation-compose:1.1.0-alpha01"
    implementation 'androidx.hilt:hilt-work:1.0.0'
    kapt "com.google.dagger:hilt-android-compiler:2.44"

AndroidManifest.xml

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

Application

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

// other stuff

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

One of my workers

@HiltWorker
class Some @AssistedInject constructor(
    @Assisted appContext: Context,
    @Assisted params: WorkerParameters,
    private val someRepository: SomeRepository,
) : CoroutineWorker(appContext, params) {
}

I get work manager instance with WorkManager.getInstance(context)

Worker is not initialized with exception

Could not instantiate some.path.SomeWorker
java.lang.NoSuchMethodException: some.path.SomeWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2363)
at java.lang.Class.getDeclaredConstructor(Class.java:2201)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:243)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:145)
at androidx.work.impl.utils.SerialExecutorImpl$Task.run(SerialExecutorImpl.java:96)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
2023-02-12 22:48:56.505 22235-22316 WM-WorkerWrapper
E  Could not create Worker some.path.SomeWorker

If I leave only context and worker params in the constructor, than it works fine

like image 265
Nazar Dunets Avatar asked Sep 15 '25 18:09

Nazar Dunets


1 Answers

I had the same problem, and after 2 days of investigating the problem was with the initialization of the library OneSignal (One Signal SDK version 4.8.6), so I had to downgrade to 4.8.4 and it worked. For further info

https://github.com/OneSignal/OneSignal-Android-SDK/issues/1748

like image 94
Diego Alarcon Avatar answered Sep 17 '25 10:09

Diego Alarcon