好吧,我正在迁移我的 Android 项目以使用清洁架构:
https://github.com/android10/Android-CleanArchitecture
这意味着我的部分代码在域模块中(纯 Java,不依赖于 Android)。对于这个项目,我使用的是 Dagger 2,它使用注释处理器(在编译时)生成源代码。
我的项目有以下 Gradle 配置:
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
configurations {
provided
}
sourceSets {
main {
compileClasspath += configurations.provided
runtimeClasspath += configurations.provided
}
test {
compileClasspath += configurations.provided
runtimeClasspath += configurations.provided
}
}
dependencies {
def domainDependencies = rootProject.ext.domainDependencies
def domainTestDependencies = rootProject.ext.domainTestDependencies
provided domainDependencies.daggerCompiler
provided domainDependencies.javaxAnnotation
compile domainDependencies.dagger
compile domainDependencies.rxJava
compile domainDependencies.joda
testCompile domainTestDependencies.junit
testCompile domainTestDependencies.assertJ
testCompile domainTestDependencies.mockito
testCompile domainTestDependencies.jMockLegacy
testCompile domainTestDependencies.commonsCsv
}
在我的测试源中,我创建了接口 TestComponent 并且 Dagger 应该生成 DaggerTestComponent。当我尝试通过命令行或 Android Studio 构建我的项目时,我收到了找不到符号的编译错误,然后:Execution failed for task ':domain:compileTestJava'。
我试图用'compile'和'testCompile'来改变'provided'。它仍然无法正常工作。
奇怪的是,在 compileTestJava 失败后,我可以在domain/build/classes/test中找到生成的 DaggerTestComponent.java 。那么,如果它正在生成,为什么我会收到这个编译错误?
需要注意的是,这个问题只发生在测试源中。我已经生成了主要源代码中使用的 Dagger 2 的源代码。
更新:
我评论了每个尝试使用 DaggerTestComponent 并尝试再次构建的地方。在domain/build/classes/test中,现在我不仅可以找到 DaggerTestComponent.java ,还可以找到 .class 编译结果。因此,它正在生成源文件并对其进行编译。为什么使用它编译文件不起作用?这似乎是一些顺序问题,比如在编译其他源时生成的源还没有准备好。