匕首2生成的测试组件不被识别

我希望这只是我在这里做错了。 我试图使用Dagger 2.0为我的JUnit测试注入依赖( 不是Espresso测试 ,只是纯JUnit)。 所以,我有一个“主”的Java模块和一个“测试”的Java模块。 在主模块中,我有一个Dagger模块和一个组件:

@Module public class MainModule { @Provides public Widget provideWidget() { return new ConcreteWidget(); } } ... @Component (modules = MainModule.class) public interface MainComponent { void inject(WidgetConsumer consumer); } 

在我的测试模块中,我有以下几点:

 @Module public class TestModule { @Provides public Widget provideWidget() { return new Widget() { @Override public void doThing() { int y = 6; y ++; } }; } } ... @Component(modules = TestModule.class) public interface TestComponent extends MainComponent{ } 

我的build.gradle具有这样的依赖关系:

 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' testCompile 'junit:junit:4.12' compile 'com.google.dagger:dagger:2.9' testCompile 'com.google.dagger:dagger:2.9' annotationProcessor 'com.google.dagger:dagger-compiler:2.9' testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.9' } 

无论出于何种原因,Dagger都会生成DaggerMainComponent ,但拒绝生成DaggerTestComponent 。 当我建立时,似乎在gradle输出中没有错误。

这是事情…我认为注释处理器正在运行,但不知何故,Android gradle插件无法在编译时拉入那些生成的源代码。 我已经检查了app / build / generated / source / apt / test /目录,并在那里找到了DaggerTestComponent.java ,但由于某种原因,它并没有作为依赖项导入。

有什么想法吗? 这是一个测试项目的链接,显示我的问题

android DSL之后添加这个到build.gradle

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

此后,您的测试组件将被识别。

为Kotlin替换generated/source/apt/... with generated/source/kapt/...

跟踪器中提出的这个问题有一个问题 。

我找到了一个解决方法,以防万一有人在将来遇到这个问题。 看来android gradle插件中的testAnnotationProcessor命令不适用于测试模块(可能是它们实现中的一个错误?)。 所以你可以编写testAnnotationProcessor ,你的build.gradle将会编译,但似乎无法正常工作。

解决方法是回到Hugo Visser(android-apt)的旧版第三方批注处理插件。

要做到这一点,请将以下代码添加到build.gradle中的buildscript依赖项中:

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

然后,在你的个人模块的build.gradle,在顶部添加以下行:

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

最后,不要使用testAnnotationProcessorannotationProcessor ,只需使用apttestApt

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

请注意,您必须使用1.8版本的android-apt,因为1.4版本不附带testApt命令/函数/任何内容。