Studio多渠道打包并生成自定义apk文件名字

Studio中基于 umeng 统计的多渠道打包。

1.在AndroidMainfest.xml设置动态渠道变量

2.在 build.gradle(Module:app) 设置productFlavors
android {
    productFlavors {
        kuan {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "kuan"]
        }
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
        }
        qh360 {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]
        }
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
        }
        wandoujia {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
        }
    }
}

3.sycn project一下再打包即可,Build-->Generate signed APK,Flavors中多选,选择需要的渠道

4.扩展:打包的生成自定义名字的apk
android {
// replace the umeng channel value according to flavor channel property.
applicationVariants.all{ variant ->
    // change output apk path.
    variant.outputs.each { output ->
        if ("${variant.getBuildType().name}" == "release") {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd-HHmmss")
            def dateFlag = "${dateFormat.format(new Date())}"
            def versionFlag = "${android.defaultConfig.versionName}"
            def flavorFlag = "${variant.productFlavors.get(0).name}"
            def newName = "releases/qspLauncher_" + dateFlag + "_v" + versionFlag + "_" + flavorFlag + ".apk"
            if (output.zipAlign) {
                output.zipAlign.outputFile = new File(getBuildDir(), newName)
            }
        }
    }
}
}

5.完成,注意生成目录

你可能感兴趣的:(android)