[英]Dagger 2 build error when upgrading from Dagger 2.7 to Dagger 2.21 on Android
[英]Dagger 2.21+ unable to generate UnitTest Component in android
讓我們從一個問題開始:
> Task :app:kaptAppDebugUnitTestKotlin FAILED
/app/build/tmp/kapt3/stubs/appDebugUnitTest/com/pckg/TestAppComponent.java:77: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract class TestAppComponent extends com.pckg.AppComponent {
^warning: The following options were not recognized by any processor: '[room.schemaLocation, kapt.kotlin.generated, room.incremental]'[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: android.databinding.annotationprocessor.ProcessDataBinding (DYNAMIC).
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptAppDebugUnitTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
我正在嘗試更新我們的龐大項目以使用增量 kapt 運行。 必需品之一是更新匕首。 我嘗試了很多版本,但最后一個版本是 2.20,上面的所有內容都給出了提到的“錯誤”。
老實說,我沒有看到任何錯誤。 構建工作正常,當我只組裝應用程序時,但是當我嘗試運行 UnitTest 任務時,它向我顯示該錯誤。 但是我找不到任何問題,也找不到 AppComponent 中的 AS 代碼檢查。 TestAppComponent 甚至沒有生成。
我相信,我們對 android 使用完全常規的本地單元測試設置。
設置:
/core/src/main/com/pckg/core/Provisions.kt
interface Provisions {
....
}
/app/src/main/com/pckg/AppComponent.kt
@Component(modules=[....])
@Singleton
interface AppComponent : Provisions {
....
}
/app/src/test/com/pckg/TestAppComponent.kt
@Component(modules=[....])
@Singleton
interface TestAppComponent : AppComponent {
....
}
我還嘗試使組件成為抽象類而不是接口(因為錯誤表明類擴展了接口,但沒有運氣——同樣的問題,只是抽象類)。
當然,我確實嘗試使用 --stacktrace 運行,但這只是更長的無意義異常。
問題:
PS:您可能想到的任何庫版本都是最新的。 AGP 3.5.0、Gradle 5.5.1、Kotlin 1.2.50、...
我記得當我嘗試將測試組件添加到 android 測試時看到過類似的東西。 在將以下內容添加到build.gradle 后,生成了組件(最初我在 kapt 指令中只有 dagger):
kaptAndroidTest "com.google.dagger:dagger-android-processor:$dagger_version"
kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger_version"
如果您正在為單元測試這樣做,請相應地進行調整。
注意:如果您只在使用增量處理時看到它,那么也許它是您需要專門為單元測試啟用的其他一些配置選項?
事實證明,問題在於測試中缺少依賴項。 在我們的例子中,它是由於與 FindBugs 發生沖突。 我們將 FB 定義為:
compileOnly "com.google.code.findbugs:annotations:3.0.1"
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
因此,如果我們有一個帶有抑制的類:
public class JavaClass {
@Inject
@SuppressFBWarnings(value = "THIS_CRASHES_DAGGER",
justification = "Since this annotation is not available in test classpath, dagger will fail.")
Context mInjectedContext;
}
@SuppressFBWarnings
在測試中不可用。 這在匕首 2.20 之前都沒有問題。 但是之后的每個版本都失敗了,因為無法解析注釋。 並且 dagger 正在嘗試讀取其他注釋以報告您使用不當的注釋等。
修復很簡單:
testCompileOnly "com.google.code.findbugs:annotations:3.0.1"
testCompileOnly "com.google.code.findbugs:jsr305:3.0.2"
或使其實現也可能確保傳播到測試。
您可以在此處找到更多相關信息: https : //github.com/google/dagger/issues/1599
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.