我在尝试使用Hilt和WorkManager将作品入队时遇到了一个异常:
Could not instantiate my.example.package.widget.ExampleWorker
java.lang.NoSuchMethodException: my.example.package.widget.ExampleWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:3325)
at java.lang.Class.getDeclaredConstructor(Class.java:3063)
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:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)
14:26:51.737 E Could not create Worker my.example.package.widget.ExampleWorker
下面是我的Worker类:
@HiltWorker
class ExampleWorker @AssistedInject constructor(
@Assisted private val context: Context,
@Assisted workerParameters: WorkerParameters,
private val exampleUseCase: ExampleUseCase
) : CoroutineWorker(context, workerParameters) {
companion object {
private val uniqueWorkName = ExampleWorker::class.java.simpleName
fun enqueue(context: Context) {
val manager = WorkManager.getInstance(context)
val requestBuilder = PeriodicWorkRequestBuilder<ExampleWorker>(
Duration.ofMinutes(15)
)
manager.enqueueUniquePeriodicWork(
uniqueWorkName,
ExistingPeriodicWorkPolicy.UPDATE,
requestBuilder.build()
)
}
}
override suspend fun doWork(): Result {
exampleUseCase.invoke()
return Result.success()
}
}
我的HiltApp:
@HiltAndroidApp
class HiltApp : Application(), Configuration.Provider {
// https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager
@Inject
lateinit var workerFactory: HiltWorkerFactory
override fun getWorkManagerConfiguration(): Configuration =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
}
在AndroidManifest.xml文件中,我按照教程删除了默认的初始化器:https://developer.android.com/guide/background/persistent/configuration/custom-configuration#remove-default
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
我在worker中注入的简化类:
class ExampleUseCase @Inject constructor() {
operator fun invoke() {}
}
以下是我的依赖:
implementation("com.google.dagger:hilt-android:2.48.1")
ksp("com.google.dagger:hilt-android-compiler:2.48.1")
implementation("androidx.hilt:hilt-work:1.0.0")
ksp("androidx.hilt:hilt-compiler:1.0.0")
implementation("androidx.work:work-runtime-ktx:2.8.1")
implementation("androidx.work:work-runtime:2.8.1")
Gradle插件版本:8.1.1,Kotlin版本:1.9.10
我在项目中更新了几个依赖项后遇到了这个异常,例如Gradle Plugin从7.4.1更新到8.1.1,Kotlin从1.8.0更新到1.9.10,Hilt从2.44.2更新到2.48.1,以及compile SDK从33更新到34等等。
我试图降级Hilt,但是,在这样做的时候,我遇到了新的异常,我相信这可能是由于所有这些升级库之间的版本不兼容造成的。
如果我从worker构造函数中删除exampleUseCase
参数,NoSuchMethodException
将被解析。但是,我在doWork()
方法中需要此字段。我尝试将其作为lateinit var
注入,但在调用时它尚未初始化。
我查了很多文件,但还是少了点什么。我将非常感谢任何帮助!
1条答案
按热度按时间kmynzznz1#
我认为这是KSP中的一个bug,请参见this