Flutter项目Gradle中Kotlin版本不兼容问题的解决求助
Kotlin版本不兼容编译错误解决方案
报错详情
META-INF/java.com.google.android.gmscore.integ.client.measurement_api_measurement_api.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.9.0,预期版本为1.7.1。
- 任务
:app:compileDebugKotlin执行失败 - 执行
org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction时发生失败,编译错误
当前根项目build.gradle配置:
buildscript { ext.kotlin_version = '1.9.0' repositories { google() mavenCentral() } 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' } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
已尝试更换Kotlin版本为1.7.10和1.9.22,错误仍存在。
解决步骤
1. 强制统一所有模块的Kotlin版本
项目中子模块可能未同步使用根配置的Kotlin版本,导致版本不一致。在根build.gradle中添加全局配置,确保所有模块对齐版本:
subprojects { afterEvaluate { project -> if (project.plugins.hasPlugin('kotlin-android')) { project.android { kotlinOptions { // 匹配依赖元数据的版本:如果依赖用1.9.0则设为1.9,用1.7.1则设为1.7 apiVersion = '1.9' languageVersion = '1.9' jvmTarget = '1.8' } } } } }
2. 匹配Gradle、Kotlin与Google服务插件的兼容性
Gradle插件版本和Kotlin版本存在严格兼容关系:
- Gradle 7.3.0建议搭配Kotlin 1.6.x~1.8.x,若使用Kotlin 1.9+,需升级Gradle插件到7.4及以上:
同时更新classpath 'com.android.tools.build:gradle:7.4.2'gradle/wrapper/gradle-wrapper.properties中的Gradle版本:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip - 升级Google服务插件到兼容版本,避免版本冲突:
classpath 'com.google.gms:google-services:4.4.2' // 或更高稳定版
3. 清理缓存并重新构建
旧缓存可能残留版本冲突的文件,执行以下命令彻底清理:
./gradlew clean build --refresh-dependencies
4. 排查依赖冲突
找出引入不兼容measurement_api模块的依赖,执行依赖分析命令:
./gradlew app:dependencies
定位到包含该模块的依赖后,可选择升级该依赖到兼容Kotlin版本,或通过exclude排除冲突模块(谨慎操作,避免功能缺失)。
内容的提问来源于stack exchange,提问作者Karthikn
相关产品推荐
相关产品推荐

