使用 Java 版本 11 或更高版本构建时,kaptDebugKotlin 崩溃

kaptDebugKotlin crashes when building with Java Version 11 or higher

在我的项目 (https://github.com/guger/MoneyBook) 上尝试 运行 命令 ./gradlew :data:test 时收到 IllegalStateException/NoClassDefFoundError。当我使用高于 11 的 Java 版本时,确实会发生此错误。我无法使用捆绑到 IDE 中的 jdk,因为 robolectric 需要 Java 9 或更高版本,在针对 Android Q 和 Java 9 进行测试时不起作用,因为它以 robolectric.[=14 无法识别的格式提供其版本字符串=]

java.lang.IllegalStateException: failed to analyze: java.lang.NoClassDefFoundError: com/sun/tools/javac/file/BaseFileObject
        at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:56)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:182)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:164)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:54)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:84)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:42)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:104)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1558)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
        at java.rmi/sun.rmi.transport.Transport.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:689)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=11=](TCPTransport.java:677)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:835) Caused by: java.lang.NoClassDefFoundError: com/sun/tools/javac/file/BaseFileObject
        at org.jetbrains.kotlin.kapt3.base.javac.KaptJavaFileManager.shouldBeFiltered(KaptJavaFileManager.kt:69)
        at org.jetbrains.kotlin.kapt3.base.javac.KaptJavaFileManager.list(KaptJavaFileManager.kt:49)
        at jdk.compiler/com.sun.tools.javac.code.ClassFinder.list(ClassFinder.java:737)
        at jdk.compiler/com.sun.tools.javac.code.ClassFinder.scanUserPaths(ClassFinder.java:674)
        at jdk.compiler/com.sun.tools.javac.code.ClassFinder.fillIn(ClassFinder.java:554)
        at jdk.compiler/com.sun.tools.javac.code.ClassFinder.complete(ClassFinder.java:299)
        at jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:642)
        at jdk.compiler/com.sun.tools.javac.code.Symbol$PackageSymbol.members(Symbol.java:1131)
        at jdk.compiler/com.sun.tools.javac.code.Symtab.listPackageModules(Symtab.java:834)
        at jdk.compiler/com.sun.tools.javac.comp.Enter.visitTopLevel(Enter.java:345)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:535)
        at jdk.compiler/com.sun.tools.javac.comp.Enter.classEnter(Enter.java:286)
        at jdk.compiler/com.sun.tools.javac.comp.Enter.classEnter(Enter.java:301)
        at jdk.compiler/com.sun.tools.javac.comp.Enter.complete(Enter.java:577)
        at jdk.compiler/com.sun.tools.javac.comp.Enter.main(Enter.java:561)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:1066)
        at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:66)
        at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:35)
        at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:230)
        at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:188)
        at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:99)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration.invoke(TopDownAnalyzerFacadeForJVM.kt:96)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:106)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:81)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze.invoke(KotlinToJVMBytecodeCompiler.kt:555)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze.invoke(KotlinToJVMBytecodeCompiler.kt:82)
        at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:546)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:177)
        ... 23 more Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.file.BaseFileObject
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:436)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 52 more

在 Java 2013 年 11 月 12 日 运行 宁 Android 项目时,有人遇到过类似的行为吗?

我最近遇到了关于 JDK9 的类似问题。 我使用这个库解决了问题:

"com.github.pengrad:jdk9-deps:1.0"

不幸的是,这是由于 Gradle 5.x 中缺少对 JDK 13 的支持。 Gradle 6.0 将支持 Java 13 (https://docs.gradle.org/6.0/release-notes.html).

在那之前,除了等待别无选择。