Android 办理 “Module was compiled with an incompatible version of Kot ...

打印 上一主题 下一主题

主题 1014|帖子 1014|积分 3042

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
办理 “Module was compiled with an incompatible version of Kotlin” 问题

在Android开发中,偶然我们会遇到Kotlin版本不兼容的问题。具体来说,你大概会看到如下错误:
  1. D:/.gradle/caches/transforms-3/caf5371a15e0d6ffc362b4a5ece9cd49/transformed/jetified-kotlin-stdlib-jdk8-1.7.10.jar!/META-INF/kotlin-stdlib-jdk8.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.
复制代码
这意味着你的项目使用的Kotlin标准库版本与编译器期望的版本不匹配。本文将具体介绍怎样办理这个问题。
问题分析

错误信息指出模块 kotlin-stdlib-jdk8-1.7.10.jar 的元数据版本是 1.7.1,而编译器期望的版本是 1.5.1。这通常是由于项目中使用了不同版本的Kotlin库导致的。要办理这个问题,我们须要确保项目中的所有Kotlin依赖项版本一致。
办理方案

1. 确定Kotlin版本

首先,确定你要使用的Kotlin版本。假设我们使用的是Kotlin 1.5.31。
2. 配置根目次 build.gradle

在项目的根目次 build.gradle 文件中,设置Kotlin版本,并使用 resolutionStrategy 逼迫所有库使用雷同的Kotlin版本。
  1. buildscript {
  2.     ext.kotlin_version = "1.5.31" // 设置你希望使用的Kotlin版本
  3.     repositories {
  4.         google()
  5.         mavenCentral()
  6.     }
  7.     dependencies {
  8.         classpath "com.android.tools.build:gradle:7.0.2" // Android插件
  9.         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Kotlin插件
  10.     }
  11. }
  12. allprojects {
  13.     repositories {
  14.         google()
  15.         mavenCentral()
  16.     }
  17.     configurations.all {
  18.         resolutionStrategy {
  19.             // 强制所有库使用相同的Kotlin版本
  20.             force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  21.             force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  22.             force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  23.         }
  24.     }
  25. }
复制代码
3. 配置应用模块 build.gradle

确保每个模块的 build.gradle 文件中引用雷同的Kotlin版本,并统一所有Kotlin相关的依赖项。
  1. apply plugin: 'com.android.application' // Android应用插件
  2. apply plugin: 'kotlin-android' // Kotlin插件
  3. android {
  4.     compileSdkVersion 31 // 或其他版本
  5.     defaultConfig {
  6.         applicationId "com.example.myapp"
  7.         minSdkVersion 21
  8.         targetSdkVersion 31
  9.         versionCode 1
  10.         versionName "1.0"
  11.     }
  12.     buildTypes {
  13.         release {
  14.             minifyEnabled false
  15.             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  16.         }
  17.     }
  18. }
  19. dependencies {
  20.     implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  21.     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  22.     // 其他依赖项
  23. }
复制代码
4. 清理并重建项目

实验以下命令清理并重建项目,以确保所有更改生效:
  1. ./gradlew clean
  2. ./gradlew build
复制代码
办理不同Kotlin版本需求的现实情况

在现实项目中,第三方库大概须要不同版本的Kotlin,这会导致版本辩论。以下是怎样处理这种情况的具体步骤。
1. 确认所有依赖的Kotlin版本

查看项目中所有依赖的Kotlin版本,确保它们使用的版本一致。可以在build.gradle中查抄:
  1. dependencies {
  2.     implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.31"
  3.     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31"
  4.     // 其他依赖项
  5. }
复制代码
2. 使用 resolutionStrategy 逼迫版本一致

在根目次 build.gradle 文件中使用 resolutionStrategy 逼迫所有依赖项使用雷同的Kotlin版本:
  1. allprojects {
  2.     configurations.all {
  3.         resolutionStrategy {
  4.             force "org.jetbrains.kotlin:kotlin-stdlib:1.5.31"
  5.             force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.31"
  6.             force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31"
  7.         }
  8.     }
  9. }
复制代码
3. 清理并重建项目

实验以下命令清理并重建项目:
  1. ./gradlew clean
  2. ./gradlew build
复制代码
示例完备 build.gradle 文件

根目次 build.gradle

  1. buildscript {
  2.     ext.kotlin_version = "1.5.31" // 你期望的Kotlin版本
  3.     repositories {
  4.         google()
  5.         mavenCentral()
  6.     }
  7.     dependencies {
  8.         classpath "com.android.tools.build:gradle:7.0.2" // Android插件
  9.         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Kotlin插件
  10.     }
  11. }
  12. allprojects {
  13.     repositories {
  14.         google()
  15.         mavenCentral()
  16.     }
  17.     configurations.all {
  18.         resolutionStrategy {
  19.             force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  20.             force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  21.             force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  22.         }
  23.     }
  24. }
复制代码
应用模块 build.gradle

  1. apply plugin: 'com.android.application' // Android应用插件
  2. apply plugin: 'kotlin-android' // Kotlin插件
  3. android {
  4.     compileSdkVersion 31 // 或其他版本
  5.     defaultConfig {
  6.         applicationId "com.example.myapp"
  7.         minSdkVersion 21
  8.         targetSdkVersion 31
  9.         versionCode 1
  10.         versionName "1.0"
  11.     }
  12.     buildTypes {
  13.         release {
  14.             minifyEnabled false
  15.             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  16.         }
  17.     }
  18. }
  19. dependencies {
  20.     implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  21.     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  22.     // 其他依赖项
  23. }
复制代码
结论

通过统一项目中的Kotlin版本,使用resolutionStrategy逼迫所有依赖项使用雷同的Kotlin版本,并清理重建项目,可以办理Kotlin版本不兼容的问题。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

民工心事

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表