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


    问题

    项目使用了 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)
    
    • 1
    • 2
    • 3
    • 4

    是 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.(KaptContext.kt:29)
    	at org.jetbrains.kotlin.kapt3.base.Kapt.kapt(Kapt.kt:33)
    	... 31 more
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    可见,根本问题是:

    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
    
    • 1

    分析

    先大概介绍一下 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"
    
    • 1
    • 2
    • 3
    • 4

    该问题官方已经有回复了,见: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
    
    • 1

    方案三:指定 kapt 运行在 kotlin deamon 中

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

    gradle.properties 中添加:

    kapt.use.worker.api=false
    
    • 1
  • 相关阅读:
    基于知识蒸馏的两阶段去雨去雪去雾模型学习记录(二)之知识收集阶段
    游戏服务器成DDoS最大攻击重灾区
    【系统架构设计】架构核心知识: 2.6 CBSE、逆向工程、净室软件工程
    第13章: 常用类
    NTP时间同步服务器设置
    @PostConstruct注解详解
    什么 ? 陪玩都月入过忘拉~这不得python采集一下
    运行时内存分配
    【附源码】计算机毕业设计JAVA重工教师职称管理系统
    睿趣科技:抖音开小店大概多久可以做起来
  • 原文地址:https://blog.csdn.net/lovelease/article/details/126868466