Android Studio打包应用默认生成的apk名称是:app-release.apk 、app-debug.apk
如果我们要让生成的apk名跟我们版本包名、渠道号有联系的话,那我们就要自定义生成的apk名了
需要在build.gradle(Module:app)文件下android{ }中添加:
android.applicationVariants.all {
variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def name = "qq_v${defaultConfig.versionName}_${variant.productFlavors[0].name}_${buildType.name}.apk"
output.outputFile = new File(outputFile.parent, name)
}
}
}
//多渠道
productFlavors {
tencent{
}
vivo{
}
oppo{
}
}
修改名字的地方
def name = "xxx_${defaultConfig.versionName}_${variant.productFlavors[0].name}_${buildType.name}.apk"
xxx:为你自己的工程名,自己起的标识- 比如 wx、qq、taobao
defaultConfig.versionName:版本号
variant.productFlavors[0].name:渠道标识
buildType.name:build方式release/debug
//这么写也可以
def name= "qq"+"_"+"v"+defaultConfig.versionName+"_"+variant.productFlavors[0].name+"_"+buildType.name+".apk"
然后使用命令打包
./gradlew assemble --会同时打debug和release的包
使用如上命令测试出包如图:
./gradlew assembleRelease --只打release的包
AndroidStudio升级到3.0后这种方式会有问题
Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=release, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl. Open File
1.需要修改 each() 和 outputFile() 方法为 all()
2.outputFileName
applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
variant.outputs.all {
def apkName = "qq_v${defaultConfig.versionName}_${variant.productFlavors[0].name}_${buildType.name}"
outputFileName = apkName + ".apk"
}
}
}
最后编辑于 :2018.08.14 19:26:57
©著作权归作者所有,转载或内容合作请联系作者 【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。 平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。