android两种渠道支持一键打包 + 随意组合各种渠道

该代码支持两种渠道的打包,对于一种或两种以上,可自行修改代码来支持,很简单的

在app下面的build.gradle.kts中,里面都有注释,复制直接用,只需修改一下渠道变量即可

kotlin 复制代码
// 定义渠道,在android{}外面哦!
val channel_pad = "pad"
val channel_phone = "phone"
val model_hongmi = "xiaomi"
val model_huawei = "huawei"
// 下面是所有渠道包list
val allChannels = listOf(channel_pad, channel_phone)
val allModels = listOf(model_hongmi, model_huawei)
// 下面是要打特定渠道包map,自由组合
val specifyMap = listOf(
    channel_pad to model_hongmi,
    channel_pad to model_huawei,
)
// 打特定渠道测试包,直接点三角运行
tasks.register("assembleSpecifyDebug") {
    group = "packaging"
    description = "Build all debug APKs for all channel+model combinations"

    specifyMap.forEach { (channel, model) ->
        val taskName = "assemble${channel.capitalize()}${model.capitalize()}Debug"
        println("开始打包: $taskName")  // 调试输出,可在 Gradle 同步时看到
        dependsOn(taskName)
    }

    doLast {
        println("✅ 所有渠道打包完成!共 ${specifyMap.size} 个包")
    }
}
// 打特定渠道正式包,直接点三角运行
tasks.register("assembleSpecifyRelease") {
    group = "packaging"
    description = "Build all release APKs for all channel+model combinations"

    specifyMap.forEach { (channel, model) ->
        val taskName = "assemble${channel.capitalize()}${model.capitalize()}Release"
        println("开始打包: $taskName")  // 调试输出,可在 Gradle 同步时看到
        dependsOn(taskName)
    }

    doLast {
        println("✅ 所有渠道打包完成!共 ${specifyMap.size} 个包")
    }
}

android {
    // .........
    // 定义维度,多渠道
    flavorDimensions += listOf("channel", "model")

    productFlavors {
        allChannels.forEach { channel ->
            create(channel) {
                dimension = "channel"
            }
        }

        allModels.forEach { model ->
            create(model) {
                dimension = "model"
                val apiUrl = if (model == model_huawei) {
                    "https://api.pjx.pjx"
                } else "https://api.example.com"
                buildConfigField("String", "API_URL", "\"$apiUrl\"")
            }
        }
    }
}
// 自定义打包包名,是在android{}外面哦!
androidComponents {
    onVariants { variant ->
        variant.outputs.forEach { output ->
            if (output is com.android.build.api.variant.impl.VariantOutputImpl) {
                val flavorName = variant.flavorName ?: ""
                val buildType = variant.buildType ?: ""
                val verName = android.defaultConfig.versionName ?: "1.0"

                output.outputFileName.set("xbb_${flavorName}_${buildType}_v${verName}.apk")
            }
        }
    }
}
相关推荐
Carson带你学Android15 小时前
见证历史!Swift 6.3 官方支持 Android,跨平台要变天了?
android
plainGeekDev16 小时前
Android性能优化面试题:你说你会优化,结果连ANR都排查不了
android·面试
richard_yuu17 小时前
鸿蒙本地数据存储实战|Preferences 封装、数据隔离与隐私合规存储方案
android·华为·harmonyos
木易 士心17 小时前
深入理解 OKHttp:设计模式、核心机制与架构优势
android·设计模式·架构
Ehtan_Zheng17 小时前
Jetpack Compose `@ReadOnlyComposable` 的“魔法”
android
沐言人生17 小时前
ReactNative 源码分析11——Native View创建流程setChildren和manageChildren
android·react native
诸神黄昏EX17 小时前
Android Build系列专题【篇七:VINTF源码解析】
android
plainGeekDev17 小时前
Android Framework 面试题:Binder都说不清楚,简历别写精通了
android·java
萌新杰少17 小时前
安卓原生项目迁移KMP——核心迁移
android·kotlin·jetbrains
小孔龙17 小时前
AndroidManifest.xml 配置速查手册
android