JDK17 与 ButterKinife 冲突问题: class butterknife.compiler.ButterKnifeProcessor$RScanner

开发环境:

Android Studio Giraffe | 2022.3.1 Patch 1

Gradle插件:classpath 'com.android.tools.build:gradle:7.2.0'

Gradle版本:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip

    Android Studio 升级到最新版后,带来一系列的更新(JDK、Gradle等),导致项目 Build 过程中错误频频,好不容易看到 Build Success 成功的字样,连接上真机设备安装过程中又报错了:

Cause: superclass access check failed: class butterknife.compiler.ButterKnifeProcessor$RScanner (in unnamed module @0x274412b0) cannot access class com.sun.tools.javac.tree.TreeScanner (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.tree to unnamed module @0x274412b0

    网上搜索了下,应该是 jdk17 与 ButterKnife 不兼容导致的,解决的办法如下:

方法一:降低JDK的版本

    在 Android Studio 的 setting - Build,Execution,Deployment - Build Tools - Gradle 可以选择下载 jdk 11 或 jdk 15, 下载成功后,重新 Build 项目即可。

如果在 Android studio 下载失败,可以自己手动到官网去下载安装,然后指定安装目录即可:

Android Studio -> File -> Project Structure -> SDK Location -> Click on Gradle Settings (blue hightlighted text) -> Select the jdk 11 with 11.0 something version name from list

方法二:编辑项目的 gradle.properties 文件

org.gradle.jvmargs=-Xmx1920M \
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

    这是在 stackoverflow 搜索找到的解决方案,解释如下:

Some of the sun libraries needs to be made visible for the newer Java compilers. See this answer for more info. I added the following to our gradle.properties file and it fixed the problem. (We use Butterknife and Realm, and needed the below three packages added. (You might get away without "javac.code" for just Butterknife))

参考:How can I fix this error with ButterKnife in Android Studio? - Stack Overflow

猜你喜欢

转载自blog.csdn.net/crazestone0614/article/details/133529529