You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code中Flutter项目Kotlin版本不兼容问题求助

解决Flutter构建APK时Kotlin版本不兼容问题

针对你遇到的「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」错误,按以下步骤排查解决:

1. 统一Kotlin版本与Gradle插件版本

修改项目中android/build.gradle(Android根目录下的配置文件),明确指定Kotlin版本并匹配对应的Gradle插件版本:

buildscript {
    // 指定Kotlin版本为1.8.0,和报错的元数据版本对齐
    ext.kotlin_version = '1.8.0'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        // Gradle插件版本需与Kotlin版本匹配,Kotlin 1.8.0对应Gradle插件7.4+
        classpath "com.android.tools.build:gradle:7.4.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

同时确保android/app/build.gradle中引用的Kotlin依赖使用统一版本:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    // 其他依赖...
}

2. 同步Gradle wrapper版本

Kotlin版本与Gradle版本存在对应关系,修改android/gradle/wrapper/gradle-wrapper.properties中的Gradle下载地址,匹配Kotlin 1.8.0要求的Gradle版本(7.3-8.0均可):

distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip

3. 彻底清理缓存后重建

仅执行flutter clean可能无法清除Android端的深层缓存,执行以下命令彻底清理后重新构建:

flutter clean
rm -rf android/.gradle android/build android/app/build
flutter pub get
flutter build apk

4. 处理Flutter SDK内置依赖冲突

如果新建默认项目也报错,说明是Flutter SDK自带的Android依赖与本地Kotlin版本不兼容:

  • 优先升级Flutter SDK到最新稳定版:
    flutter upgrade
    
  • 若不想升级SDK,可在android/build.gradle中添加强制版本统一规则,覆盖所有依赖的Kotlin版本:
    subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'org.jetbrains.kotlin') {
                    details.useVersion '1.8.0'
                }
            }
        }
    }
    

内容的提问来源于stack exchange,提问作者rusty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 22:52:34