android多渠道打包定义名字,Android Studio自定义多渠道打包

现在为了推广产品,会在多个渠道应用市场发布应用,为了统计不同渠道的数据,需要在应用中表明渠道,如果一个一个去修改渠道打包效率会很低。AS为我们提供了简便的方法:配置好所需渠道信息,可以一次打包所有的渠道包。下面就来介绍一下多渠道打包配置和自定义APK打包名称方法。

14d3d3d9803d?utm_source=tuicool&utm_medium=referral

多渠道签名图

1. AS 2.x多渠道打包

(1) AndroidManifest中增加节点

...

android:name="UMENG_CHANNEL"

android:value="${UMENG_CHANNEL_VALUE}" />

...

(2) 项目app下 build.gradle 的android中添加配置

android {

....

//多渠道打包

productFlavors {

yingyongbao {}

huawei {}

baidu {}

xiaomi {}

qh360 {}

}

productFlavors.all {

flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]

}

....

}

2. AS 2.x自定义APK打包名称

android {

....

//自定义打包时apk名称

applicationVariants.all { variant ->

variant.outputs.each { output ->

def fileName = "${variant.versionName}_${variant.productFlavors[0].name}_release.apk"

def outFile = output.outputFile

if (outFile != null && outFile.name.endsWith('.apk')) {

output.outputFile = newFile(outFile.parent, fileName)

}

}

....

}

APK名称:版本名_渠道名.apk

3. AS 3.x多渠道打包

(1) AndroidManifest中增加节点(和2.x一样,没有变化)

...

android:name="UMENG_CHANNEL"

android:value="${UMENG_CHANNEL_VALUE}" />

...

(2) 项目app下 build.gradle 的android中添加配置

多版本打包

android {

....

//多渠道打包

flavorDimensions "tier","minApi"

productFlavors {

yingyongbao {

dimension "tier"

}

huawei {

dimension "tier"

}

baidu {

dimension "tier"

}

xiaomi {

dimension "minApi"

}

qh360 {

dimension "minApi"

}

}

productFlavors.all {

flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]

}

....

}

单版本打包

android {

...

defaultConfig {

...

flavorDimensions "code"

}

productFlavors {

yingyongbao {}

huawei {}

baidu {}

xiaomi {}

qh360 {}

}

productFlavors.all {

flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]

}

...

}

AS 3.0后gradle添加了flavorDimensions属性,用来控制多个版本的代码和资源,缺失会报错

4. AS 3.x自定义APK打包名称

android {

....

//自定义打包时apk名称

applicationVariants.all { variant ->

variant.outputs.all { output ->// each 改为 all

def fileName = "${variant.versionName}_${variant.productFlavors[0].name}_release.apk"

def outFile = output.outputFile

if (outFile != null && outFile.name.endsWith('.apk')) {

outputFileName = fileName // output.outputFile 改为 outputFileName

//或者 outputFileName = new File(outFile.parent, fileName).getName()

}

}

....

}

APK名称:版本名_渠道名.apk,此外,AS 3.0后打包完,apk所在层级发生了变化:release/xxx.apk,而且还多一个release/output.json参数文件。如果不想要output.json文件,可以打包完成后自动删除。

applicationVariants.all { variant ->

variant.outputs.all { output ->

def fileName = "${variant.versionName}_${variant.productFlavors[0].name}_release.apk"

def outFile = output.outputFile

if (outFile != null && outFile.name.endsWith('.apk')) {

outputFileName = new File(outFile.parent, fileName).getName()

}

}

variant.assemble.doLast {

variant.outputs.all { output ->

delete output.outputFile.parent + "/output.json"

copy {

from output.outputFile.parent

into output.outputFile.parentFile.parent

}

delete output.outputFile.parent

}

}

}

这样最后打包后的 apk文件就和 AS 2.x打包后的一样了。

以上就是 AS 2.x和 AS 3.x多渠道打包、自定义打包APK名称对比区别,大家可根据自己的AS版本来选用相应的方法,希望对大家有所帮助!

========== 相关推荐 ==========

============================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值