flutter 升级3.19 后 gradle 迁移

6,782 阅读1分钟

官方迁移指南

报错

You are applying Flutter's main 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

原因

升级 flutter 3.19 以后 增加了对应用这些插件的 Gradle 声明性插件{}块(也称为插件 DSL)的支持,现在推荐使用这种方法使用 Plugin DSL 来应用 Gradle 插件 在3.16 前创建的文件需要手动迁移

迁移

修改 项目/android/settings.gradle

image.png

内容删掉替换为

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

include ":app"

image.png

修改 app/build.gradle

将文件中 apply plugin 部分进行修改

image.png

在文件顶部添加


plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

image.png

删掉

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.")
}

示例

添加导入华为包com.huawei.agconnect

git仓库