java.lang.UnsupportedClassVersionError:com/example/MyClass 由较新版本的 Java 运行时编译(类文件版本 61.0),此 Java 运行时版本仅识别最高 55.0 的类文件版本
java.lang.UnsupportedClassVersionError: com/example/MyClass has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java runtime only recognizes class file versions up to 55.0
ID: android/kotlin-unsupported-class-version
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Android Gradle Plugin 7.4.0 | active | — | — | — |
| AGP 8.0.0 | active | — | — | — |
| AGP 8.1.0 | active | — | — | — |
| AGP 8.2.0 | active | — | — | — |
| Kotlin 1.8.0 | active | — | — | — |
| Kotlin 1.9.0 | active | — | — | — |
| Kotlin 2.0.0 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
根因分析
依赖项或项目模块使用比 compileSdk 或 JVM 目标支持的更高的 Java 字节码版本编译,导致运行时或构建时版本不匹配。
English
A dependency or project module is compiled with a higher Java bytecode version than the compileSdk or JVM target supports, causing a version mismatch at runtime or build time.
官方文档
https://developer.android.com/studio/build/gradle-tips#configure-jdk解决方案
-
Set the Java bytecode version in build.gradle (Module: app): android { compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "11" } } Ensure the JDK used by Gradle is at least JDK 17 for class file version 61.0. Check with java -version in terminal. -
If a dependency is compiled with Java 17 bytecode, update the Gradle JDK in Android Studio: File > Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK > select JDK 17 or higher. Then rebuild.
-
Exclude the offending dependency and use a compatible version. For example, if a library requires bytecode 61.0, find an older version that targets 55.0: implementation('com.example:library:1.0.0') { exclude group: 'com.example' } Then add implementation 'com.example:library:0.9.0'.
无效尝试
常见但无效的做法:
-
Set compileSdk to a higher value like 34 to force the build to accept newer bytecode
95% 失败
compileSdk controls API level, not bytecode version; it does not affect class file version compatibility.
-
Add multiDexEnabled true to build.gradle
99% 失败
MultiDex handles method count limits, not bytecode version issues.
-
Downgrade the Kotlin version to 1.6.0 to use older bytecode
80% 失败
This may break newer Kotlin features and does not fix the dependency that is already compiled with a higher version.