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

相关推荐
Summer不秃17 分钟前
Flutter之使用mqtt进行连接和信息传输的使用案例
前端·flutter
旭日猎鹰21 分钟前
Flutter踩坑记录(二)-- GestureDetector+Expanded点击无效果
前端·javascript·flutter
sunly_22 分钟前
Flutter:AnimatedSwitcher当子元素改变时,触发动画
flutter
AiFlutter23 分钟前
Flutter封装Coap
flutter
旭日猎鹰6 小时前
Flutter踩坑记录(三)-- 更改入口执行文件
flutter
旭日猎鹰6 小时前
Flutter踩坑记录(一)debug运行生成的项目,不能手动点击运行
flutter
️ 邪神6 小时前
【Android、IOS、Flutter、鸿蒙、ReactNative 】自定义View
flutter·ios·鸿蒙·reactnative·anroid
比格丽巴格丽抱18 小时前
flutter项目苹果编译运行打包上线
flutter·ios
SoaringHeart18 小时前
Flutter进阶:基于 MLKit 的 OCR 文字识别
前端·flutter
AiFlutter1 天前
Flutter通过 Coap发送组播
flutter