Dagger not generating components for /test class Dagger not generating components for /test class android android

Dagger not generating components for /test class


You need to add following to your build.gradle file for instrumentation test:

androidTestApt 'com.google.dagger:dagger-compiler:<version>'

or for JUnit test:

testApt 'com.google.dagger:dagger-compiler:<version>'

This is required to generate Dagger code for your test components.


EDIT:

If you are using jack tool chain then add followingfor android test:

androidTestAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>'

for JUnit tests:

testAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>'

EDIT:

In case you are using kotlin-kapt for Kotlin code use following:

kaptAndroidTest 'com.google.dagger:dagger-compiler:<version>'

or for JUnit test:

kaptTest 'com.google.dagger:dagger-compiler:<version>'

Check this link for more info.


For Android Studio 3 and dagger 2.13 the already mentioned annotation processors are needed:

testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.13'

But also do not forgot to do this for the instrumented test under androidTest:

androidTestAnnotationProcessor'com.google.dagger:dagger-compiler:2.13'

You might get the impression that this alone does not work, because the DaggerXYZ classes are not generated. After hours I found out that the test source generation is only triggered when the tests are executed. If you start a test or androidTest from Android Studio the source generation should be triggered.

If you need this earlier trigger gradle manually:

gradlew <moduledirectory>:compile<Flavor>DebugAndroidTestSourcesgradlew <moduledirectory>:compile<Flavor>DebugTestSources

Replace Debug if you run a test in a different build type.

Note:

If you are using multiDexEnable = true you might get an error:

Test running failed: Instrumentation run failed due to 'java.lang.IncompatibleClassChangeError'

Use a different runner in this case:

android {  defaultConfig {    multiDexEnabled true    testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"


Just to add a bit to the above answer, since there have been some recent changes.

From Android Gradle plugin version 2.2 and above you will no longer use testApt.

So from now on you need to put only this in the build.gradle:

testAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>'

But more than that, what I came here for, is the following: if you need gradle to generate the DaggerComponent classes for you you will have to do a bit extra work.

Open our build.gradle file and AFTER the android section write this:

android.applicationVariants.all { variant ->    if (variant.buildType.name == "debug") {        def aptOutputDir = new File(buildDir, "generated/source/apt/${variant.unitTestVariant.dirName}")        variant.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)        assembleDebug.finalizedBy('assembleDebugUnitTest')    }}

This will create the directory build/generated/source/apt/test/ as a Java classes recipient and the last part will trigger the "assembleDebugUnitTest" task that will finally create those Dagger2 components in the folder that was just created.

Note that this script is just being triggered for the "debug" variant and takes advantage of that build variant using the "assembleDebug" task. If for some reason you need it in other variants just tweak that a bit.

Why Dagger2 does not do this automatically is beyond me, but hey, I am no pro.