Android-build.gradle动态版本号

build.gradle动态版本号


ps:最近遇到了qa搞不对包的问题,于是就简单学习了一下动态版本号,用来区分不同日期的包

defaultConfig {
        applicationId "xxx.xxx.xxx"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode xx
        versionName xx
    }

这是我们的默认Config,我们要做的有两点
1.区分环境
2.动态赋值versionName

1.区分环境

applicationVariants.all{ variant ->
        switch (variant.getName()){
            case "stgRelease" :
                break
            case "stgDebug" :
                break
            default :
            	break
           	...
        }
    }

在这里插入图片描述
在我们的项目里 通过

applicationVariants.all{ variant ->
        switch (variant.getName())
}

来区分不同的环境 variant.getName() 对应不同buildType

2.动态赋值

首先,定义两个方法
方法中传一个布尔值用来区分不同环境的值返回
具体的version算法 可以自己定义

def getVersionCode(boolean isRelease) {
	//VersionCode一定是int类型
    // 正式环境
    if (isRelease) {
        40
    }
    // debug环境
    else {
        Integer.parseInt(new Date().format("yyMMddHHmm"))
    }
}
def getVersionName(boolean isRelease) {
	//VersionName一定是String类型
    // 正式环境
    if (isRelease) {
        "x.x.x"
    }
    // debug环境
    else {
        String today = new Date().format("MMdd")
        String time = new Date().format("HHmm")
        "x.x.x.$today.$time"
    }
}

包括getVersionCode里的40,可以通过静态变量来赋值,方便查找

ext.appVersionCode = 40

最后在Config里及applicationVariants里动态改变version

defaultConfig {
        applicationId "xxx.xxx.xxx"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode getVersionCode(true)
        versionName getVersionName(true)
    }
applicationVariants.all{ variant ->
        switch (variant.getName()){
            case "stgRelease" :
             variant.outputs.each { output ->
                    output.versionCodeOverride = getVersionCode(false)
                    output.versionNameOverride = getVersionName(false)
                }
                break
            case "stgDebug" :
             variant.outputs.each { output ->
                    output.versionCodeOverride = getVersionCode(false)
                    output.versionNameOverride = getVersionName(false)
                }
                break
            default :
             variant.outputs.each { output ->
                    output.versionCodeOverride = getVersionCode(true)
                    output.versionNameOverride = getVersionName(true)
                }
            	break
           	...
        }
    }

最后贴一下整个build.gradle

apply plugin: 'com.android.application'

ext.appVersionCode = 40
ext.debugAppVersionCode = 40
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    
    defaultConfig {
        applicationId "xxx.xxx.xxx"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode getVersionCode(true)
        versionName getVersionName(true)
        ...
    }
    ...
    applicationVariants.all{ variant ->
        switch (variant.getName()){
            case "stgRelease" :
                variant.outputs.each { output ->
                    output.versionCodeOverride = getVersionCode(false)
                    output.versionNameOverride = getVersionName(false)
                }
                break
            case "stgDebug" :
                variant.outputs.each { output ->
                    output.versionCodeOverride = getVersionCode(false)
                    output.versionNameOverride = getVersionName(false)
                }
                break
            default :
                variant.outputs.each { output ->
                    output.versionCodeOverride = getVersionCode(true)
                    output.versionNameOverride = getVersionName(true)
                }
                break
        }
    }
...

def getVersionCode(boolean isRelease) {
    // 正式环境
    if (isRelease) {
        appVersionCode
    }
    // debug环境
    else {
        debugAppVersionCode
    }
}

//Major_Version_Number.Minor_Version_Number[Revision_Number[.Build_Number]]
def getVersionName(boolean isRelease) {
    // 正式环境
    if (isRelease) {
        "x.x.x"
    }
    // debug环境
    else {
        String today = new Date().format("MMdd")
        String time = new Date().format("HHmm")
        "x.x.x.$today.$time"
    }
}

dependencies {
    ...
}

debug版本号
在这里插入图片描述
正式版本号
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值