kapt不兼容JDK 16:KaptContext cannot access class com.sun.tools.javac.util.Contex

原文链接:https://blog.csdn.net/lovelease/article/details/126868466
问题
项目使用了 kotlin 的 kapt 插件添加某个依赖,打包 APK 时报错:

  • What went wrong:
    Execution failed for task ':network:kaptDebugKotlin'.

    A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
    java.lang.reflect.InvocationTargetException (no error message)

是 task:kaptDebugKotlin 失败了,使用 --stacktrace 重新编译打包,得到更详细的错误提示:

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin'.
    ...
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
    ...
Caused by: java.lang.reflect.InvocationTargetException
    ...
    ... 3 more
Caused by: java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x1f74be16) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x1f74be16
    at org.jetbrains.kotlin.kapt3.base.KaptContext.<init>(KaptContext.kt:29)
    at org.jetbrains.kotlin.kapt3.base.Kapt.kapt(Kapt.kt:33)
    ... 31 more

可见,根本问题是:

Caused by: java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x1f74be16) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x1f74be16

分析
先大概介绍一下 KAPT,它是 kotlin 用来支持 Android 的注解处理的一个插件,把java使用 annotationProcessor 添加的依赖改为使用 kapt,比如:

// java
annotationProcessor 'com.squareup.moshi:moshi-kotlin-codegen:1.12.0'
// kotlin
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.12.0"

该问题官方已经有回复了,见:https://youtrack.jetbrains.com/issue/KT-45545/Kapt-is-not-compatible-with-JDK-16 ,这里总结一下:

KAPT 是基于 javac 的 internal API 编译的,但 JDK 16 对 internal API 做了访问限制,因此这可以说是 KAPT 与 JDK 16 的兼容问题。

解决方案
方案一:降低 JDK 至 16 以下

不用多说了,既然是与 16 的兼容问题,降低版本自然能解决。

方案二:指定jvm参数,允许illegal-access(推荐)
gradle.properties 中添加:

org.gradle.jvmargs=--illegal-access=permit

方案三:指定 kapt 运行在 kotlin deamon 中
kapt默认运行在gradle deamon中,可以通过以下配置强制其运行在kotlin deamon中,但可能会使其变慢。

gradle.properties 中添加:

kapt.use.worker.api=false

none
最后修改于:2024年09月19日 22:27

添加新评论