由于国内android市场众多渠道,为了统计每个渠道的下载及其它数据统计,就需要我们针对每个渠道单独打包,如果让你打几十个市场的包岂不烦死了,不过有了gradle,这再也不是事了。
友盟多渠道打包
废话不多说,以友盟统计为例,在androidmanifest.xml里面会有这么一段:
第一步 在androidmanifest.xml里配置placeholder
productflavors.all {
flavor -> flavor.manifestplaceholders = [umeng_channel_value: name]
}
}
很简单清晰有没有?直接执行 ./gradlew assemblerelease , 然后就可以静静的喝杯咖啡等待打包完成吧。
assemble结合build variants来创建task
上一篇博客介绍了 assemble 这个命令,会结合 build type 创建自己的task,如:
./gradlew assemblerelease
如果我们想打包wandoujia渠道的release版本,执行如下命令就好了:
同理我想打全部release版本:
总之,assemble 命令创建task有如下用法:
**assemble**: 允许直接构建一个variant版本,例如assembleflavor1debug。
**assemble**: 允许构建指定build type的所有apk,例如assembledebug将会构建flavor1debug和flavor2debug两个variant版本。
**assemble**: 允许构建指定flavor的所有apk,例如assembleflavor1将会构建flavor1debug和flavor1release两个variant版本。
完整的gradle脚本
最后福利大放送,来一份我在项目中使用的完整的gradle文件配置:
def releasetime() {
return new date().format("yyyy-mm-dd", timezone.gettimezone("utc"))
}
android {
compilesdkversion 21
buildtoolsversion '21.1.2'
defaultconfig {
applicationid "com.boohee.*"
minsdkversion 14
targetsdkversion 21
versioncode 1
versionname "1.0"
// dex突破65535的限制
multidexenabled true
// 默认是umeng的渠道
manifestplaceholders = [umeng_channel_value: "umeng"]
}
lintoptions {
abortonerror false
}
signingconfigs {
debug {
// no debug config
}
release {
storefile file("../yourapp.keystore")
storepassword "your password"
keyalias "your alias"
keypassword "your password"
}
}
buildtypes {
debug {
// 显示log
buildconfigfield "boolean", "log_debug", "true"
versionnamesuffix "-debug"
minifyenabled false
zipalignenabled false
shrinkresources false
signingconfig signingconfigs.debug
}
release {
// 不显示log
buildconfigfield "boolean", "log_debug", "false"
minifyenabled true
zipalignenabled true
// 移除无用的resource文件
shrinkresources true
proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
signingconfig signingconfigs.release
applicationvariants.all { variant ->
variant.outputs.each { output ->
def outputfile = output.outputfile
if (outputfile != null && outputfile.name.endswith('.apk')) {
// 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
def filename = "boohee_v${defaultconfig.versionname}_${releasetime()}_${variant.productflavors[0].name}.apk"
output.outputfile = new file(outputfile.parent, filename)
}
}
}
}
}
// 友盟多渠道打包
productflavors {
wandoujia {}
_360 {}
baidu {}
xiaomi {}
tencent {}
taobao {}
...
}
productflavors.all { flavor ->
flavor.manifestplaceholders = [umeng_channel_value: name]
}
}
dependencies {
compile filetree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.3'
compile 'com.jakewharton:butterknife:6.0.0'
...
}
大家有问题或疑问、建议欢迎博客留言,android studio的教程暂且到这里结束了,相信大家基本的都已会使用了,还有其他技巧与操作靠大家自己摸索了,之后有时间也会在博客上整理下一些tips之类的,欢迎大家关注。
如对本文有疑问, 点击进行留言回复!!
Codeforces C. A Cookie for You (模拟 / 分类 / 贪心) (Round #654 Div.2)
DialogFragment弹窗(带黑色透明背景和不带黑色透明背景)
android 常用布局文件(LinearLayout,RelativeLayout,FrameLayout,ConstraintLayout,TableLayout)
网友评论