我在这里遵循指南: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几乎相同。当匕首为src / main中的Component构建器生成代码时,它们会正确生成。但是,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'
}
我的app / 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
,但是我似乎无法弄清楚如何使它工作。我知道这是绝对可能的。