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

Flutter房产应用运行报错:需升级Kotlin Gradle插件及build.gradle

解决Flutter项目Kotlin Gradle插件版本过低问题

以下是升级后的app/build.gradle文件内容,同时补充项目根目录build.gradle的修改说明:

升级后的app/build.gradle

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

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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

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

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 34

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

    defaultConfig {
        applicationId "com.example.real_estate"
        minSdkVersion 21 // 建议升级到21,若需兼容更低版本可保留16,但部分新特性无法使用
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

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

项目根目录build.gradle修改

找到项目根目录下的build.gradle文件,更新Kotlin Gradle插件版本,确保和依赖版本一致:

buildscript {
    ext.kotlin_version = '1.9.20'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.2'
        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
}

关键修改说明

  • Kotlin版本升级:指定Kotlin版本为1.9.20,兼容当前Flutter稳定版,同时保证依赖与插件版本一致
  • SDK版本升级:将compileSdkVersion和targetSdkVersion升级到34,适配最新Android平台要求,兼容新版本Kotlin插件
  • Gradle插件版本:根目录build.gradle中使用8.2.2版本的Android Gradle插件,与Kotlin 1.9.20完美匹配

修改完成后,执行flutter clean,重新运行项目即可解决插件版本过低的报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 20:09:23