如何使用这种格式的gradle更改apk名称? [英] How to change apk name by using gradle like this format?

查看:155
本文介绍了如何使用这种格式的gradle更改apk名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 $ b 

使用Gradle创建应用程序时,我想更改app-release.apk文件名。 $ b [格式]
(软件包名称的应用程序名称)_V(版本代码)_(yyMMdd)_(R | T)

[解释]
):例子)com.example.myApp - > myApp
(版本代码):构建版本代码2.2.3 - > 223
(yyMMdd):构建日期2015.11.18 - > 151118
(R | T):如果app被释放,R但调试是T。

如果我在版本中生成一个apk文件,结果是:myApp_V223_151118_R.apk。

如何在gradle中创建一个像这样的文件名?更新:请检查 Anrimian的答案,下面简单得多。



试试这:

gradle.properties

  applicationName = MyApp 

build.gradle $ b

  android {
...
defaultConfig {
versionCode 111
...
}
buildTypes {
release {
...
applicationVariants.all {variant - >
renameAPK(variant,defaultConfig,'R')
}
}
debug {
...
applicationVariants.all {variant - >
renameAPK(variant,defaultConfig,'T')
}
}
}
}
def renameAPK(variant,defaultConfig,buildType){
variant.outputs.each {输出 - >
def formattedDate = new Date()。format('yyMMdd')

def file = output.packageApplication.outputFile
def fileName = applicationName +_V+ defaultConfig.versionCode +_+ formattedDate +_+ buildType +.apk
output.packageApplication.outputFile = new File(file.parent,fileName)
}
}

参考:
https://stackoverflow.com/a/30332234/206292
https ://stackoverflow.com/a/27104634/206292


I'd like to change "app-release.apk" file name to like following when I build an app by using gradle.

[format]
(appname of package name)_V(version code)_(yyMMdd)_(R|T)

[explain]
(appname of package name) : example) com.example.myApp -> myApp
(version code) : build version code 2.2.3 -> 223
(yyMMdd) : build date 2015.11.18 -> 151118  
(R|T) : if app is release, "R" but debug is "T".

If I generate an apk file in release, result is : myApp_V223_151118_R.apk.

How to make a file name like this in gradle?

解决方案

Update: Please check Anrimian's answer below which is much simpler and shorter.

Try this:

gradle.properties

applicationName = MyApp

build.gradle

android {
  ...
  defaultConfig {
     versionCode 111
     ...
  }
  buildTypes {
     release {
         ...
         applicationVariants.all { variant ->
             renameAPK(variant, defaultConfig, 'R')
         }
     }
     debug {
         ...
         applicationVariants.all { variant ->
             renameAPK(variant, defaultConfig, 'T')
         }
     }
  }
}
def renameAPK(variant, defaultConfig, buildType) {
 variant.outputs.each { output ->
     def formattedDate = new Date().format('yyMMdd')

     def file = output.packageApplication.outputFile
     def fileName = applicationName + "_V" + defaultConfig.versionCode + "_" + formattedDate + "_" + buildType + ".apk"
     output.packageApplication.outputFile = new File(file.parent, fileName)
 }
}

Reference: https://stackoverflow.com/a/30332234/206292 https://stackoverflow.com/a/27104634/206292

这篇关于如何使用这种格式的gradle更改apk名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆