WorManeger + Hilt FAILED

  android, android-workmanager, dagger-hilt

I’m trying to do according to the documentation, but the WorkManager crashes even before the doWork method

manifest

    <provider
        android:name="androidx.work.impl.WorkManagerInitializer"
        android:authorities="${applicationId}.workmanager-init"
        tools:node="remove" />

application

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

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

}

worker

@HiltWorker
class UploaderWorker @AssistedInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters,
private val repository: Repository
) :
CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
    Log.d("UploadWorkManager", "Start CoroutineWorker")
    return  Result.success()
}

}

repository

@Singleton
class Repository @Inject constructor(
@ApplicationContext private val appContext: Context,
private val db: AppDb

)

start

val currentWorks = WorkManager.getInstance(context)
        .getWorkInfosByTag(WORK_TAG).get()
    if (currentWorks.isNotEmpty()) {
        currentWorks.forEach {currentWork ->
            Log.d(WORK_TAG, "CurrentWork ${currentWork.id}   ${currentWork.state}")
        }
        WorkManager.getInstance(context).cancelAllWorkByTag(WORK_TAG)
    }

    val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.CONNECTED)
        .build()
    val work = OneTimeWorkRequestBuilder<UploaderWorker>()
        .setConstraints(constraints)
        .addTag(WORK_TAG)
        .setInitialDelay(1, TimeUnit.MINUTES)
        .build()

    WorkManager.getInstance(context).enqueue(work)
    Log.d(WORK_TAG, "Start ${work.id}")

I started work< but when I show status, work FAILED

CurrentWork 6efa6352-be05-4bb7-9c0f-4be5a002b8c3   FAILED

Source: Android Questions

LEAVE A COMMENT