Dagger 2 生成的测试组件无法识别 [英] Dagger 2 generated test component not recognized
问题描述
我希望这只是我在这里做错了.我正在尝试使用 Dagger 2.0 为我的 JUnit 测试(不是 Espresso 测试,只是纯 JUnit)注入依赖项.所以,我有一个主"Java 模块和一个测试"Java 模块.在主模块中,我有一个 Dagger 模块和一个组件:
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 pure JUnit). So, I have a 'main' java module and a 'test' java module. In the main module, I've got a Dagger Module and a Component:
@Module
public class MainModule {
@Provides
public Widget provideWidget() {
return new ConcreteWidget();
}
}
...
@Component (modules = MainModule.class)
public interface MainComponent {
void inject(WidgetConsumer consumer);
}
在我的测试模块中,我有以下内容:
And in my test module, I have the following:
@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 具有如下所示的依赖项:
My build.gradle has dependencies that look like this:
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 输出中似乎没有错误.
For whatever reason, Dagger generates DaggerMainComponent
, but refuses to generate DaggerTestComponent
. There appear to be no errors in the gradle output when I build.
事情就是这样...我认为注释处理器正在运行,但不知何故,android gradle 插件无法在编译时提取那些生成的源代码.我检查了 app/build/generated/source/apt/test/目录并在其中找到了 DaggerTestComponent.java
,但由于某种原因,它没有作为依赖项导入.
Here's the thing... I think the annotation processor is running, but somehow the android gradle plugin is failing to pull in those generated sources during compile-time. I have inspected the app/build/generated/source/apt/test/ directory and found DaggerTestComponent.java
in there, but for some reason, it's not imported as a dependency.
有什么想法吗?这是显示我的问题的测试项目的链接>
推荐答案
在 android
DSL 之后添加这个到 build.gradle
:
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.
对于 Kotlin,将 generated/source/apt/...
替换为 generated/source/kapt/...
For Kotlin replace generated/source/apt/...
with generated/source/kapt/...
跟踪器中存在与此相关的一个问题.
There is an issue raised in the tracker concerning this.
这篇关于Dagger 2 生成的测试组件无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!