Flutter构建APK报错:Kotlin元数据版本不兼容求助
解决Kotlin版本不兼容问题:Module was compiled with an incompatible version of Kotlin
问题分析
你遇到的错误核心是Kotlin元数据版本不匹配:项目中某个模块用Kotlin 1.9.x编译(元数据版本1.9.0),但你的构建环境期望的是1.7.x版本(元数据1.7.1)。结合你的配置来看,问题出在:
- 你使用了过新的Kotlin 2.0.0版本,而Flutter Gradle插件、Firebase依赖暂未完全适配该版本
- Firebase BOM版本(33.0.0)与当前Kotlin版本存在兼容性冲突
解决步骤
1. 调整Kotlin版本到兼容范围
将项目级build.gradle中的Kotlin版本修改为Flutter官方推荐的1.9.20(与错误提示中的元数据1.9.0匹配,同时兼容Gradle 8.4.1):
buildscript { ext.kotlin_version = '1.9.20' // 替换原有的2.0.0 repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.4.1' classpath 'com.google.gms:google-services:4.4.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
2. 更新Firebase BOM版本
将应用级build.gradle中的Firebase BOM升级到兼容Kotlin 1.9.x的版本:
dependencies { implementation platform('com.google.firebase:firebase-bom:33.4.0') // 替换原有的33.0.0 implementation 'com.google.firebase:firebase-analytics' }
3. 清理缓存并重新构建
执行以下命令清理项目缓存,确保依赖重新同步:
flutter clean flutter pub get
之后再尝试flutter build apk或在Android Studio中运行项目。
补充说明
关于Kotlin元数据版本:
- Kotlin的元数据版本与语言版本一一对应,比如1.7.1对应Kotlin 1.7.x,1.9.0对应Kotlin 1.9.x
- 当依赖库的编译版本与项目构建插件版本不匹配时,就会触发这类兼容性错误,统一版本是核心解决思路
如果问题仍存在,可运行flutter doctor检查Flutter版本是否需要升级到最新稳定版,确保Flutter与Gradle、Kotlin版本的整体兼容性。
内容的提问来源于stack exchange,提问作者casey
相关产品推荐
相关产品推荐

