Can't pass the dao dependency to android workmanager constructor

Viewed 41

I'm facing a problem with work manager. I want to pass a dao to work manager constructor and follow all the implementations but can't access the dao.

java.lang.NoSuchMethodException: com.itmedicus.pdmderma.worker.DermaWorker. [class android.content.Context, class androidx.work.WorkerParameters]

Worker Class

    @HiltWorker
    class DermaWorker @AssistedInject constructor(
    @Assisted appContext: Context,
    @Assisted params: WorkerParameters,
    private val dermaDao: DermaDao
    ): CoroutineWorker ( appContext,params)
     {
    override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
    try {
          DamiData.addDermaList()
           val list = DamiData.dermaList
          for (derma in list) {
            dermaDao.insertDermaContent(derma)
        }
        Result.success()

    } catch (ex: Exception) {
        Result.failure()
    }
  }
}

Application Class

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

    companion object {
    lateinit var appContext: Context
    }

    override fun onCreate() {
    super.onCreate()
    appContext = applicationContext
    plant(Timber.DebugTree())
   }

    override fun getWorkManagerConfiguration(): Configuration {
    return Configuration.Builder().setWorkerFactory(workerFactory)
        .setMinimumLoggingLevel(android.util.Log.DEBUG)
        .build()
  }
}

Manifest

        <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" />
       </provider>

HiltWorkerModule

       @Module
       @InstallIn(SingletonComponent::class)
       object WorkManagerInitializer : Initializer<WorkManager> {

       @Provides
       @Singleton
       override fun create(@ApplicationContext context: Context): WorkManager {
       val configuration = Configuration.Builder().build()
       WorkManager.initialize(context, configuration)
       return WorkManager.getInstance(context)
    }

      override fun dependencies(): List<Class<out Initializer<*>>> {
      // No dependencies on other libraries.
      return emptyList()
    }
  }

Build.gradle

     //Dagger Hilt
     implementation 'com.google.dagger:hilt-android:2.42'
     kapt 'com.google.dagger:hilt-compiler:2.42'
     kapt 'androidx.hilt:hilt-compiler:1.0.0'
     implementation 'androidx.hilt:hilt-work:1.0.0'
     implementation "androidx.startup:startup-runtime:1.1.1"

     // work manager
     implementation "androidx.work:work-runtime-ktx:2.7.1"
2 Answers

Where is your factory class? Like this?

@AssistedFactory
    interface Factory {
        fun create(appContext: Context, params: WorkerParameters, dermaDao: DermaDao): DermaWorker
    }
}

Yes.I solved the issue. I'm trying to init workmanager from a viewmodel.In the viewmodel constructor, I'm passing private val workManager : WorkManager.But in this process, I can't init the workmanager.So I'm keeping the AndroidViewModel for context & manually creating the workmanager dependency. Here is my solving code.

@HiltViewModel
class DermaViewModel @Inject constructor(
private val dermaRepository: DermaRepository,
application
) : AndroidViewModel(context) {

init {
    initWorkManager()
}

private fun initWorkManager() {
    val request: WorkRequest = OneTimeWorkRequestBuilder<DermaWorker>()
        .build()
    val workManager = WorkManager.getInstance(context)
    workManager.enqueue(request)
 }
}
Related