添加 sourceSet 时已经设置了 endPosTable

endPosTable already set when adding sourceSet

我正在使用自定义注释处理器,但遇到了一个新问题。 我正在使用 Gradle (3.1.1),当我将处理器生成的文件路径添加到 sourceSet 时,我遇到了奇怪的问题。

我先执行 gradle clean build,然后再执行 gradle build,然后构建就被销毁了。 令我惊讶的是,我得到的堆栈跟踪非常常见。

An exception has occurred in the compiler (1.8.0_91). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.IllegalStateException: endPosTable already set
    at com.sun.tools.javac.util.DiagnosticSource.setEndPosTable(DiagnosticSource.java:136)
    at com.sun.tools.javac.util.Log.setEndPosTable(Log.java:350)
    at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:667)
    at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:950)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.<init>(JavacProcessingEnvironment.java:892)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.next(JavacProcessingEnvironment.java:921)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1187)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    at com.sun.tools.javac.main.Main.compile(Main.java:523)
    at com.sun.tools.javac.main.Main.compile(Main.java:381)
    at com.sun.tools.javac.main.Main.compile(Main.java:370)
    at com.sun.tools.javac.main.Main.compile(Main.java:361)
    at com.sun.tools.javac.Main.compile(Main.java:56)
    at com.sun.tools.javac.Main.main(Main.java:42)

如果我总是在构建之前清理我没有问题,而且当我不添加源集时构建总是成功的。 我什至尝试在我的注释处理器中创建新生成的文件之前删除该文件,但这也没有成功。

在研究的过程中,我也发现了这个有趣的东西 link: JDK Bug report

但是考虑到我必须总是先清理或者将它从我的 sourceSet 中删除并不是很好。由于 Eclipse 不喜欢找不到按照惯例应该存在的文件。

你对如何解决这个问题有什么建议吗?

由于这是 JDK 8 的官方 Bug,旨在在 JDK 9 中解决,我实施了一个变通方法,如果文件已存在,则删除带有 gradle 的文件.处理器不再有问题,我可以将它保存在我的 sourceSet 中。

你好:)

编辑:解决方法本身。

gradle.ext.generatedQueriesDir = 'build/generated-sources/local/query'
/*
 * This is a workaround to delete the file that will be created by the annotation processor if it already exists.
 * There is a known bug in the Java compiler and JDK 8 which should be fixed at JDK 9.
 * http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8067747 <-- Master report
 * http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8146348 <-- duplicates master report
 */
if ( file( gradle.ext.generatedQueriesDir ).exists() ) {
  FileCollection collection = files { file( gradle.ext.generatedQueriesDir ).listFiles() }
  collection.each { delete it }
}

我们使用 gradle,因此这是 gradle 脚本的解决方法。但基本上解决方法是删除在开始编译和预编译步骤之前将生成的文件。

我正在尝试在我的 Android 应用程序的测试源集中使用生成的 Dagger* 文件。我在@Nico 的解决方法中更改了一些内容,现在一切正常。

/build.gradle:

ext {
    generatedSourcesDir = 'build/generated/source/apt/test/debug'
}

/app/build.gradle

android {
    sourceSets {
        test.java.srcDirs += generatedSourcesDir
    }
}

tasks.withType(JavaCompile) {
   if (file(generatedSourcesDir).exists()) {
       FileCollection collection = files { file(generatedSourcesDir).listFiles() }
       collection.each { delete it }
   }
}

dependencies {
    ...
}