Android Studio 动态版本号,[build.gradle配置系列(一)]android studio根据版本号动态生成apk名...

1、 在build.gradle定义函数,根据时间动态返回时间标签

def static releasetime() {

return new date().format("yyyymmdd", timezone.gettimezone("utc"))

}

2、根据时间生成versionname

android {

compilesdkversion 26

buildtoolsversion "26.0.0"

defaultconfig {

applicationid "com.jason.log.text"

minsdkversion 21

targetsdkversion 26

versioncode 1

versionname "1.0".concat("_").concat(releasetime())//版本号加上时间

testinstrumentationrunner "android.support.test.runner.androidjunitrunner"

}

3、在android标签中的buildtypes配置动态生成apk名

android {

...

buildtypes {

debug {

minifyenabled false

buildconfigfield "boolean", "log_debug", "false" //定义变量,区分debug 或release 状态,可直接在代码中buildconfig.log_debug使用,便于添加调试log

}

release {

//签名

minifyenabled false

buildconfigfield "boolean", "log_debug", "true"

proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'

}

applicationvariants.all { variant ->

variant.outputs.each { output ->

def outputfile = output.outputfile

if (outputfile != null && outputfile.name.endswith('.apk')) {

def type = ""

if (variant.buildtype.name == 'debug') {

type = "_debug"

}

def filename = "logtest_v${defaultconfig.versionname}${type}.apk" //定义apk名

output.outputfile = new file(outputfile.parent, filename)

}

}

}

}

}

另外,我们可以在debug或release 标签中定义一个变量,用于控制是否输出调试log,如下:

buildconfig.log_debug在debug状态为true,release 状态下为false

if(buildconfig.log_debug){

log.d(tag,"....");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值