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

Flutter项目依赖更新后Kotlin Gradle插件版本不兼容报错

问题:升级依赖后Flutter项目编译报错,提示Kotlin Gradle插件版本不兼容

报错信息

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...

FAILURE: Build failed with an exception.

  • What went wrong:
    The Android Gradle plugin supports only Kotlin Gradle plugin version 1.5.20 and higher.
    The following dependencies do not satisfy the required version:
    project ':wakelock' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.

  • Get more help at Gradle官方帮助文档

BUILD FAILED in 2s
Error: Gradle task assembleDebug failed with exit code 1

Exited (1).

项目配置文件

android/app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-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.nitoza.app.kmp.warga"
    compileSdk flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion
    // if (project.android.hasProperty("namespace")) {
    //      namespace("change.this.to.your.namespace")
    //  }
    // ndkVersion "25.1.8937393"
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID
        applicationId "com.nitoza.app.kmp.warga"
        // You can update the following values to match your application needs.
        minSdkVersion 21   
        // minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.23"
}

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/gradle.properties

org.gradle.jvmargs=-Xmx4G
android.useAndroidX=true
android.enableJetifier=true

android/gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-all.zip

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 "1.7.10" apply false
}

include ":app"

Flutter环境信息

  • Flutter 3.19.6 • 稳定版频道
  • Framework • 修订版本54e66469a9(13天前)• 2024-04-17 13:08:03 -0700
  • Engine • 修订版本c4cd48e186
  • Tools • Dart 3.3.4 • DevTools 2.31.1

升级依赖前同一版本Flutter运行正常,升级后出现上述错误。


解决方案

方案1:更新wakelock依赖到最新版本

报错核心是wakelock插件依赖的Kotlin Gradle插件版本过低,直接在pubspec.yaml中升级wakelock版本,执行命令:

flutter pub upgrade wakelock

新版本插件通常会适配更高版本的Kotlin和Gradle,解决版本不兼容问题。

方案2:强制统一所有子项目的Kotlin Gradle插件版本

如果wakelock无更新版本或更新后仍报错,可在android/build.gradle中添加配置,强制所有子项目使用与主项目一致的Kotlin版本:

subprojects {
    project.evaluationDependsOn(':app')
    // 添加以下配置
    plugins.withId('kotlin-android') {
        kotlin {
            kotlinVersion = '1.7.10' // 与settings.gradle中声明的版本保持一致
        }
    }
}

方案3:升级Android Gradle插件与Kotlin版本至匹配的更高版本

当前Android Gradle插件版本为7.3.0,官方推荐匹配Kotlin 1.8.20版本,修改android/settings.gradle:

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.8.20" apply false // 更新此处版本
}

同时同步修改android/app/build.gradle中的kotlin-stdlib版本:

dependencies {
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.20"
}

后续操作

修改配置后执行以下命令清理缓存并重新构建:

flutter clean
flutter pub get
flutter run

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:54:53