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"
}
}
}

Android studio 自定义打包apk名的更多相关文章

  1. Android studio 自定义打包APK名称

    Android Studio打包应用默认生成的apk名称是:app-release.apk .如果我们要让生成的apk名跟我们版本包名有联系的话,那我们就要自定义生成的apk名了,要怎么做呢. 我们只 ...

  2. <Android Studio> 3.打包APK

    我的IDE版本是 3.5 我希望输出的apk文件格式是: 名称_v版本_release/debug_日期 时间.apk 步骤: 1.打开build.gradle 末尾添加如下代码 def releas ...

  3. [Android Studio] Android studio 多渠道打包(超简洁版)

    [Android Studio] Android studio 多渠道打包(超简洁版) 转载:http://xuyazhou.com/archives/461 http://relex.me/usin ...

  4. Android Studio 动态调试 apk 反编译出的 smali 代码

    在信安大赛的准备过程中,主要通过 Android Studio 动态调试 apk 反编译出来的 smali 代码的方式来对我们分析的执行流程进行验证.该技巧的主要流程在此记录.以下过程使用 Andro ...

  5. Android studio 使用心得(四)—android studio 多渠道打包(二)

    Android studio 使用心得(四)—android studio 多渠道打包 这篇文章讲了一种打包方式.是直接在android studio 里面可视化操作,结合配置文件.我个人觉得严格上来 ...

  6. android studio 自定义路径安装报错"You are attempting to install the android SDK

    android studio 自定义路径安装报错"You are attempting to install the android SDK 解决方法: 出现这个提示 主要是安装 Andro ...

  7. Android Studio 自定义debug签名文件keystore

    Android Studio 自定义debug签名文件keystore

  8. 第07讲- Android项目的打包apk

    第07讲Android项目的打包apk 方法一:在工作目录bin文件夹下有一个与项目同名的apk文件 (最懒惰的方式,不推荐,不安全,不利于版本更新,只有在开发模式时使用) 方法二:使用key方式 签 ...

  9. Android Studio单独生成apk

    /********************************************************************* * Android Studio单独生成apk * 说明: ...

随机推荐

  1. coreseek增量索引合并

    重建主索引和增量索引: [plain] view plain copy /usr/local/coreseek/bin/indexer--config /usr/local/coreseek/etc/ ...

  2. jQuery校验

    jQuery校验 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库 <script src=&q ...

  3. C语言基础(11)-随机数发生器

    一. rand() rand是一个C语言库函数,功能是生成一个随机数.rand需要一个不同的种子,才能生成不同的随机数. 二. srand(int seed) rand需要一个不同的种子,才能生成不同 ...

  4. sql 脚本编写之路 常用语句(一)

    1.用一个表中的某一列更新另外一个表的某些列: for ACCESS 数据库: update a, b set a.name=b.name1 where a.id=b.id for SQL Serve ...

  5. Linux 平台GCC使用小结

    gcc -Wall [-I search_headfile_path] [-L search_lib_path] sourcefile -lNAME -o exe-name -Wall选项打开所有最常 ...

  6. VB中PictureBox控件使用教程

    PictureBox对象可以说是任何对象的原始型态,它可以加载图片.显示文字.画图外,它还能与Frame对象一样,在自己本身里头加载其它的对象而自成一个小群组,用PictureBox可以仿真出任何对象 ...

  7. ffmpeg为视频添加时间戳 - 手动编译ffmpeg

    FFMPEG给视频加时间戳水印 项目中需要给视频添加时间戳,理所当然最好用的办法是ffmpeg.在找到正确的做法前,还被网上的答案timecode给水了一下(水的不轻,在这里转了2天),大概是这样写的 ...

  8. 微信小程序开发工具的数据,配置,日志等目录在哪儿? 怎么找?

    原文地址:http://www.wxapp-union.com/portal.php?mod=view&aid=359 本文由本站halfyawn原创:感谢原创者:如有疑问,请在评论内回复   ...

  9. ovirt配置为cas登录

    准备工作 Ovirt测试机.CAS服务器.AD服务器 cas.crt -- CAS服务器的CA证书 allwinner.cer -- CAS服务器的证书颁发机构根证书 Ovirt测试机要求:apach ...

  10. erlang 虚机性能调优

    erlang 默认启动参数更多的是针对电信平台实时特性,简单调整参数能很大程度降低CPU消耗,提高处理能力. 1. 关闭spin_wait 设置参数:+sbwt none 我上一篇文章提到:erlan ...