Android Studio打包应用默认生成的apk名称是:app-release.apk 、如果我们要让生成的apk名跟我们版本包名有联系的话,那我们就要自定义生成的apk名了
需要在build.gradle(Module:app)文件下android{ }中添加:
// apk name def
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, defaultConfig.applicationId + "-" + buildType.name + "-v" +
defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk" );
}
}
这样我们打包的apk名就是:com.test.demo-release-v1.01-2.apk ,就自动带上我们的包名和版本号了,省去每次都要手动自己改了。
如果要生成如下版本号时间戳的apk包名,则可以用如下build.gradle代码:
模块名-渠道名-版本号-版本名称-包名-编译时间.apk
apply plugin: 'com.android.application' def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
} android {
compileSdkVersion 23
buildToolsVersion "23.0.1" defaultConfig {
applicationId "com.djk.myapplication"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
} productFlavors{
develop{}
xiaomi{}
huawei{}
anzhi{}
} android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
//这里修改apk文件名
def fileName = "demo_${variant.productFlavors[0].name}-${defaultConfig.versionCode}-${defaultConfig.versionName}-${releaseTime()}.apk"
//def fileName = outputFile.name.replace("app", "${rootProject.ext.appName}-${releaseTime()}-${defaultConfig.versionCode}-${defaultConfig.versionName}")
output.outputFile = new File(outputFile.parent, fileName)
}
}
} } dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
}
效果
补充:
ref:
Android Studio apk打包自定义包名
https://blog.csdn.net/github_37472200/article/details/78537592?locationNum=3&fps=1
// for as 3.3+
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"))
}
// for as 3.3 以下
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.outputFil
生产的包名为:项目名-版本号-打包时间-编译类型.apk
这个有个BUG就是在打包时能正常生产apk包,但是直接运行安装到手机时会提示找不到包名,初步分析是因为包名中含有时间戳,去掉分秒的时间就可以了:”yyyyMMddHH”
ref:
Android studio gradle配置完整版(转) - petercao - 博客园
http://www.cnblogs.com/bluestorm/p/6641083.html
/**
* apk rename
* 生成apk名形如: cqq-v1.0-1-release.apk
*/
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) { // like: com.android.cmm.cqq-v1.0-1-release.apk
outputFileName = defaultConfig.applicationId + "-v" + defaultConfig.versionName + "-" +
defaultConfig.versionCode + "-" + output.baseName + ".apk" // like: cqq-v1.0-1-release.apk
// outputFileName = defaultConfig.applicationId.subSequence(
// defaultConfig.applicationId.lastIndexOf(".") + 1,
// defaultConfig.applicationId.length()) + "-v" + defaultConfig.versionName + "-" +
// defaultConfig.versionCode + "-" + output.baseName + ".apk"
}
}
}