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


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

打包指令


# 根据需要修改 “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,我是“青杉”,您可以通过如下方式关注我:

你可能感兴趣的:(flutter,开发总结,flutter,flutter混合开发,flutter打包)