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,我是"青杉",您可以通过如下方式关注我:

相关推荐
jcLee956 小时前
Flutter/Dart:使用日志模块Logger Easier
flutter·log4j·dart·logger
tmacfrank7 小时前
Flutter 异步编程简述
flutter
tmacfrank7 小时前
Flutter 基础知识总结
flutter
叫我菜菜就好8 小时前
【Flutter_Web】Flutter编译Web第三篇(网络请求篇):dio如何改造方法,变成web之后数据如何处理
前端·网络·flutter
AiFlutter12 小时前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
m0_748247802 天前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
迷雾漫步者2 天前
Flutter组件————PageView
flutter·跨平台·dart
迷雾漫步者2 天前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig2 天前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜2 天前
flutter自学笔记5- dart 编码规范
flutter·dart