Dagger 2.21+ 无法在 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,上面的所有内容都给出了提到的 'error'.

老实说,我没有看到任何错误。构建工作正常,当我 assemble 只有应用程序时,但是当我尝试 运行 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 {
    ....
}

我还尝试使组件成为抽象的 classes 而不是接口(因为错误说 class 扩展了接口,但没有运气 - 同样的问题,只是抽象 classes).

当然,我确实尝试 运行 使用 --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"

所以如果我们有一个 class 抑制:

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 在测试中不可用。这在 dagger 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