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

Flutter项目运行Gradle报错:com/android/build/api/dsl/ApplicationExtension

Flutter项目Gradle报错:com/android/build/api/dsl/ApplicationExtension 解决方法

问题背景

接手维护一个依赖库过时的Flutter项目,在Android模拟器运行时触发Gradle构建失败,报错信息如下:

FAILURE: Build failed with an exception.

* What went wrong:
com/android/build/api/dsl/ApplicationExtension
> com.android.build.api.dsl/ApplicationExtension

* 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 https://help.gradle.org.

BUILD FAILED in 796ms
Running Gradle task 'assembleRelease'...                         1,443ms
Gradle task assembleRelease failed with exit code 1

已确认当前使用的版本组合(Gradle 8.5、Kotlin 1.9.20、AGP 8.2.2)官方兼容,尝试过./gradlew clean、调整多组版本、flutter doctor -v检测无异常等操作,问题仍未解决。

当前项目配置:

android/build.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}"
    
    // REMOVE this line to avoid premature evaluation issues
    // project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

android/app/build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.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 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') ?: '1'
def flutterVersionName = localProperties.getProperty('flutter.versionName') ?: '1.0'

android {
    namespace 'com.ubc.kegcheckubc'
    compileSdk 34
    ndkVersion flutter.ndkVersion

    defaultConfig {
        applicationId "com.ubc.kegcheckubc"
        minSdk 21
        targetSdk flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

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

    buildTypes {
        release {
            signingConfig signingConfigs.debug // Replace with your real signing config if available
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

解决方案

1. 彻底清理构建缓存

执行以下操作清除所有可能的缓存冲突:

  • 运行项目根目录下的命令:./gradlew clean
  • 删除项目根目录的build文件夹
  • 删除用户目录下的Gradle缓存:
    • macOS/Linux:rm -rf ~/.gradle/caches
    • Windows:删除C:\Users\<你的用户名>\.gradle\caches文件夹
  • 删除Android模块的缓存:删除项目android/.build和android/.cache文件夹
  • 最后重新运行:flutter run

2. 匹配Flutter SDK与AGP版本

AGP 8.x系列需要Flutter 3.16及以上版本支持,执行flutter --version查看当前SDK版本,若版本过低,升级到最新稳定版:

flutter upgrade

3. 重构Gradle插件配置(适配Gradle 8.x规范)

Gradle 8.x推荐使用Plugins DSL替代传统的buildscript配置,修改android/build.gradle为:

plugins {
    id 'com.android.application' version '8.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.20' apply false
    id 'dev.flutter.flutter-gradle-plugin' version '1.0.0' apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

同时修改android/app/build.gradle,移除dependencies中的kotlin-stdlib依赖(kotlin-android插件已自动引入):

// 删掉这一行
// implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

4. 检查第三方插件依赖冲突

运行flutter pub deps查看所有Flutter插件,排查是否有插件依赖过时的AGP版本,将这类插件更新到最新版,或暂时禁用可疑插件测试构建是否正常。

5. 验证local.properties配置

确保local.properties中的flutter.sdk路径为绝对路径,且路径中无空格、特殊字符:

flutter.sdk=/Users/你的用户名/flutter

内容的提问来源于stack exchange,提问作者Daniel Krivchun

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 02:50:19