• 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
  • 相关阅读:
    Linux下线程池(ThreadPool)
    基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
    嵌入式面试经典30问
    cmake 基本使用过程
    JS中的闭包
    List基本使用(C++)
    【高效办公】一、ubuntu之间共享桌面
    从JSON1链中学习处理JACKSON链的不稳定性3
    vue2计算属性
    排序算法,思想+C语言代码
  • 原文地址:https://blog.csdn.net/lovelease/article/details/126868466