gpt4 book ai didi

android - 使用 Hilt : Could not instantiate woker 注入(inject) CoroutineWorker

转载 作者:行者123 更新时间:2023-12-04 23:38:38 31 4
gpt4 key购买 nike

有人问了同样的问题,但它们对我不起作用,一开始我使用的是最新版本的工作管理器,它是 2.7 alpha 3,但我降级了,因为它只兼容 android 12 preview sdk,错误仍然存​​在!
它无法实例化 worker ,因为这些依赖项包含在 worker 的构造函数中,在我添加它们之前它正在工作,但如果我不能添加它们,那么刀柄没有任何好处,所以情况如下:
Manifest 不包含任何与工作管理器相关的配置!
应用类:

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

@Inject
lateinit var workerFactory: HiltWorkerFactory

override fun getWorkManagerConfiguration() =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()

}
worker :
@HiltWorker
class DriveSyncWorker @AssistedInject constructor(
@Assisted val context: Context,
@Assisted workerParams: WorkerParameters,
val dependency: StorageHelper,
)
注意:它没有使用 HiltWorkFactory !
错误:
E/WM-WorkerFactory: Could not instantiate com.wakaztahir.timeline.utils.workers.DriveSyncWorker
java.lang.NoSuchMethodException: com.wakaztahir.timeline.utils.workers.DriveSyncWorker.<init> [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)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:244)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
E/WM-WorkerWrapper: Could not create Worker com.wakaztahir.timeline.utils.workers.DriveSyncWorker
Gradle :
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

def work_version = "2.6.0-alpha02"
implementation "androidx.work:work-runtime-ktx:$work_version"
implementation 'androidx.hilt:hilt-work:1.0.0-beta01'
implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
链接:
Cannot Inject coroutine worker using hilt
Injecting coroutine worker using hilt

最佳答案

由于您使用高于 2.6.0-alpha01 的工作管理器版本,因此高于 2.6.0-alpha01 的工作管理器版本使用启动初始化程序 Read Here
将此提供程序添加到您的应用程序 AndroidManifest.xml :

<application ... >

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

</application>

关于android - 使用 Hilt : Could not instantiate woker 注入(inject) CoroutineWorker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67388068/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com