Dagger 2 generated test component not recognized

后端 未结 3 1574
眼角桃花
眼角桃花 2020-12-09 19:37

I\'m hoping that this is just something I\'m doing wrong here. I\'m trying to use Dagger 2.0 to inject dependencies for my JUnit tests (not Espresso tests, just pu

相关标签:
3条回答
  • 2020-12-09 19:59

    I found a workaround, just in case anybody gets stuck with this issue in the future. It appears the testAnnotationProcessor command in the android gradle plugin does not work for test modules (possibly a bug in their implementation?). So you can write testAnnotationProcessor and your build.gradle will compile but it seems to not work properly.

    The workaround is to fall back to the older third-party annotation processing plugin by Hugo Visser (android-apt).

    To do that, add the following to your buildscript dependencies in your main build.gradle:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.3.0-rc1'
    
            // ADD THIS LINE HERE vvv
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        }
    }
    

    Then, in your individual module's build.gradle, add the following line at the top:

    apply plugin: 'com.android.library'
    
    // ADD THIS LINE HERE vvv
    apply plugin: 'com.neenbedankt.android-apt'
    

    Finally, instead of using testAnnotationProcessor and annotationProcessor, just use apt and testApt:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.2.0'
    
        compile 'com.google.dagger:dagger:2.9'
        // USE apt INSTEAD OF annotationProcessor HERE vvv
        apt 'com.google.dagger:dagger-compiler:2.9'
    
        testCompile 'com.google.dagger:dagger:2.9'
        // USE testApt INSTEAD OF testAnnotationProcessor HERE vvv
        testApt 'com.google.dagger:dagger-compiler:2.9'
    
        testCompile 'junit:junit:4.12'
    }
    

    Note that you must use the 1.8 version of android-apt, as the 1.4 version does not ship with the testApt command/function/whatever.

    0 讨论(0)
  • 2020-12-09 20:12

    Add this to build.gradle after android DSL:

    android {
        ...
    }
    
    android.applicationVariants.all {
        def aptOutputDir = new File(buildDir, "generated/source/apt/${it.unitTestVariant.dirName}")
        it.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)
    }
    

    Thereafter, your test component would be recognized.

    For Kotlin replace generated/source/apt/... with generated/source/kapt/...

    There is an issue raised in the tracker concerning this.

    0 讨论(0)
  • 2020-12-09 20:16

    Depending on your test type:

    • insert testAnnotationProcessor "com.google.dagger:dagger-compiler:2.x" inside dependencies on your build.gradle file for src/test

    or

    • insert androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:2.x" inside dependencies on your build.gradle file for src/androidTest
    0 讨论(0)
提交回复
热议问题