【问题标题】:Dagger 2 generated test component not recognizedDagger 2 生成的测试组件无法识别
【发布时间】:2017-03-14 18:10:39
【问题描述】:

我希望这只是我在这里做错了。我正在尝试使用 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,但由于某种原因,它没有作为依赖项导入。

有什么想法吗? Here is a link to a test project showing my issue

【问题讨论】:

  • 如果可能,请发布一个测试项目。
  • 当然可以。编辑了我的回复以添加指向测试项目的链接。

标签: java android junit android-gradle-plugin dagger-2


【解决方案1】:

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/...

跟踪器中有an issue raised与此有关。

【讨论】:

  • 感谢您的解决方法,但我遇到了麻烦。有什么地方可以检查 DSL 语法吗?它告诉我applicationVariants 在android 中不存在。我有一种感觉,他们要么重命名它,要么摆脱它,要么把它移到其他地方。 android.buildTypesandroid.sourceSets 存在,但 android.applicationVariants 似乎不存在(使用 gradle plugin v 2.3.0-rc1)。
  • 我刚刚对您提供的源代码进行了一项更改。我只在build.gradle 中的android {} 之后添加了这三行。看看吧。
  • 是的,我确实试过了(在 android 块之后)。我得到“无法为 com.android.build.gradle.LibraryExtension 类型的对象获取未知属性 'applicationVariants'。”啊!我想我知道我的问题是什么......我在顶部使用apply plugin: 'com.android.library'。抱歉,我在这里发布了一个示例项目,并尝试将此修复程序应用于我的实际项目,这是一个 android 库 (AAR) 项目。我敢打赌图书馆没有变体或其他东西。这对于图书馆项目来说会是怎样的,你知道吗?
  • 没关系,这是libraryVariants。弄清楚了。测试了您的修复程序,它运行良好,谢谢!不能等到谷歌修复他们的插件,我们真的不应该这样做......
  • 在过去的几个小时里,我一直在思考这个问题。终于找到了可行的解决方法。谢谢!
【解决方案2】:

我找到了一种解决方法,以防万一将来有人遇到此问题。看来 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 命令/函数/whatever。

【讨论】:

  • 它似乎不再工作了(使用 Gradle 3.1.0) --> 错误:android-apt 插件与 Android Gradle 插件不兼容。请改用“annotationProcessor”配置。
【解决方案3】:

取决于您的测试类型:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多