![](/img/trans.png)
[英]Android WorkManager Worker can not be injected using Dagger Hilt `@WorkerInject`
[英]My Android app builds failed Hilt, WorkManager
我正在嘗試按照參考從 Dagger2 遷移到 Hilt: https : //developer.android.com/training/dependency-injection/hilt-jetpack#workmanager
但是當我構建我的應用程序時,它失敗並顯示以下錯誤日志:
/Users/.../myapp/build/tmp/kapt3/stubs/devDebug/.../MyWorker.java:13: error: incompatible types: NonExistentClass cannot be converted to Annotation
@error.NonExistentClass()
^
這是第 13 行:
line 11 @error.NonExistentClass()
line 12 public MyWorker(@org.jetbrains.annotations.NotNull()
line 13 @error.NonExistentClass()
line 14 android.content.Context appContext, @org.jetbrains.annotations.NotNull()
line 15 @error.NonExistentClass()
line 16 androidx.work.WorkerParameters params, @org.jetbrains.annotations.NotNull()
我不知道我該如何修復它...
我使用的版本是:
// kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.5.21"
// Hilt
implementation "com.google.dagger:hilt-android:2.35"
kapt "com.google.dagger:hilt-android-compiler:2.35"
implementation 'androidx.hilt:hilt-work:1.0.0'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
// WorkManager
implementation "androidx.work:work-runtime-ktx:2.5.0"
implementation "androidx.work:work-rxjava2:2.5.0"
這些版本是最新的穩定版本。
我嘗試添加以下 Gradle 設置,但它是相同的錯誤...
kapt {
correctErrorTypes true
}
嗚嗚嗚嗚……
我找到了原因...
對不起大家...
我是韓國人,所以一直在看這個韓文版的指導文檔: https : //developer.android.com/training/dependency-injection/hilt-jetpack
但我發現它太舊了!
它指導使用@WorkerInject
在韓國版本,但它指導使用@AssistedInject
在英語版本...
當我更改它時,錯誤消失了......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.