Flutter项目已升级Kotlin仍报错:元数据版本不匹配求助
解决Flutter项目Kotlin元数据版本不匹配问题
问题重现
已将Kotlin升级至1.9.23,Gradle使用8.6版本,但运行项目时仍报错:
The binary version of its metadata is 1.9.0, expected version is 1.7.1.
Flutter Fix ─────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin │
│ Gradle plugin.
解决方案
1. 同步模块级build.gradle的Kotlin版本
打开android/app/build.gradle,确保Kotlin依赖版本与根build.gradle定义的版本一致:
plugins { id 'com.android.application' id 'kotlin-android' id 'dev.flutter.flutter-gradle-plugin' } dependencies { // 引用根项目定义的kotlin_version,或直接写死'1.9.23' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // 其他依赖配置... }
2. 强制统一所有依赖的Kotlin版本
在根android/build.gradle的allprojects块中添加版本强制策略,避免第三方依赖引入旧版Kotlin造成冲突:
allprojects { repositories { google() mavenCentral() } configurations.all { resolutionStrategy { force "org.jetbrains.kotlin:kotlin-stdlib:1.9.23" force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.23" force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.23" force "org.jetbrains.kotlin:kotlin-reflect:1.9.23" } } }
3. 修复Android Gradle Plugin(AGP)版本兼容性
Kotlin 1.9.23需要搭配AGP 8.0+版本,在根android/build.gradle中添加兼容的AGP依赖:
buildscript { ext.kotlin_version = '1.9.23' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.2.2' // 与Gradle 8.6兼容 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.4.1' } }
4. 清理缓存并重新构建
执行以下命令彻底清理缓存后重新构建项目:
flutter clean # Linux/macOS清理Gradle缓存 rm -rf ~/.gradle/caches # Windows清理Gradle缓存:rd /s /q %USERPROFILE%\.gradle\caches flutter pub get cd android && ./gradlew clean build --refresh-dependencies # Linux/macOS # Windows执行:cd android && gradlew clean build --refresh-dependencies
内容的提问来源于stack exchange,提问作者Harsh Prajapati
相关产品推荐
相关产品推荐

