Android - Gradle 使用干货 之 生成版本号,打包重命名和多渠道

 

Android - Gradle 使用干货 之 生成版本号,打包重命名和多渠道

标签: androidgradle渠道buildtype
  169人阅读  评论(0)  收藏  举报
  分类:
 

目录(?)[+]

1. 版本号

  • version code
  • version name

目标生成的 version name 如下所示 :

// debug
1.0.78.170427.153740.8fe425
// release
1.0.78.170427.8fe425
   
   
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

version code 都为 78 ;

解析:

  • 大版本号 : 1 (手动)
  • 小版本号 :0 (手动)
  • 修改版本号:78 (自动,git 提交次数)
  • 编译日期: 170427(自动,年月日), 153740 (自动,时分秒)
  • 提交记录: 8fe425 (自动,git 最后一次提交记录后6位)

获取 Git 提交次数:

// 获取修订版本 git 提交次数
static def getRevisionNumber() {
    Process process = "git rev-list --count HEAD".execute()
    process.waitFor()
    return process.getText().toInteger()
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

获取提交记录后6位:没有返回 yyDDmm 日期

// 获取修订版本最后一次 git 记录后6位
static def getRevisionDescription() {
    String desc = 'git describe --always'.execute().getText().trim()
    return (desc == null || desc.size() == 0) ? new Date().format("yyMMdd") : desc.substring(desc.size() - 6)
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

获取 version code :

// 获取 version code
static def getVersionCode(boolean isDebug) {
    if (isDebug) {
        return Integer.parseInt(new Date().format("yyMMddHHmm"))
    }
    return getRevisionNumber()
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

获取 version name :

// 获取 version name
def getVersionName(boolean isDebug) {
    String version = appConfig.appmajor +
            '.' + appConfig.appminor +
            '.' + getRevisionNumber()
    String today = new Date().format('yyMMdd')
    String time = new Date().format('HHmmss')
    if (isDebug) {
        return version + ".$today.$time." + getRevisionDescription()
    }
    return version + ".$today." + getRevisionDescription()
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

使用 :

 versionCode getVersionCode(true)
 versionName getVersionName(true)
   
   
  • 1
  • 2
  • 1
  • 2

2. 打包重命名

debug 生成名称 :

appName_buildType.apk
   
   
  • 1
  • 1

release 生成名称

appName_channelName_buildType_vesionCode.apk
   
   
  • 1
  • 1

配置如下 :

 //打包命名
    applicationVariants.all {
        variant ->
            variant.outputs.each {
                output ->
                    if (variant.buildType.name == 'release') {
                        variant.mergedFlavor.versionCode = getVersionCode(false)
                        variant.mergedFlavor.versionName = getVersionName(false)
                        // release
                        def apkName = "${project.getName()}_${variant.flavorName}_${buildType.name}_v${variant.versionCode}.apk";
                        output.outputFile = new File(output.outputFile.parent, apkName);
                    } else {
                        variant.mergedFlavor.versionCode = getVersionCode(true)
                        variant.mergedFlavor.versionName = getVersionName(true)
                        // debug
                        def apkName = "${project.getName()}_${buildType.name}.apk";
                        output.outputFile = new File(output.outputFile.parent, apkName);
                    }
            }
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3. 多渠道

这里有两个版本 : beta 和 production , 当然也可以写多个版本;

productFlavors {
        beta {
            buildConfigField "String", "API_BETA", "\"$appConfig.betaAPI\""
            applicationId appConfig.appId + ".beta"
            manifestPlaceholders = [APP_NAME: appConfig.betaName,
                                    APP_LOGO: appConfig.betaLogo,
                                    PACKAGE : appConfig.appId
            ]
        }
        production {
            buildConfigField "String", "API_PRO", "\"$appConfig.proAPI\""
            applicationId appConfig.appId
            manifestPlaceholders = [APP_NAME: appConfig.proName,
                                    APP_LOGO: appConfig.proLogo,
                                    PACKAGE : appConfig.appId
            ]
        }
        WDJ {
           // 豌豆荚
        }
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

配合 AndroidManifest.xml 使用,不同的版本可以不同的logoname :

 <application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="${APP_LOGO}"
        android:label="${APP_NAME}"
        android:roundIcon="${APP_LOGO}"
        android:supportsRtl="true"
        android:theme="@style/TestTheme"
        tools:replace="android:label,icon"></application>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4. 全部配置

配置 config.gradle 使用 配置如下

ext.versions = [
        // 本地仓库版本     
]

// app 相关信息总配置
ext.appConfig = [
        appmajor     : 1,
        appminor     : 0,
        appId        : 'com.smartahc.android.template.demo',
        keyAlias     : 'template',
        keyPassword  : 'template',
        storeFile    : 'template.jks',
        storePassword: 'template',
        betaAPI      : 'http://beta.labelnet.cn',
        betaName     : '@string/app_name',
        betaLogo     : '@mipmap/ic_launcher_round',
        proAPI       : 'http://pro.labelnet.cn',
        proName      : '@string/app_name',
        proLogo      : '@mipmap/ic_launcher',
]
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

build.gradle

apply plugin: 'com.android.application'
apply from: "$rootDir/config.gradle"


android {
    compileSdkVersion versions.complieSdk
    buildToolsVersion versions.buildTools
    // APP 配置
    defaultConfig {
        applicationId appConfig.appId
        minSdkVersion versions.minSdk
        targetSdkVersion versions.targetSdk
        versionCode getVersionCode(true)
        versionName getVersionName(true)

        // 65535
        multiDexEnabled true
    }

    lintOptions {
        abortOnError false
    }

    dataBinding {
        enabled = false
    }

    // 签名 配置
    signingConfigs {
        debug {

        }
        release {
            keyAlias appConfig.keyAlias
            keyPassword appConfig.keyPassword
            storeFile file(appConfig.storeFile)
            storePassword appConfig.storePassword
        }
    }
    // 编译类型
    buildTypes {
        buildTypes {
            debug {
                applicationIdSuffix '.debug'
                buildConfigField 'boolean', 'IS_DEBUG', 'true'
                debuggable true
                zipAlignEnabled true
                minifyEnabled false
                shrinkResources false
                signingConfig signingConfigs.debug
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            release {
                buildConfigField 'boolean', 'IS_DEBUG', 'false'
                debuggable false
                zipAlignEnabled true
                minifyEnabled false
                signingConfig signingConfigs.release
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    //打包命名
    applicationVariants.all {
        variant ->
            variant.outputs.each {
                output ->
                    if (variant.buildType.name == 'release') {
                        variant.mergedFlavor.versionCode = getVersionCode(false)
                        variant.mergedFlavor.versionName = getVersionName(false)
                        // release
                        def apkName = "${project.getName()}_${variant.flavorName}_${buildType.name}_v${variant.versionCode}.apk";
                        output.outputFile = new File(output.outputFile.parent, apkName);
                    } else {
                        variant.mergedFlavor.versionCode = getVersionCode(true)
                        variant.mergedFlavor.versionName = getVersionName(true)
                        // debug
                        def apkName = "${project.getName()}_${buildType.name}.apk";
                        output.outputFile = new File(output.outputFile.parent, apkName);
                    }
            }
    }

    productFlavors {
        beta {
            buildConfigField "String", "API_BETA", "\"$appConfig.betaAPI\""
            applicationId appConfig.appId + ".beta"
            manifestPlaceholders = [APP_NAME: appConfig.betaName,
                                    APP_LOGO: appConfig.betaLogo,
                                    PACKAGE : appConfig.appId
            ]
        }
        production {
            buildConfigField "String", "API_PRO", "\"$appConfig.proAPI\""
            applicationId appConfig.appId
            manifestPlaceholders = [APP_NAME: appConfig.proName,
                                    APP_LOGO: appConfig.proLogo,
                                    PACKAGE : appConfig.appId
            ]
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':core_ui')
}

// versionCode  : git commit 次数 ;
// versionName : 大版本号.小版本号.修改版本号.编译版本号 ;

// 获取 version code
static def getVersionCode(boolean isDebug) {
    if (isDebug) {
        return Integer.parseInt(new Date().format("yyMMddHHmm"))
    }
    return getRevisionNumber()
}

// 获取 version name
def getVersionName(boolean isDebug) {
    String version = appConfig.appmajor +
            '.' + appConfig.appminor +
            '.' + getRevisionNumber()
    String today = new Date().format('yyMMdd')
    String time = new Date().format('HHmmss')
    if (isDebug) {
        return version + ".$today.$time." + getRevisionDescription()
    }
    return version + ".$today." + getRevisionDescription()
}

// 获取修订版本 git 提交次数
static def getRevisionNumber() {
    Process process = "git rev-list --count HEAD".execute()
    process.waitFor()
    return process.getText().toInteger()
}

// 获取修订版本最后一次 git 记录后6位
static def getRevisionDescription() {
    String desc = 'git describe --always'.execute().getText().trim()
    return (desc == null || desc.size() == 0) ? new Date().format("yyMMdd") : desc.substring(desc.size() - 6)
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值