目标生成的 version name
如下所示 :
// debug
1.0.78.170427.153740.8fe425
// release
1.0.78.170427.8fe425
version code
都为 78
;
解析:
git
提交次数)获取 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 code
static def getVersionCode(boolean isDebug) {
if (isDebug) {
return Integer.parseInt(new Date().format("yyMMddHHmm"))
}
return getRevisionNumber()
}
获取 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()
}
使用 :
versionCode getVersionCode(true)
versionName getVersionName(true)
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);
}
}
}
这里有两个版本 : 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 {
// 豌豆荚
}
}
配合 AndroidManifest.xml
使用,不同的版本可以不同的logo
和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>
配置 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 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)
}