【问题标题】:Dagger not generating components for /test classDagger 没有为 /test 类生成组件
【发布时间】:2016-03-26 03:57:12
【问题描述】:

我在这里遵循指南:https://github.com/ecgreb/dagger-2-testing-demo

我的 app/src/main 中有以下设置(省略了注入和 @Provides 代码):

public class FlingyApplication extends Application {
    @Singleton
    @Component(modules = { FlingyModule.class })
    public interface FlingyComponent
}

@Module
public class FlingyModule

在 app/src/test 中:

public class TestFlingyApplication extends Application {
    @Singleton
    @Component(modules = { TestFlingyModule.class })
    public interface TestFlingyComponent extends FlingyComponent
}

@Module
public class TestFlingyModule

到目前为止,它几乎与示例 github 相同。当 dagger 去为 src/main 中的组件构建器生成代码时,它们会正确生成。但是,Dagger 不会为 src/test 中的组件构建器生成代码。

我的主要 build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0-alpha3'

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.5.1'
}

我的应用程序/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'


android {
    # There is obviously more in here, but this is the custom part:
    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

dependencies {
    compile 'com.squareup:otto:1.3.8'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton:butterknife:7.0.1'

    compile 'com.google.dagger:dagger:2.0.1'
    apt 'com.google.dagger:dagger-compiler:2.0.1'
    compile 'javax.annotation:javax.annotation-api:1.2'

    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'io.reactivex:rxjava:1.1.0'

    testCompile 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'org.mockito:mockito-core:1.10.19'
}

所以当我构建时,我得到了DaggerFlingyApplication_FlingyComponent 类,但没有得到DaggerTestFlingyApplication_TestFlingyComponent

我注意到的一件有趣的事情是,如果我换行:

apt 'com.google.dagger:dagger-compiler:2.0.1'
# TO
compile 'com.google.dagger:dagger-compiler:2.0.1'

我在运行./gradlew compileDebugUnitTestSources 时看到以下内容:

:app:compileDebugJavaWithJavac
Note: /app/build/generated/source/apt/debug/com/jy/flingy/DaggerFlingyApplication_FlingyComponent.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:compileDebugUnitTestJavaWithJavac
Note: /app/build/intermediates/classes/test/debug/com/jy/flingy/DaggerTestFlingyApplication_TestFlingyComponent.java uses unchecked or unsafe operations.

我不知道为什么它会构建为中间体,我假设我需要 build.gradle 文件来使用 apt 而不是 compile,但我似乎无法弄清楚如何让它工作.我知道这绝对有可能。

【问题讨论】:

  • 本文档 (google.github.io/dagger/testing.html) 建议不要使用 dagger 进行单元测试..
  • 如果您有很多依赖项,那么它可能指向您的单元测试测试多个单元。也就是说,谷歌并不总是最了解(喘气!),这是他们的一个相当广泛的声明。在特定情况下在单元测试中使用匕首对我们来说效果很好,因此这个问题。

标签: android dagger-2


【解决方案1】:

您需要将以下内容添加到您的 build.gradle 文件以进行仪器测试:

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

或用于 JUnit 测试:

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

这是为您的测试组件生成 Dagger 代码所必需的。


编辑:

如果您使用的是jack 工具链,请添加以下内容 安卓测试:

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

对于 JUnit 测试:

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

编辑:

如果您将 kotlin-kapt 用于 Kotlin 代码,请使用以下代码:

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

或用于 JUnit 测试:

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

查看this链接了解更多信息。

【讨论】:

  • 我得到以下信息:Error:(55, 0) Gradle DSL method not found: 'testApt()'
  • 我必须将 apt 升级到 1.7+...classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 才能使 testApt 工作
  • 我尝试使用这个变体“testAnnotationProcessor”,但没有生成该类。你知道这种方法有什么问题吗?
  • Jack 处理器依赖项不与 /test 目录中的处理器一起工作。我切换到apt,它工作正常。因此,如果您需要此功能,请放弃 Jack 并使用 apt。
  • 记得执行测试或运行assembleAndroidTest 以实际看到依赖类DaggerMyComponent 已解决,只是rebuild 不会从dagger 生成类。
【解决方案2】:

对于 Android Studio 3 和 dagger 2.13,需要已经提到的注解处理器:

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

但也不要忘记为androidTest 下的仪器测试执行此操作:

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

您可能会觉得仅此一项是行不通的,因为没有生成 DaggerXYZ 类。几个小时后,我发现只有在执行测试时才会触发测试源生成。如果您从 Android Studio 启动 test 或 androidTest,则应触发源代码生成。

如果你需要这个更早的手动触发 gradle:

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

如果您在不同的构建类型中运行测试,请替换 Debug。

注意:

如果你使用 multiDexEnable = true 你可能会得到一个错误:

测试运行失败:检测运行失败,原因是 'java.lang.IncompatibleClassChangeError'

在这种情况下使用不同的跑步者:

android {

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

【讨论】:

  • 是的,您需要运行一些测试,然后 Dagger 将生成测试 dagger 类(组件)+1
  • 我一直认为rebuild 会贯穿整个项目,但显然不是。谢谢大佬。
  • androidTestAnnotationProcessor - 这就是我要找的,谢谢!
【解决方案3】:

只是在上面的答案中添加一点,因为最近发生了一些变化。

从 Android Gradle 插件版本 2.2 及更高版本开始,您将不再使用 testApt。

所以从现在开始你只需要把这个放在 build.gradle 中:

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

但更重要的是,我来这里的目的是:如果您需要 gradle 为您生成 DaggerComponent 类,您将不得不做一些额外的工作。

打开我们的 build.gradle 文件并在 android 部分之后写下这个:

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

这将创建目录 build/generated/source/apt/test/ 作为 Java 类接收者,最后一部分将触发“assembleDebugUnitTest”任务,最终将在刚刚创建的文件夹中创建那些 Dagger2 组件。

请注意,此脚本只是为“调试”变体触发,并使用“assembleDebug”任务利用该构建变体。如果由于某种原因您在其他变体中需要它,只需稍微调整一下即可。

为什么 Dagger2 不自动执行此操作我无法理解,但是,嘿,我不是专业人士。

【讨论】:

  • 为我工作,你错过了结局}
  • 我喜欢这种方法,因为它不需要安装更多的 android 库。因为我在一个单独的库中做 TDD,所以我从 android.libraryVariants.all 开始
  • 太棒了。在搜索了几个小时以寻找testApt 的替代品之后,这成功了。谢谢!
  • @saltandpepper 您在使用最新版本的 gradle 时遇到什么问题?
  • 谢谢。我花了 2 天时间来修复它。 Kotlin + Dagger 2.19 + 最后一个 gradle
【解决方案4】:

如果您为 dagger 依赖项添加了 kaptAndroidTest,但在重新构建项目时仍未获得测试组件,请尝试运行 assembleAndroidTest。

【讨论】:

  • 我推荐这个答案,因为它可以帮助您查看依赖关系图是否有问题或依赖关系之外的其他内容
  • 你拯救了我的一天兄弟,我被这个问题困扰了很长时间。
【解决方案5】:

添加到上述解决方案并为 dagger 添加 testKapt 和 androidTestKapt,我的模块和组件由于缺少导入而导致导入错误

例如

 import android.support.test.espresso.core.deps.dagger.Module
 import android.support.test.espresso.core.deps.dagger.Module

而不是

import dagger.Module
import dagger.Provides

希望对你有帮助

【讨论】:

  • 起初我跳过了这个建议,因为我确信这不可能是问题,但在又花了 20 分钟调试后,我实际上去检查了导入。瞧,Android Studio 出于某种原因自动导入了 espresso 版本..
  • 解决了大脑袋问题
【解决方案6】:

您好,即使添加了所有 gradle 依赖项和注释,如果它仍然不起作用,那么您需要为此运行 assembleAndroidTest gradle 脚本。 只需制作一个空的测试用例并运行它。它会为你完成这项工作。 干杯

【讨论】:

  • 天啊!我通过“构建”选项浪费了很多时间。但实际上是通过运行甚至空测试创建的匕首文件。
【解决方案7】:

如果您使用的是 kotlin,请使用“kaptAndroidTest”在 build.gradle 文件中为 android 测试生成 dagger 组件。

【讨论】:

    【解决方案8】:

    我从命令行运行 ./gradlew build 并获得了有关 Android Studio 没有告诉我的缺少的提供方法的信息。

    【讨论】:

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