Android

来源:互联网 发布:算法分析与设计 王晓东 编辑:程序博客网 时间:2024/06/05 05:07

1. 版本号

  • version code
  • version name

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

// debug1.0.78.170427.153740.8fe425// release1.0.78.170427.8fe425

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()}

获取提交记录后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)}

获取 version code :

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

获取 version name :

// 获取 version namedef 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()}

使用 :

 versionCode getVersionCode(true) versionName getVersionName(true)

2. 打包重命名

debug 生成名称 :

appName_buildType.apk

release 生成名称

appName_channelName_buildType_vesionCode.apk

配置如下 :

 //打包命名    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);                    }            }    }

3. 多渠道

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

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 {           // 豌豆荚        }    }

配合 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>

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',]

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 codestatic def getVersionCode(boolean isDebug) {    if (isDebug) {        return Integer.parseInt(new Date().format("yyMMddHHmm"))    }    return getRevisionNumber()}// 获取 version namedef 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)}

0 0
原创粉丝点击