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

Flutter构建失败:Kotlin版本不兼容问题求助

解决Flutter项目中Kotlin版本不兼容问题

问题概述

开发Flutter项目时,构建过程中遭遇Kotlin版本不兼容问题:部分依赖模块使用Kotlin 1.9.0编译,但项目当前环境期望1.7.1版本,同时收到升级Kotlin Gradle插件的提示。

错误详情

e: C:/Users/digit/.gradle/caches/transforms-3/431ce31617111116ed092b20fb775fda/transformed/jetified-kotlin-stdlib-1.9.0.jar!/META-INF/kotlin-stdlib-jdk7.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.
e: C:/Users/digit/.gradle/caches/transforms-3/431ce31617111116ed092b20fb775fda/transformed/jetified-kotlin-stdlib-1.9.0.jar!/META-INF/kotlin-stdlib-jdk8.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.
e: C:/Users/digit/.gradle/caches/transforms-3/431ce31617111116ed092b20fb775fda/transformed/jetified-kotlin-stdlib-1.9.0.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.
e: C:/Users/digit/.gradle/caches/transforms-3/6327b6d5cbee3dd5c434505ea5de9c0a/transformed/jetified-firebase-analytics-ktx-22.0.1-api.jar!/META-INF/java.com.google.android.libraries.firebase.firebase_analytics_ktx_granule.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.
e: C:/Users/digit/.gradle/caches/transforms-3/648640f83817e7438e3934ba83addfe2/transformed/jetified-play-services-measurement-api-22.0.1-api.jar!/META-INF/java.com.google.android.gmscore.integ.client.measurement_api_measurement_api.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.
e: C:/Users/digit/.gradle/caches/transforms-3/c321e7444a5c731da2398570d6ef04f1/transformed/jetified-kotlin-stdlib-common-1.9.0.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.

系统提示

[!] 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 C:\Users\digit\Documents\flutter projects\android\build.gradle:
ext.kotlin_version = '<latest-version>'

当前项目配置

android/build.gradle

buildscript {
    ext.kotlin_version = '1.9.0' // Updated to the latest version
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2' // Updated Gradle Plugin
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.2'
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44'
    }
}

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/app/build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'com.google.gms.google-services'
    id 'com.google.dagger.hilt.android'
    id 'dev.flutter.flutter-gradle-plugin'
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader("UTF-8") { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "1"
}

def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
    flutterVersionName = "1.0"
}

android {
    namespace = "com.muhasebepro.muhasebe_pro_finall"
    compileSdkVersion 34
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.muhasebepro.muhasebe_pro_finall"
        minSdkVersion 24
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source = '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:31.2.3')
    implementation 'com.google.firebase:firebase-analytics-ktx'

    implementation 'com.google.dagger:hilt-android:2.44'
    kapt 'com.google.dagger:hilt-compiler:2.44'
}

apply plugin: 'kotlin-kapt'

已尝试步骤

  • 在android/build.gradle中将Kotlin版本更新为1.9.0
  • 确保所有依赖使用相同Kotlin版本
  • 执行flutter clean和flutter pub get清理并拉取依赖
  • 检查无重复依赖

解决方案

1. 匹配AGP与Kotlin版本兼容性

Android Gradle插件(AGP)和Kotlin Gradle插件存在严格版本匹配要求:

  • AGP 7.4.x的兼容Kotlin版本范围为1.7.0~1.8.20,1.9.0超出兼容范围会导致冲突。
  • 将Kotlin版本降级到1.8.20(AGP 7.4.2的最高兼容版本),修改android/build.gradle:
ext.kotlin_version = '1.8.20'

2. 强制所有依赖使用统一Kotlin版本

在android/build.gradle的allprojects块中添加强制配置,确保第三方依赖(如Firebase、Hilt)对齐指定版本:

allprojects {
    repositories {
        google()
        mavenCentral()
    }
    // 新增版本强制规则
    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
        }
    }
}

3. 彻底清理缓存

仅flutter clean无法清除Gradle缓存中的旧依赖,需手动删除本地Gradle缓存目录:

  • Windows:C:\Users\digit\.gradle\caches
  • 执行项目级清理命令:
flutter clean && cd android && ./gradlew cleanBuildCache

4. 排查依赖树冲突

执行命令查看完整依赖树,确认是否有插件强制引入旧版Kotlin:

cd android && ./gradlew app:dependencies

若发现冲突插件,优先升级插件版本,或在resolutionStrategy中添加对应强制规则。

5. 重新构建项目

完成上述修改后,依次执行:

flutter pub get
cd android && ./gradlew build --refresh-dependencies
flutter run

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 05:34:52