Inject a dao into Worker with Hilt problem
Asked Answered
H

1

7

I have initiated a worker in my App class like below;

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

@Inject
lateinit var workerFactory: HiltWorkerFactory

override fun getWorkManagerConfiguration() =
    Configuration.Builder()
        .setMinimumLoggingLevel(android.util.Log.DEBUG)
        .setWorkerFactory(workerFactory)
        .build()

I want to inject a singleton dao into my worker class for update something in DB. So I am injecting dao like below;

 @HiltWorker
 class ClearImageNotInArchiveWorker @AssistedInject constructor(
      @Assisted val appContext: Context,
      @Assisted val workerParams: WorkerParameters,
      private val kitapDao: KitapDao): CoroutineWorker(appContext, workerParams) {

override suspend fun doWork(): Result {...}
}

When I enquee this WorkManager class in App class , I am getting

WorkerFactory: Could not instantiate com.mesutemre.kutuphanem.job.ClearImageNotInArchiveWorker error. But application is not crashing. Is there any problem about injecting dao in here?

Also my singleton dao;

@Singleton
@Provides
fun provideKitapDao(database: KutuphanemDatabase) = database.getKitapDao();
Hollerman answered 12/1, 2022 at 13:6 Comment(2)
Did you disable the default WorkManager initializer?Colorific
I removed it from manifestHollerman
H
9

I solved the problem. I messed up hilt-compiler kapts. I have added

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

into the gradle then I added

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

into AndroidManifest.xml . Then the problem solved.

Hollerman answered 14/1, 2022 at 6:23 Comment(1)
wow! you save me!Zebra

© 2022 - 2024 — McMap. All rights reserved.