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

Flutter构建失败求助:Gradle中Kotlin版本不匹配问题

Flutter项目Kotlin版本不兼容构建错误解决方案

我在开发Flutter项目(Flutter版本:Channel stable, 3.24.0,运行环境:Windows 10 19045.2486),更新部分依赖后执行flutter build apk命令时构建失败,报错信息如下:

e: C:/Users/Moham/.gradle/caches/transforms-3/1a36c150372f3edd1303b1fe7aa45a27/transformed/fragment-1.7.1-api.jar!/META-INF/fragment_release.kotlin_module: 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.
Running Gradle task 'assembleRelease'...                          251.8s

┌─ Flutter Fix ──────────────────────────────────────────────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin Gradle plugin.                                 │
│ Find the latest version on https://kotlinlang.org/docs/releases.html#release-details, then update the  │
│ version number of the plugin with id "org.jetbrains.kotlin.android" in the plugins block of            │
│ C:\flutter_app\lklk-live\android\settings.gradle.                                                      │
│                                                                                                        │
│ Alternatively (if your project was created before Flutter 3.19), update                                │
│ C:\flutter_app\lklk-live\android\build.gradle                                                          │
│ ext.kotlin_version = '<latest-version>'                                                                │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘

我的android/build.gradle配置:

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
}

我的android/settings.gradle配置:

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "2.0.20" apply false
}

include ":app"

我已经在settings.gradle中将Kotlin Gradle插件版本设置为最新的2.0.20,但仍然收到版本不兼容的报错,不知道该如何解决这个问题。


解决方案

核心问题

报错显示fragment-1.7.1-api.jar是用Kotlin 1.8编译的,但当前Gradle环境期望1.6版本。你遇到的问题本质是Android Gradle插件(AGP)版本与Kotlin Gradle插件版本不匹配——你用的AGP 7.3.0仅支持最高Kotlin 1.7.x版本,而你直接升级到了Kotlin 2.0.20,导致版本兼容冲突。

具体修复步骤

  • 方案一:降级Kotlin插件到AGP兼容版本
    修改android/settings.gradle中的Kotlin插件版本,将2.0.20改为与AGP 7.3.0匹配的1.7.20:

    plugins {
        id "dev.flutter.flutter-plugin-loader" version "1.0.0"
        id "com.android.application" version "7.3.0" apply false
        id "org.jetbrains.kotlin.android" version "1.7.20" apply false
    }
    
  • 方案二:升级AGP到支持Kotlin 2.0的版本
    若想保留Kotlin 2.0.20,需要将AGP版本升级到8.2及以上(AGP 8.x支持Kotlin 1.8+),修改android/settings.gradle:

    plugins {
        id "dev.flutter.flutter-plugin-loader" version "1.0.0"
        id "com.android.application" version "8.2.2" apply false
        id "org.jetbrains.kotlin.android" version "2.0.20" apply false
    }
    

    注意:AGP 8.x需要Gradle 7.5及以上版本,若你的Gradle版本不足,需同步更新android/gradle/wrapper/gradle-wrapper.properties中的distributionUrl,例如:

    distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip
    
  • 清理Gradle缓存
    执行命令清理旧缓存,避免残留影响:
    Windows系统:

    gradlew clean
    gradlew build --refresh-dependencies
    
  • 同步依赖与验证
    执行flutter pub upgrade更新所有Flutter依赖,然后重新运行flutter build apk验证是否修复。


内容的提问来源于stack exchange,提问作者Mohamad Adib Tawil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:40:23