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

Flutter项目执行flutter run时Kotlin版本不兼容编译报错求助

解决Flutter项目Kotlin版本不兼容编译错误

问题根源

项目中Kotlin模块以1.9.0版本编译,但Gradle插件预期的元数据版本为1.7.1,版本不匹配导致:app:compileDebugKotlin任务执行失败。

解决步骤

1. 更新项目根目录的build.gradle

你当前的根目录build.gradle缺少Kotlin Gradle插件的版本配置,需添加buildscript块指定兼容的最新Kotlin稳定版本(建议与Flutter官方推荐版本匹配,如1.9.20)。修改后的文件如下:

buildscript {
    ext.kotlin_version = '1.9.20' // 可替换为最新稳定版
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.4.2" // 确保Gradle版本与Kotlin版本兼容
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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
}

2. 同步app模块的Kotlin配置

打开android/app/build.gradle文件,确保顶部插件引用与根目录配置一致:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'dev.flutter.flutter-gradle-plugin'
}

无需手动指定版本,会自动继承根目录的kotlin_version配置。

3. 清除缓存并重新构建

执行以下命令清除旧缓存,避免版本残留:

flutter clean
flutter pub get
cd android
./gradlew clean
cd ..
flutter run

若仍报错,可尝试更新Flutter到最新稳定版:

flutter upgrade

内容的提问来源于stack exchange,提问作者M Sarmad Qadeer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 12:50:22