简体   繁体   中英

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. 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'.

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. But I'm unable to find any issue, nor the AS code inspections in AppComponent. The TestAppComponent is not even generated.

I believe, we use completely regular setup of local unit tests for android.

Setup:

/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 {
    ....
}

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.

Questions:

  • Do you know what needs to be changed when updating to dagger 2.21 and above?
  • Do you know how to force dagger/kapt/gradle to say more than (no error message)?

PS: Whatever library version you might think of is the latest. 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. After adding the following to build.gradle the component was generated (originally I only had dagger in the kapt directive):

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. We have defined FB as:

    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. This was ok up to dagger 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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