繁体   English   中英

Dagger 2 生成的测试组件无法识别

[英]Dagger 2 generated test component not recognized

我希望这只是我在这里做错的事情。 我正在尝试使用 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/...替换为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命令/功能/任何东西。

根据您的测试类型:

  • src/test build.gradle 文件的依赖项中插入testAnnotationProcessor "com.google.dagger:dagger-compiler:2.x"

或者

  • src/androidTest build.gradle 文件的依赖项中插入androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:2.x"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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