WorkManager + Hilt 失败
Posted
技术标签:
【中文标题】WorkManager + Hilt 失败【英文标题】:WorkManager + Hilt FAILED 【发布时间】:2021-08-28 15:30:45 【问题描述】:我正在关注文档,但 WorkManager 甚至在 doWork 方法之前就崩溃了
清单
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="$applicationId.workmanager-init"
tools:node="remove" />
应用
@HiltAndroidApp
class MonitoringApp : Application(), Configuration.Provider
@Inject
lateinit var workerFactory: HiltWorkerFactory
override fun getWorkManagerConfiguration(): Configuration
return Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
工人
@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()
存储库
@Singleton
class Repository @Inject constructor(
@ApplicationContext private val appContext: Context,
private val db: AppDb
)
开始
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")
我开始工作
CurrentWork 6efa6352-be05-4bb7-9c0f-4be5a002b8c3 FAILED
====更新===== Logcat中显示如下错误
021-06-11 21:37:10.698 15039-19464/XXXX E/WM-WorkerFactory:无法实例化 XXXX.framework.workmanger.UploaderWorker java.lang.NoSuchMethodException:XXXX.framework.workmanger.UploaderWorker。 [class android.content.Context,class androidx.work.WorkerParameters] at java.lang.Class.getConstructor0(Class.java:2332) at java.lang.Class.getDeclaredConstructor(Class.java:2170) at androidx.work。 WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95) 在 androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:244) 在 androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136) 在 androidx.work.impl .utils.SerialExecutor$Task.run(SerialExecutor.java:91) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641 ) 在 java.lang.Thread.run(Thread.java:919) 2021-06-11 21:37:10.699 15039-19464/XXXX E/WM-WorkerWrapper: 无法创建 Worker XXXX.framework.workmanger.UploaderWorker
【问题讨论】:
【参考方案1】:提供者的问题
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="$applicationId.androidx-startup"
tools:node="remove">
</provider>
【讨论】:
以上是关于WorkManager + Hilt 失败的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 工作管理器返回“工作人员结果失败 [id=XXXXxXXXXXXXXXX 标签= be.tramckrijte.workmanager.BackgroundWorker ]”
测试 WorkManager - RUNNING 而不是 ENQUEUED