Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Dagger未为/测试类生成组件_Android_Dagger 2 - Fatal编程技术网

Android Dagger未为/测试类生成组件

Android Dagger未为/测试类生成组件,android,dagger-2,Android,Dagger 2,我在这里遵循指南: 我的app/src/main中有以下设置(省略了注入和@Provides代码): 在app/src/test中: public class TestFlingyApplication extends Application { @Singleton @Component(modules = { TestFlingyModule.class }) public interface TestFlingyComponent extends FlingyCom

我在这里遵循指南:

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

在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中的组件生成器生成代码

我的主要身材。格雷德尔:

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

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.5.1'
}
testAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>'
我的应用程序/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'
}
因此,当我构建时,我得到的是
daggerLingYaApplication\u FlingyComponent
类,而不是
DaggerTestFlingyApplication\u TestFlingyComponent

有趣的是,我注意到如果我换线:

apt 'com.google.dagger:dagger-compiler:2.0.1'
# TO
compile 'com.google.dagger:dagger-compiler:2.0.1'
当我运行
/gradlew compiledBugUnitTestSources
时,我看到了以下内容:

: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
,但我似乎不知道如何让它工作。我知道这是绝对可能的。

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

androidTestApt 'com.google.dagger:dagger-compiler:<version>'
对于JUnit测试:

testAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>'
或者对于JUnit测试:

testApt 'com.google.dagger:dagger-compiler:<version>'
kaptTest 'com.google.dagger:dagger-compiler:<version>'
kaptest'com.google.dagger:dagger编译器:'

查看链接了解更多信息。

只需对上述答案添加一点内容,因为最近有一些更改

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

因此,从现在起,您只需在build.gradle中添加以下内容:

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

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.5.1'
}
testAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>'
这将创建目录build/generated/source/apt/test/作为Java类接收者,最后一部分将触发“AssembledBugUnitTest”任务,该任务将最终在刚刚创建的文件夹中创建这些Dagger2组件

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


为什么Dagger2不能自动完成这项任务我不明白,但是,嘿,我不是专业人士。

对于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启动
测试
androidTest
,则应触发源代码生成

如果您需要手动使用此早期触发器梯度:

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

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

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

添加到上述解决方案并为dagger添加testKapt和androidTestKapt,我遇到的问题是,由于缺少导入,我的模块和组件具有错误的导入

e、 g

而不是

import dagger.Module
import dagger.Provides

希望这有助于

即使在添加了所有gradle依赖项和注释之后,如果它仍然不起作用,那么您需要为此运行assembleAndroidTest gradle脚本。 只需创建一个空的测试用例并运行它。它会帮你完成任务的。

干杯

我从命令行运行了
/gradlew build
,得到了一个缺少的提供方法的信息,Android Studio没有告诉我。

我得到了以下信息:
错误:(55,0)找不到Gradle DSL方法:'testApt()'
我必须将apt升级到1.7+
classpath'com.neenbedankt.gradle.plugins:android apt:1.8'
要使testApt工作,我尝试使用此变体“testAnnotationProcessor”,但未生成该类。您知道这种方法是否有任何问题吗?Jack处理器依赖项不适用于/test目录中的处理器。我换成了,效果很好。因此,如果您需要此功能,请放弃Jack并使用apt。请记住执行测试或运行
assembleAndroidTest
以实际查看依赖项类
DaggerMyComponent
已解决,只需
rebuild
将不会从dagger生成类。此文档()建议不要在单元测试中使用dagger。如果您有很多依赖项,那么它可能指向您的单元测试测试不止一个单元。尽管如此,谷歌并不总是知道最好的(喘息!),这是一个相当广泛的声明。在特定情况下,在单元测试中使用dagger对我们很有效,因此这个问题对我很有效,你错过了结尾
}
我喜欢这种方法,因为它不需要安装更多的android库。因为我在一个单独的库中进行TDD,所以我从android.libraryVariants.allFantastic开始。在为
testApt
的替代方案搜索了数小时后,这就成功了。谢谢@Salt and Pepper最新版本的gradle有什么问题吗?谢谢。我花了两天时间才修好。Kotlin+Dagger 2.19+最后一个毕业生您需要运行一些测试,然后Dagger将生成测试Dagger类(组件)+1我一直认为,
重建
会贯穿整个项目,但显然不会。谢谢,伙计。androidTestAnnotationProcessor-这就是我要找的,谢谢!我推荐这个答案,因为它可以帮助你查看你的依赖关系图或其他依赖关系是否有问题。你救了我一天,兄弟,我在这个问题上陷了很长时间。起初我跳过了这个建议,因为我确信这不可能是问题,但在又花了20分钟调试之后,我实际上去检查了导入。瞧,Android Studio已经自动导入了espre
gradlew <moduledirectory>:compile<Flavor>DebugAndroidTestSources
gradlew <moduledirectory>:compile<Flavor>DebugTestSources
android {

  defaultConfig {
    multiDexEnabled true
    testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
 import android.support.test.espresso.core.deps.dagger.Module
 import android.support.test.espresso.core.deps.dagger.Module
import dagger.Module
import dagger.Provides