Flutter 混合开发 - aar打包

背景

项目接入 Flutter 后有两种方式,一种是 module 引入开发,一种是 aar 依赖开发。当前项目中在 Debug 阶段为了方便调试采用 module 开发,在发版时(即 Release 阶段)采用 aar 依赖引入。为了配合这种模式就需要在 release 阶段将 aar 上传至公司 maven 仓库中,但目前 Flutter 仅提供本地发布,所以下面基于官方脚本修改,实现上传至公司 maven。

打包脚本

修改自: Flutter 仓库中的 aar_init_script.gradle。路径 ->flutter/packages/flutter_tools/gradle/aar_init_script.gradle

groovy 复制代码
import java.nio.file.Paths
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.publish.maven.MavenPublication

void configureProject(Project project, String outputDir) {
    if (!project.hasProperty("android")) {
        throw new GradleException("Android property not found.")
    }
    if (!project.android.hasProperty("libraryVariants")) {
        throw new GradleException("Can't generate AAR on a non Android library project.");
    }
    project.version = project.version.replace("-SNAPSHOT", "")

    if (project.hasProperty("buildNumber")) {
        project.version = project.property("buildNumber")
    }

    project.components.forEach { component ->
        if (component.name == "release") {
            println ("component = " + component.name )
            addAarTask(project, component)
        }
    }

    project.publishing {
        repositories {
    		// 在这里设置为自己的 maven 参考地址 !!!
            maven {
                url = uri("file://${outputDir}/outputs/repo")
            }
        }
    }

    if (!project.property("is-plugin").toBoolean()) {
        return
    }

    String storageUrl = System.getenv('FLUTTER_STORAGE_BASE_URL') ?: "https://storage.googleapis.com"
    project.repositories {
        maven {
            url "$storageUrl/download.flutter.io"
        }
    }
    String engineVersion = Paths.get(getFlutterRoot(project), "bin", "internal", "engine.version")
            .toFile().text.trim()
    project.dependencies {
        compileOnly ("io.flutter:flutter_embedding_release:1.0.0-$engineVersion") {
            transitive = false
        }
    }
}

void configurePlugin(Project project, String outputDir) {
    if (!project.hasProperty("android")) {
        return
    }
    configureProject(project, outputDir)
}


void addAarTask(Project project, component) {
    String variantName = component.name.capitalize()
    String taskName = "assembleAar$variantName"
    project.tasks.create(name: taskName) {
        if (!project.gradle.startParameter.taskNames.contains(taskName)) {
            return
        }
        project.publishing.publications.create(component.name, MavenPublication) { pub ->
            groupId = "${pub.groupId}"
            artifactId = "${pub.artifactId}_${pub.name}"
            version = "${pub.version}"
            from component
        }
        finalizedBy "publish"
    }
}

allprojects {
    apply plugin: "maven-publish"
}

projectsEvaluated {
    Project moduleProject = rootProject.subprojects.find { it.name == "flutter" }
    assert moduleProject != null
    assert moduleProject.hasProperty("output-dir")
    //打包dart代码
    configureProject(moduleProject, moduleProject.property("output-dir"))
	//打包三方依赖
    Set<Project> modulePlugins = rootProject.subprojects.findAll {
        it.name != "flutter" && it.name != "app"
    }
    modulePlugins.each { pluginProject ->
        configurePlugin(pluginProject, moduleProject.property("output-dir"))
        moduleProject.android.libraryVariants.all { variant ->
            String variantName = variant.name.capitalize()
            if (variantName == "release"){
                println ("moduleProject = " + moduleProject.name + " & variantName = " + variantName + " & pluginProject = " + pluginProject.name)
                Task moduleAssembleAarTask = moduleProject.tasks.findByPath("assembleAar$variantName")
                Task pluginAssembleAarTask = pluginProject.tasks.findByPath("assembleAar$variantName")
                if (moduleAssembleAarTask != null && pluginAssembleAarTask != null ){
                    moduleAssembleAarTask.dependsOn(pluginAssembleAarTask)
                }
            }
        }
    }
}

打包指令

shell 复制代码
# 根据需要修改 "VERSION"(即 aar 版本号)
./gradlew -I="$PROJECT_PATH"/publishAar.gradle -Pflutter-root="${HOME}"/flutter -Poutput-dir="$PROJECT_PATH"/build/host -Pis-plugin=false -PbuildNumber="$VERSION" --full-stacktrace --info -Pverbose=true -Ptarget=lib/main.dart -Pdart-obfuscation=false -Ptrack-widget-creation=true -Ptree-shake-icons=false -Ptarget-platform=android-arm64 assembleAarRelease

Hi,我是"青杉",您可以通过如下方式关注我:

相关推荐
君蓦6 小时前
Flutter 本地存储与数据库的使用和优化
flutter
problc16 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
lqj_本人1 天前
鸿蒙next选择 Flutter 开发跨平台应用的原因
flutter·华为·harmonyos
lqj_本人1 天前
Flutter&鸿蒙next 状态管理框架对比分析
flutter·华为·harmonyos
起司锅仔1 天前
Flutter启动流程(2)
flutter
hello world smile1 天前
最全的Flutter中pubspec.yaml及其yaml 语法的使用说明
android·前端·javascript·flutter·dart·yaml·pubspec.yaml
lqj_本人1 天前
Flutter 的 Widget 概述与常用 Widgets 与鸿蒙 Next 的对比
flutter·harmonyos
iFlyCai1 天前
极简实现酷炫动效:Flutter隐式动画指南第二篇之一些酷炫的隐式动画效果
flutter
lqj_本人1 天前
Flutter&鸿蒙next 中使用 MobX 进行状态管理
flutter·华为·harmonyos