【flutter 在最新版本中已经弃用了传统的 apply from 方式引入 Gradle 插件】
报错
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Launching lib\main.dart on 2112123AC in debug mode...
Running Gradle task 'assembleDebug'...FAILURE: Build failed with an exception.* Where:
Script 'D:\flutter\flutter_3.29.2\packages\flutter_tools\gradle\flutter.gradle' line: 9* What went wrong:
A problem occurred evaluating script.
> You are applying Flutter's main Gradle plugin imperatively using the apply script method, which is not possible anymore. Migrate to applying Gradle plugins with the declarative plugins block: https://flutter.dev/to/flutter-gradle-plugin-apply* 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 1s
Error: Gradle task assembleDebug failed with exit code 1
解决方案
这个错误是因为 Flutter 在最新版本中已经弃用了传统的 apply from
方式引入 Gradle 插件,要求使用更现代的 Declarative Plugins Block(声明式插件块)。以下是完整的解决方案:
错误原因分析
You are applying Flutter's main Gradle plugin imperatively using the apply script method...
- 根本问题:Flutter 3.x+ 要求使用新的插件声明方式
- 影响文件:
android/app/build.gradle
- 关键变化:必须用
plugins {}
块替换apply from
解决方案(分步操作)
第一步:修改 android/app/build.gradle
-
删除旧代码:移除这行
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
-
添加新插件声明:在文件顶部添加(通常在
android {}
块之前):plugins {id "com.android.application"id "kotlin-android"id "dev.flutter.flutter-gradle-plugin" // 新增的Flutter插件 }
第二步:修改 android/settings.gradle
-
添加插件管理配置:在文件开头添加:
pluginManagement {repositories {google()mavenCentral()gradlePluginPortal()}plugins {id "dev.flutter.flutter-gradle-plugin" version "1.0.0" // 版本号根据实际情况调整} }
-
添加依赖路径:在文件末尾添加:
include ':app' def flutterProjectDir = rootProject.projectDir.parentFile.toPath() def plugins = new Properties() def pluginsFile = new File(flutterProjectDir.toFile(), '.flutter-plugins') if (pluginsFile.exists()) {pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) } } plugins.each { name, path ->def pluginDirectory = flutterProjectDir.resolve(path).resolve('android').toFile()include ":$name"project(":$name").projectDir = pluginDirectory }
第三步:检查/更新 local.properties
确保文件中有正确的 Flutter SDK 路径:
flutter.sdk=D\:\\flutter\\flutter_3.29.2 # 注意Windows路径转义
sdk.dir=C\:\\Users\\YourName\\AppData\\Local\\Android\\Sdk
第四步:清理并重建项目
在终端执行:
flutter clean
flutter pub get
cd android
./gradlew clean # 或 gradlew.bat clean (Windows)
flutter run
备选方案(如果问题依旧)
如果以上修改后仍有问题,可以尝试临时降级 Flutter 版本:
flutter version 3.7.12 # 选择支持旧插件的版本
或更新到最新稳定版:
flutter upgrade
文件结构最终示例
android/app/build.gradle
顶部
plugins {id "com.android.application"id "kotlin-android"id "dev.flutter.flutter-gradle-plugin" // Flutter插件
}def localProperties = new Properties()
...
android/settings.gradle
完整示例
pluginManagement {repositories {google()mavenCentral()gradlePluginPortal()}plugins {id "dev.flutter.flutter-gradle-plugin" version "1.0.0"}
}include ':app'
def flutterProjectDir = rootProject.projectDir.parentFile.toPath()
...
重要提示:这些修改基于 Flutter 3.29.2 的官方适配要求,如果你的版本不同,可能需要微调插件版本号(可在 Flutter 发布日志 中查找对应版本)。