匕首2.21+;无法在android中生成UnitTest组件
让我们从一个问题开始:匕首2.21+;无法在android中生成UnitTest组件,android,unit-testing,kotlin,dagger-2,android-testing,Android,Unit Testing,Kotlin,Dagger 2,Android Testing,让我们从一个问题开始: > 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 cl
> 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 {
....
}
@Component(modules=[....])
@Singleton
interface AppComponent : Provisions {
....
}
@Component(modules=[....])
@Singleton
interface TestAppComponent : AppComponent {
....
}
/app/src/main/com/pckg/AppComponent.kt
interface Provisions {
....
}
@Component(modules=[....])
@Singleton
interface AppComponent : Provisions {
....
}
@Component(modules=[....])
@Singleton
interface TestAppComponent : AppComponent {
....
}
/app/src/test/com/pckg/TestAppComponent.kt
interface Provisions {
....
}
@Component(modules=[....])
@Singleton
interface AppComponent : Provisions {
....
}
@Component(modules=[....])
@Singleton
interface TestAppComponent : AppComponent {
....
}
我还试图使组件成为抽象类而不是接口(因为错误表明该类扩展了接口,但运气不好——同样的问题,只是抽象类)
当然,我尝试过使用--stacktrace运行,但它只是一个更长的无意义异常
问题:
- 你知道更新到dagger 2.21和 在上面
- 你知道如何强迫dagger/kapt/gradle说更多的话吗 (没有错误消息)
附言:你可能想到的任何图书馆版本都是最新的。AGP 3.5.0,Gradle 5.5.1,Kotlin 1.2.50,…我记得在尝试将测试组件添加到android测试时看到类似的情况。将以下内容添加到build.gradle后,生成了组件(最初我在kapt指令中只有dagger): 如果您在单元测试中这样做,请相应地进行调整
注意:如果您只在使用增量处理时看到它,那么可能是您需要专门为单元测试启用的其他配置选项?我记得在尝试将测试组件添加到android测试时看到类似的情况。将以下内容添加到build.gradle后,生成了组件(最初我在kapt指令中只有dagger): 如果您在单元测试中这样做,请相应地进行调整
注意:如果您只在使用增量处理时看到它,那么可能是您需要专门为单元测试启用的其他配置选项?结果表明,问题在于测试中缺少依赖项。 在我们的案例中,这是由于与FindBugs的冲突。 我们将FB定义为:
compileOnly "com.google.code.findbugs:annotations:3.0.1"
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
因此,如果我们有一个具有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;
}
@SuppressFBWarnings
在测试中不可用。这是确定的匕首2.20。但之后的每个版本都失败了,因为无法解析注释。dagger试图阅读其他注释来报告您使用的注释不好,等等
解决方法很简单:
testCompileOnly "com.google.code.findbugs:annotations:3.0.1"
testCompileOnly "com.google.code.findbugs:jsr305:3.0.2"
或者让它实现可能也会确保传播到测试
您可以在这里找到更多信息:事实证明,问题在于测试中缺少依赖项。 在我们的案例中,这是由于与FindBugs的冲突。 我们将FB定义为:
compileOnly "com.google.code.findbugs:annotations:3.0.1"
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
因此,如果我们有一个具有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;
}
@SuppressFBWarnings
在测试中不可用。这是确定的匕首2.20。但之后的每个版本都失败了,因为无法解析注释。dagger试图阅读其他注释来报告您使用的注释不好,等等
解决方法很简单:
testCompileOnly "com.google.code.findbugs:annotations:3.0.1"
testCompileOnly "com.google.code.findbugs:jsr305:3.0.2"
或者让它实现可能也会确保传播到测试
您可以在这里找到更多信息:我为kaptTest定义了这些测试(不是AndroidTest,因为它们是本地单元测试,不是仪器测试)。我也尝试只升级dagger,没有任何增量操作,但问题仍然是一样的。我认为我们的代码中会有一些错误。我已经为kaptTest(不是AndroidTest,因为它们是本地单元测试,不是仪器测试)定义了这些测试。我也尝试只升级dagger,没有任何增量操作,但问题仍然是一样的。我认为我们的代码中会有一些错误。只是不知道如何识别它。