Flutter‘s app_plugin_loader Gradle plugin imperatively using the apply script错误解决

在这里插入图片描述

问题描述

用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的精彩世界!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值