Flutter项目Kotlin版本不兼容错误求助
Kotlin版本不兼容导致Flutter Release构建失败问题修复指导
问题描述
重新启用搁置2年多的Flutter项目,更新Gradle和Kotlin版本后,执行flutter build apk或flutter run --release构建Release版APK或运行时,出现错误:Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0,但调试和Profile模式可正常运行。
已尝试的解决方案
- 删除Gradle缓存
- 升级至最新Kotlin版本(1.9.23)
- 降级Kotlin版本至1.6.0
- 执行
flutter clean&flutter pub get
项目配置文件
build.gradle
buildscript { ext.kotlin_version = '1.9.0' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.8' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1' } } allprojects { repositories { google() jcenter() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
gradle.properties
#Tue Apr 09 12:00:26 IST 2024 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
错误日志
e: other files/my-app/arcore_flutter_plugin/.transforms/f39fb4032ea66347d24faef5e111f4d0/transformed/out/jars/classes.jar!/META-INF/arcore_flutter_plugin_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0. e: other files/my-app/flutter_email_sender/.transforms/e41ad79431d4394b70406c2bf6b2df42/transformed/out/jars/classes.jar!/META-INF/flutter_email_sender_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0. e: other files/my-app/wakelock/.transforms/1113f73603aa5f492fef390312763387/transformed/out/jars/classes.jar!/META-INF/wakelock_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0. e: other files/my-app/device_info_plus/.transforms/69b198a981f53349112f410cc8d4b737/transformed/out/jars/classes.jar!/META-INF/device_info_plus_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0. e: other files/my-app/hmssdk_flutter/.transforms/328da43348617b35dfa4fe66be05c595/transformed/out/jars/classes.jar!/META-INF/hmssdk_flutter_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/transforms-3/f2b1c7a43f771f864943924aeee52fb0/transformed/jetified-play-services-measurement-api-21.5.0/jars/classes.jar!/META-INF/java.com.google.android.gmscore.integ.client.measurement_api_measurement_api.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-android/1.7.2/55dc9f2e0c18ef8aa98367599a22d434790848b8/kotlinx-coroutines-android-1.7.2.jar!/META-INF/kotlinx-coroutines-android.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/transforms-3/c4ca1b3e7d97b2fb42620d397b1e4003/transformed/jetified-ads-adservices-java-1.0.0-beta05/jars/classes.jar!/META-INF/ads-adservices-java_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/transforms-3/426f3f66e1c58b3bd2e92630e5014b8a/transformed/jetified-ads-adservices-1.0.0-beta05/jars/classes.jar!/META-INF/ads-adservices_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.7.2/27cb5db69aeef59d8cc2a2cc2be4200f26091e72/kotlinx-coroutines-core-jvm-1.7.2.jar!/META-INF/kotlinx-coroutines-core.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-play-services/1.7.2/d8b01823c681c6cfdba6ef76f62d87a2b5d88066/kotlinx-coroutines-play-services-1.7.2.jar!/META-INF/kotlinx-coroutines-play-services.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/transforms-3/4b907693550fdef6661f7bcae153126c/transformed/jetified-core-ktx-1.13.0-alpha01/jars/classes.jar!/META-INF/core-ktx_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/transforms-3/269aef7382a10ec574d3e2a34a3e535b/transformed/core-1.13.0-alpha01/jars/classes.jar!/META-INF/core_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.9.0/8ee15ef0c67dc83d874f412d84378d7f0eb50b63/kotlin-stdlib-1.9.0.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0. e: C:/Users/bhara/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.9.0/8ee15ef0c67dc83d874f412d84378d7f0eb50b63/kotlin-stdlib-1.9.0.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.
修复方案
1. 统一子项目Kotlin版本配置
问题核心是部分Flutter插件使用高版本Kotlin编译,但主项目未强制子项目使用统一版本。修改根目录build.gradle的subprojects块,添加版本强制配置:
subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" project.evaluationDependsOn(':app') // 强制所有Kotlin Android子项目使用统一版本 plugins.withId('kotlin-android') { kotlin { jvmToolchain(11) // 匹配Gradle 7.4推荐的JDK版本 compilerOptions { apiVersion = '1.8' // 选择与AGP 7.3.0兼容的中间版本 languageVersion = '1.8' } } } }
2. 匹配Gradle、AGP与Kotlin版本兼容性
当前AGP 7.3.0兼容Kotlin 1.6.0~1.8.0,若要使用更高版本需升级AGP和Gradle:
- 修改根目录
build.gradle:
buildscript { ext.kotlin_version = '1.8.22' // AGP 7.3.0支持的最高Kotlin版本 repositories { google() mavenCentral() // 替换已停止维护的jcenter } dependencies { classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.4.1' // 升级到兼容版本 classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.9' // 升级到兼容版本 } } allprojects { repositories { google() mavenCentral() } }
- 若要升级到Kotlin 1.9.x,需将AGP升级到7.4.0+,Gradle升级到7.5+,并更新
gradle.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
3. 彻底清理并重新构建
执行以下命令清除所有缓存后重新构建:
flutter clean rm -rf ~/.gradle/caches # Windows用户手动删除C:\Users\<用户名>\.gradle\caches目录 flutter pub get flutter build apk --release
4. 升级Flutter插件
错误日志中提到的插件(如arcore_flutter_plugin、device_info_plus)可能未适配新环境,检查并升级插件:
flutter pub outdated
将所有可更新的插件升级到最新版本。
内容的提问来源于stack exchange,提问作者Naveen
相关产品推荐
相关产品推荐

