【错误记录】Android Studio 编译报错 ( Module was compiled with an incompatible version of Kotlin. ) 2
文章目录
在之前遇到过类似问题 【错误记录】Android Studio 编译报错 ( Module was compiled with an incompatible version of Kotlin. The binary ) , 报错的依赖库不同 , 本篇博客再次分析一遍 ;
一、报错信息
Android Studio 工程在编译时 , 报如下错误 :
e: Incompatible classes were found in dependencies.
Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors
e: C:/Users/octop/.gradle/caches/modules-2/files-2.1/org.jetbrains.
kotlin/kotlin-stdlib-common/1.7.10/bac80c520d0a9e3f3673bc2658c6ed02ef45a76a/
kotlin-stdlib-common-1.7.10.jar!/META-INF/kotlin-stdlib-common.kotlin_module:
Module was compiled with an incompatible version of Kotlin.
The binary version of its metadata is 1.7.1, expected version is 1.5.1.
二、问题分析
直接把报错信息翻译一下 :
e: Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors
e: C:/Users/octop/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.7.10/bac80c520d0a9e3f3673bc2658c6ed02ef45a76a/kotlin-stdlib-common-1.7.10.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1.
报错的模块是 C:/Users/octop/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.7.10/bac80c520d0a9e3f3673bc2658c6ed02ef45a76a/kotlin-stdlib-common-1.7.10.jar
依赖库模块 ;
翻译后 : kotlin-stdlib-common-1.7.10.jar 模块是用不兼容的Kotlin版本编译的。其元数据的二进制版本为1.7.1,预期版本为1.5.1。
根据报错的信息可知 , 问题出在 kotlin-stdlib-common-1.7.10.jar
依赖库上 , 该依赖库配置的 Kotlin 版本是 1.7.1 , 但是本工程中根目录的 build.gradle 顶层构建脚本中 , 配置的 Kotlin 版本是 1.5.0 ;
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.0"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
知道问题原因 , 那么解决方案就很简单了 , 有两种解决方案 :
- 提高 Kotlin 版本到 1.7.10
- 降低 kotlin-stdlib-common-1.7.10.jar 版本 , 使得 Kotlin 1.5.0 版本的可以兼容该依赖库 ;
三、解决方案
方案一 : 提高 Kotlin 版本到 1.7.10
直接将 工程根目录 中 build.gradle 构建脚本的 Kotlin 版本提升到 1.7.10 ;
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.7.10"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
方案二 : 降低 kotlin-stdlib-common 依赖库版本
使用的 kotlin-stdlib-common-1.7.10.jar 依赖库版本太高 , 降低该依赖库版本 ;
首先分析该依赖库是如何导入进来的 , 在模块的构建脚本中 , 没有直接导入该依赖库 ,
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:+'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
执行
gradlew app:dependencies --configuration releaseRuntimeClasspath
命令 , 查询依赖库的详细情况 ;
命令行输出如下 :
PS Y:\002_WorkSpace\001_AS\Coroutine> .\gradlew app:dependencies --configuration releaseRuntimeClasspath
WARNING:: Please remove usages of `jcenter()` Maven repository from your build scripts and migrate your build to other Maven repositories.
This repository is deprecated and it will be shut down in the future.
See http://developer.android.com/r/tools/jcenter-end-of-service for more information.
Currently detected usages in: root project 'Coroutine', project ':app'
> Task :app:dependencies
------------------------------------------------------------
Project :app
------------------------------------------------------------
releaseRuntimeClasspath - Runtime classpath of compilation 'release' (target (androidJvm)).
+--- org.jetbrains.kotlin:kotlin-stdlib:1.5.0 -> 1.7.10
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10
| \--- org.jetbrains:annotations:13.0
+--- androidx.core:core-ktx:+ -> 1.9.0
| +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| +--- androidx.core:core:1.9.0
| | +--- androidx.annotation:annotation:1.2.0 -> 1.3.0
| | +--- androidx.annotation:annotation-experimental:1.3.0
| | | \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 (*)
| | +--- androidx.collection:collection:1.0.0 -> 1.1.0
| | | \--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.concurrent:concurrent-futures:1.0.0
| | | +--- com.google.guava:listenablefuture:1.0
| | | \--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.lifecycle:lifecycle-runtime:2.3.1 -> 2.5.1
| | | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | +--- androidx.arch.core:core-common:2.1.0
| | | | \--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | +--- androidx.arch.core:core-runtime:2.1.0
| | | | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | | \--- androidx.arch.core:core-common:2.1.0 (*)
| | | \--- androidx.lifecycle:lifecycle-common:2.5.1
| | | \--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.versionedparcelable:versionedparcelable:1.1.1
| | | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| | \--- androidx.core:core-ktx:1.9.0 (c)
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 (*)
| \--- androidx.core:core:1.9.0 (c)
+--- androidx.appcompat:appcompat:1.5.1
| +--- androidx.activity:activity:1.5.1
| | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| | +--- androidx.core:core:1.8.0 -> 1.9.0 (*)
| | +--- androidx.lifecycle:lifecycle-runtime:2.5.1 (*)
| | +--- androidx.lifecycle:lifecycle-viewmodel:2.5.1
| | | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.21 -> 1.7.10 (*)
| | | \--- androidx.lifecycle:lifecycle-viewmodel-savedstate:2.5.1 (c)
| | +--- androidx.lifecycle:lifecycle-viewmodel-savedstate:2.5.1
| | | +--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| | | +--- androidx.core:core-ktx:1.2.0 -> 1.9.0 (*)
| | | +--- androidx.lifecycle:lifecycle-livedata-core:2.5.1
| | | | +--- androidx.arch.core:core-common:2.1.0 (*)
| | | | +--- androidx.arch.core:core-runtime:2.1.0 (*)
| | | | \--- androidx.lifecycle:lifecycle-common:2.5.1 (*)
| | | +--- androidx.lifecycle:lifecycle-viewmodel:2.5.1 (*)
| | | +--- androidx.savedstate:savedstate:1.2.0
| | | | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | | +--- androidx.arch.core:core-common:2.1.0 (*)
| | | | +--- androidx.lifecycle:lifecycle-common:2.4.0 -> 2.5.1 (*)
| | | | \--- org.jetbrains.kotlin:kotlin-stdlib:1.6.20 -> 1.7.10 (*)
| | | +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.21 -> 1.7.10 (*)
| | | \--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1
| | | +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1
| | | | \--- org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.1
| | | | +--- org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.1
| | | | | +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1 (c)
| | | | | +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1 (c)
| | | | | \--- org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.1 (c)
| | | | +--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0
| | | | | +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.0 -> 1.7.10 (*)
| | | | | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0
| | | | | \--- org.jetbrains.kotlin:kotlin-stdlib:1.6.0 -> 1.7.10 (*)
| | | | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.6.0 -> 1.7.10
| | | +--- org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.1 (*)
| | | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0 (*)
| | +--- androidx.savedstate:savedstate:1.2.0 (*)
| | +--- androidx.tracing:tracing:1.0.0
| | | \--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | \--- org.jetbrains.kotlin:kotlin-stdlib:1.6.21 -> 1.7.10 (*)
| +--- androidx.annotation:annotation:1.3.0
| +--- androidx.appcompat:appcompat-resources:1.5.1
| | +--- androidx.annotation:annotation:1.2.0 -> 1.3.0
| | +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| | +--- androidx.core:core:1.6.0 -> 1.9.0 (*)
| | +--- androidx.vectordrawable:vectordrawable:1.1.0
| | | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | +--- androidx.core:core:1.1.0 -> 1.9.0 (*)
| | | \--- androidx.collection:collection:1.1.0 (*)
| | +--- androidx.vectordrawable:vectordrawable-animated:1.1.0
| | | +--- androidx.vectordrawable:vectordrawable:1.1.0 (*)
| | | +--- androidx.interpolator:interpolator:1.0.0
| | | | \--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| | | \--- androidx.collection:collection:1.1.0 (*)
| | \--- androidx.appcompat:appcompat:1.5.1 (c)
| +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| +--- androidx.core:core:1.8.0 -> 1.9.0 (*)
| +--- androidx.core:core-ktx:1.8.0 -> 1.9.0 (*)
| +--- androidx.cursoradapter:cursoradapter:1.0.0
| | \--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| +--- androidx.drawerlayout:drawerlayout:1.0.0 -> 1.1.1
| | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.core:core:1.2.0 -> 1.9.0 (*)
| | \--- androidx.customview:customview:1.1.0
| | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.core:core:1.3.0 -> 1.9.0 (*)
| | \--- androidx.collection:collection:1.1.0 (*)
| +--- androidx.emoji2:emoji2:1.2.0
| | +--- androidx.annotation:annotation:1.2.0 -> 1.3.0
| | +--- androidx.collection:collection:1.1.0 (*)
| | +--- androidx.core:core:1.3.0 -> 1.9.0 (*)
| | +--- androidx.lifecycle:lifecycle-process:2.4.1
| | | +--- androidx.lifecycle:lifecycle-runtime:2.4.1 -> 2.5.1 (*)
| | | +--- androidx.startup:startup-runtime:1.1.1
| | | | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | | | \--- androidx.tracing:tracing:1.0.0 (*)
| | | \--- androidx.annotation:annotation:1.2.0 -> 1.3.0
| | \--- androidx.startup:startup-runtime:1.0.0 -> 1.1.1 (*)
| +--- androidx.emoji2:emoji2-views-helper:1.2.0
| | +--- androidx.collection:collection:1.1.0 (*)
| | +--- androidx.core:core:1.3.0 -> 1.9.0 (*)
| | \--- androidx.emoji2:emoji2:1.2.0 (*)
| +--- androidx.fragment:fragment:1.3.6
| | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.core:core:1.2.0 -> 1.9.0 (*)
| | +--- androidx.collection:collection:1.1.0 (*)
| | +--- androidx.viewpager:viewpager:1.0.0
| | | +--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| | | +--- androidx.core:core:1.0.0 -> 1.9.0 (*)
| | | \--- androidx.customview:customview:1.0.0 -> 1.1.0 (*)
| | +--- androidx.loader:loader:1.0.0
| | | +--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| | | +--- androidx.core:core:1.0.0 -> 1.9.0 (*)
| | | +--- androidx.lifecycle:lifecycle-livedata:2.0.0
| | | | +--- androidx.arch.core:core-runtime:2.0.0 -> 2.1.0 (*)
| | | | +--- androidx.lifecycle:lifecycle-livedata-core:2.0.0 -> 2.5.1 (*)
| | | | \--- androidx.arch.core:core-common:2.0.0 -> 2.1.0 (*)
| | | \--- androidx.lifecycle:lifecycle-viewmodel:2.0.0 -> 2.5.1 (*)
| | +--- androidx.activity:activity:1.2.4 -> 1.5.1 (*)
| | +--- androidx.lifecycle:lifecycle-livedata-core:2.3.1 -> 2.5.1 (*)
| | +--- androidx.lifecycle:lifecycle-viewmodel:2.3.1 -> 2.5.1 (*)
| | +--- androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.1 -> 2.5.1 (*)
| | +--- androidx.savedstate:savedstate:1.1.0 -> 1.2.0 (*)
| | \--- androidx.annotation:annotation-experimental:1.0.0 -> 1.3.0 (*)
| +--- androidx.lifecycle:lifecycle-runtime:2.5.1 (*)
| +--- androidx.lifecycle:lifecycle-viewmodel:2.5.1 (*)
| +--- androidx.resourceinspection:resourceinspection-annotation:1.0.1
| | \--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| +--- androidx.savedstate:savedstate:1.2.0 (*)
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 (*)
| \--- androidx.appcompat:appcompat-resources:1.5.1 (c)
+--- com.google.android.material:material:1.6.1
| +--- androidx.annotation:annotation:1.2.0 -> 1.3.0
| +--- androidx.appcompat:appcompat:1.1.0 -> 1.5.1 (*)
| +--- androidx.cardview:cardview:1.0.0
| | \--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| +--- androidx.coordinatorlayout:coordinatorlayout:1.1.0
| | +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| | +--- androidx.core:core:1.1.0 -> 1.9.0 (*)
| | +--- androidx.customview:customview:1.0.0 -> 1.1.0 (*)
| | \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| +--- androidx.constraintlayout:constraintlayout:2.0.1 -> 2.1.4
| | +--- androidx.appcompat:appcompat:1.2.0 -> 1.5.1 (*)
| | +--- androidx.core:core:1.3.2 -> 1.9.0 (*)
| | \--- androidx.constraintlayout:constraintlayout-core:1.0.4
| +--- androidx.core:core:1.5.0 -> 1.9.0 (*)
| +--- androidx.drawerlayout:drawerlayout:1.1.1 (*)
| +--- androidx.dynamicanimation:dynamicanimation:1.0.0
| | +--- androidx.core:core:1.0.0 -> 1.9.0 (*)
| | +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| | \--- androidx.legacy:legacy-support-core-utils:1.0.0
| | +--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| | +--- androidx.core:core:1.0.0 -> 1.9.0 (*)
| | +--- androidx.documentfile:documentfile:1.0.0
| | | \--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| | +--- androidx.loader:loader:1.0.0 (*)
| | +--- androidx.localbroadcastmanager:localbroadcastmanager:1.0.0
| | | \--- androidx.annotation:annotation:1.0.0 -> 1.3.0
| | \--- androidx.print:print:1.0.0
| | \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| +--- androidx.vectordrawable:vectordrawable:1.1.0 (*)
| \--- androidx.viewpager2:viewpager2:1.0.0
| +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
| +--- androidx.fragment:fragment:1.1.0 -> 1.3.6 (*)
| +--- androidx.recyclerview:recyclerview:1.1.0 (*)
| +--- androidx.core:core:1.1.0 -> 1.9.0 (*)
| \--- androidx.collection:collection:1.1.0 (*)
\--- androidx.constraintlayout:constraintlayout:2.1.4 (*)
(c) - dependency constraint
(*) - dependencies omitted (listed previously)
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed
PS Y:\002_WorkSpace\001_AS\Coroutine>
提供一套依赖版本 , 需要统一降低所有的依赖库版本 ;
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
方案二通过降低版本的方案有点复杂 , 需要逐一向上排查依赖的版本 , 可能是上一层依赖版本较高 , 需要将上一层依赖降低版本 , 上一层也可能还有上一层依赖 ;
可以考虑通过 【Android Gradle 插件】Android 依赖管理 ⑥ ( 依赖冲突处理 | transitive 依赖传递设置 | exclude 依赖排除设置 | force 强制指定依赖库 ) 博客中 , 使用 force 强制指定依赖库 , 不管版本是多少 , 必须强制使用 org.jetbrains.kotlin:kotlin-stdlib-common:1.5.0
版本的依赖库 ;
configurations.all {
resolutionStrategy {
force org.jetbrains.kotlin:kotlin-stdlib-common:1.5.0
}
}
相关文章
- android onresume方法,非静态方法’onResume’Android Studio
- android bindservice方法,Android bindservice方法返回false
- android onresume方法,Android onActivityResult()和onResume()的执行顺序
- android 模拟器 haxm,Android模拟器不使用HAXM
- android sdk根目录,Android SDK位置
- android telephony 原理解析与开发指南_Android逆向pdf
- Android n_android 反编译
- 【Android 安装包优化】p7zip 源码交叉编译 Android 平台可执行程序 ( 下载 p7zip 源码 | 交叉编译 Android 中使用 7z 可执行程序 )
- 【Android APT】编译时技术 ( 编译时注解 和 注解处理器 依赖库 )
- 【错误记录】Android Studio 4.2.1 编译报错 ( Kotlin 版本推荐设置 1.5.0 )
- 【错误记录】Android Studio 编译时 Kotlin 代码编译报错 ( Not enough information to infer type variable T )
- 【Android 逆向】x86 CPU 架构体系 ( CPU 模型 | 内存模型 )
- 【广告接入】Android 应用接入有米广告积分墙 ( 注册有米云账号 | 首次登录设置开发者信息 | 添加应用 )
- 【错误记录】编译 Android 版本的 ijkplayer 报错 ( You must define ANDROID_NDK before starting. | 下载指定版本 NDK )
- 【错误记录】Android Studio 编译报错 ( Module was compiled with an incompatible version of Kotlin. The binary )
- 【Kotlin 协程】协程异常处理 ④ ( Android 协程中出现异常导致应用崩溃 | Android 协程中使用协程异常处理器捕获异常 | Android 全局异常处理器 )
- 【错误记录】Android Studio 编译报错 ( To use data binding annotations in Kotlin, apply the ‘kotlin-kapt‘ plu )
- [android] 界面的划分详解手机开发
- Android Studio 导入项目 出现安装Error:Cause: failed to find target with hash string ‘android-23’ 等错误详解手机开发
- Kotlin Android Extensions使用指南详解手机开发
- 解锁极致移动体验:Root你的Android手机吧(linuxroot手机)