文章目录
问题描述
用Android studio开发flutter的andorid apk时编译报如下警告:You are applying Flutter’s app_plugin_loader Gradle plugin imperatively using the apply script method, which is deprecated and will be removed in a future release. Migrate to applying Gradle plugins with the declarative plugins block: https://flutter.dev/go/flutter-gradle-plugin-apply
解决方法
这是一个 Flutter Gradle 插件的应用方式过时的警告。它提示你目前的项目正在使用 apply
语句来引入 app_plugin_loader
Gradle 插件,这种方式即将被废弃。未来推荐使用 plugins
块的声明式方式来应用插件。
以下是解决该问题的步骤:
1. 查看当前的 android/build.gradle
在 Flutter 项目的 android/build.gradle
文件中,你可能会看到类似以下内容:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.22'
// 可能的 Flutter 插件加载器
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.19'
}
}
// 用 apply 加载插件的方式
apply from: "$flutterRoot/packages/flutter_tools/gradle/app_plugin_loader.gradle"
这段代码中的 apply from
是被警告提到的过时方式。
2. 迁移到 plugins
声明式方法
你需要将 apply
改为 plugins
块形式。打开 android/app/build.gradle
,按以下方式进行修改:
修改前:
apply from: "$flutterRoot/packages/flutter_tools/gradle/app_plugin_loader.gradle"
修改后:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.protobuf' // 如果用到了 protobuf
id 'com.android.library'
id 'com.google.gms.google-services' // 示例,仅用在 Firebase 中
}
注意:确保 plugins
块中的插件声明和你实际项目使用的 Gradle 插件版本一致。
3. 同步 Flutter 工具更新
在迁移后,运行以下命令以确保 Flutter 工具链与 Gradle 设置匹配:
flutter clean
flutter pub get
flutter build apk
4. 参考 Flutter 官方文档
Flutter 提供了 官方迁移指南,你可以根据项目需要调整配置。
补充说明
如果迁移后仍有问题,可以尝试以下排查步骤:
- 确保
android/gradle/wrapper/gradle-wrapper.properties
文件中 Gradle 版本为推荐版本。 - 确保
classpath
声明的 Gradle 插件版本与迁移的插件兼容。
结束语
Flutter是一个由Google开发的开源UI工具包,它可以让您在不同平台上创建高质量、美观的应用程序,而无需编写大量平台特定的代码。我将学习和深入研究Flutter的方方面面。从基础知识到高级技巧,从UI设计到性能优化,欢饮关注一起讨论学习,共同进入Flutter的精彩世界!