白羽
2019-01-23
来源 :网络
阅读 1507
评论 0
摘要:本文将带你了解Android应用开发Android Studio apk打包自定义包名,希望本文对大家学Android有所帮助。
本文将带你了解Android应用开发Android Studio apk打包自定义包名,希望本文对大家学Android有所帮助。
Android Studio apk打包自定义包名,直接上代码:
android {
......
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
outputFileName =
defaultConfig.applicationId.subSequence(defaultConfig.applicationId.lastIndexOf(""."")
+ 1, defaultConfig.applicationId.length()) + ""-v"" +
defaultConfig.versionCode + ""-"" + releaseTime() + ""-"" +
output.baseName + "".apk""
}
}
}
}
def releaseTime() {
return new Date().format(""yyyyMMddHHmmss"", TimeZone.getTimeZone(""UTC""))
}
生产的包名为:项目名-版本号-打包时间-编译类型.apk
该代码只能在3.0.0及以上使用,variant.outputs.all 方法只在3.0.0才出现的
如果你想在3.0.0以下使用你可以试试这个:
android {
......
variant.outputs.each { output ->
if (""release"".equals(variant.buildType.name)) {
println(""releasebaseName:""+variant.buildType.name)
def string =
defaultConfig.applicationId.subSequence(defaultConfig.applicationId.lastIndexOf(""."")
+ 1,
defaultConfig.applicationId.length()) + ""-v"" +
defaultConfig.versionCode + ""-"" + releaseTime() + ""-"" +
output.baseName + "".apk"";
output.outputFile = new File(output.outputFile.parent, string)
}else if (""debug"".equals(variant.buildType.name)) {
println(""debugbaseName:""+variant.buildType.name)
def string =
defaultConfig.applicationId.subSequence(defaultConfig.applicationId.lastIndexOf(""."")
+ 1,
defaultConfig.applicationId.length()) + ""-v"" +
defaultConfig.versionCode + ""-"" + releaseTime() + ""-"" +
output.baseName + "".apk"";
output.outputFile = new File(output.outputFile.parent, string)
}
}
}
def releaseTime() {
return new Date().format(""yyyyMMddHHmmss"", TimeZone.getTimeZone(""UTC""))
}
生产的包名为:项目名-版本号-打包时间-编译类型.apk
这个有个BUG就是在打包时能正常生产apk包,但是直接运行安装到手机时会提示找不到包名,初步分析是因为包名中含有时间戳,去掉分秒的时间就可以了:”yyyyMMddHH”。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号