简体   繁体   English

Dagger 2.21+ 无法在 android 中生成 UnitTest 组件

[英]Dagger 2.21+ unable to generate UnitTest Component in android

Let's start with an issue:让我们从一个问题开始:

> 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

I'm trying to update our huge project to run with incremental kapt.我正在尝试更新我们的庞大项目以使用增量 kapt 运行。 One of the essentials was to update dagger.必需品之一是更新匕首。 I tried a lot version, but the last one working is 2.20, everything above gives the mentioned 'error'.我尝试了很多版本,但最后一个版本是 2.20,上面的所有内容都给出了提到的“错误”。

To be honest, I don't see any error.老实说,我没有看到任何错误。 The build works fine, when I assemble only app, but when I try to run the UnitTest task, it shows me that error.构建工作正常,当我只组装应用程序时,但是当我尝试运行 UnitTest 任务时,它向我显示该错误。 But I'm unable to find any issue, nor the AS code inspections in AppComponent.但是我找不到任何问题,也找不到 AppComponent 中的 AS 代码检查。 The TestAppComponent is not even generated. TestAppComponent 甚至没有生成。

I believe, we use completely regular setup of local unit tests for android.我相信,我们对 android 使用完全常规的本地单元测试设置。

Setup:设置:

/core/src/main/com/pckg/core/Provisions.kt /core/src/main/com/pckg/core/Provisions.kt

interface Provisions {
    ....
}

/app/src/main/com/pckg/AppComponent.kt /app/src/main/com/pckg/AppComponent.kt

@Component(modules=[....])
@Singleton
interface AppComponent : Provisions {
    ....
}

/app/src/test/com/pckg/TestAppComponent.kt /app/src/test/com/pckg/TestAppComponent.kt

@Component(modules=[....])
@Singleton
interface TestAppComponent : AppComponent {
    ....
}

I also tried to make the Components to be abstract classes instead of interface (because the error says that class extends the interface, but without luck - same issue, just with abstract classes).我还尝试使组件成为抽象类而不是接口(因为错误表明类扩展了接口,但没有运气——同样的问题,只是抽象类)。

Of course I did try to run with --stacktrace, but it's just longer pointless exception.当然,我确实尝试使用 --stacktrace 运行,但这只是更长的无意义异常。

Questions:问题:

  • Do you know what needs to be changed when updating to dagger 2.21 and above?你知道更新到dagger 2.21及以上需要改变什么吗?
  • Do you know how to force dagger/kapt/gradle to say more than (no error message)?你知道如何强制 dagger/kapt/gradle 多说(没有错误信息)?

PS: Whatever library version you might think of is the latest. PS:您可能想到的任何库版本都是最新的。 AGP 3.5.0, Gradle 5.5.1, Kotlin 1.2.50,... AGP 3.5.0、Gradle 5.5.1、Kotlin 1.2.50、...

I remember seeing something like this when I was trying to add the test component to the android tests.我记得当我尝试将测试组件添加到 android 测试时看到过类似的东西。 After adding the following to build.gradle the component was generated (originally I only had dagger in the kapt directive):在将以下内容添加到build.gradle 后,生成了组件(最初我在 kapt 指令中只有 dagger):

kaptAndroidTest "com.google.dagger:dagger-android-processor:$dagger_version"
kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger_version"

If you're doing that for unit tests, adjust accordingly.如果您正在为单元测试这样做,请相应地进行调整。

Note: If you're only seeing it when using incremental processing, then maybe it's some other configuration option you need to enable specifically for the unit test?注意:如果您只在使用增量处理时看到它,那么也许它是您需要专门为单元测试启用的其他一些配置选项?

Turns out, the problem was in missing dependencies in tests.事实证明,问题在于测试中缺少依赖项。 In our case it was due to conflict with FindBugs.在我们的例子中,它是由于与 FindBugs 发生冲突。 We have defined FB as:我们将 FB 定义为:

    compileOnly "com.google.code.findbugs:annotations:3.0.1"
    compileOnly "com.google.code.findbugs:jsr305:3.0.2"

So if we have a class with suppress:因此,如果我们有一个带有抑制的类:

public class JavaClass {
    @Inject
    @SuppressFBWarnings(value = "THIS_CRASHES_DAGGER",
            justification = "Since this annotation is not available in test classpath, dagger will fail.")
    Context mInjectedContext;
}

The @SuppressFBWarnings is not available in tests. @SuppressFBWarnings在测试中不可用。 This was ok up to dagger 2.20.这在匕首 2.20 之前都没有问题。 But every version after is failing on that, because the annotation cannot be resolved.但是之后的每个版本都失败了,因为无法解析注释。 And dagger is trying to read other annotations to report you badly used annotations, etc.并且 dagger 正在尝试读取其他注释以报告您使用不当的注释等。

The fix is easy:修复很简单:

    testCompileOnly "com.google.code.findbugs:annotations:3.0.1"
    testCompileOnly "com.google.code.findbugs:jsr305:3.0.2"

or making it implementation might ensure propagation to the tests also.或使其实现也可能确保传播到测试。

You can find more about this here: https://github.com/google/dagger/issues/1599您可以在此处找到更多相关信息: https : //github.com/google/dagger/issues/1599

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM