有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 Dagger 2组件生成的类在Gradle Java插件的compileTestJava中找不到

嗯,我正在迁移我的Android项目以使用Clean Architecure:

https://github.com/安卓10/Android-CleanArchitecture

这意味着我的部分代码在域模块中(纯Java,与Android没有依赖关系)。对于这个项目,我使用Dagger2,它使用注释处理器(在编译时)生成源代码

我的项目有以下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构建我的项目时,我收到编译错误找不到symbol,然后:任务执行失败:域:compileTestJava'

我试图用“compile”和“testCompile”来更改“provided”。它仍然不起作用

奇怪的是,在compileTestJava失败后,我可以找到生成的DaggerTestComponent。域/构建/类/测试中的java。那么,如果它正在生成,为什么我会收到这个编译错误

需要注意的是,这个问题只发生在测试源中。我已经生成了匕首2的主要来源

更新:

我评论了每一个试图使用DaggerTest组件的地方,并试图再次构建。在域/build/classes/test中,现在我不仅可以找到DaggerTest组件。java,而且还有。类是编译的结果。因此,它正在生成源文件并进行编译。为什么使用它编译文件不起作用?这似乎是一些顺序问题,比如生成的源代码在编译其他源代码时还没有准备好


共 (1) 个答案

  1. # 1 楼答案

    多亏了@EpicPandaForce,我开始琢磨是否有一个适合纯Java的插件。搜索后,我找到了这个:

    https://github.com/tbroyer/gradle-apt-plugin

    我只是应用了那个插件,并用apt和testApt改变了我的依赖关系