1. 版本号
目标生成的 version name
如下所示 :
1.0.78.170427.153740.8fe425
1.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
日期
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
:
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)
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)
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)
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
使用,不同的版本可以不同的logo
和name
:
<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 = [
]
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
defaultConfig {
applicationId appConfig.appId
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
versionCode getVersionCode(true)
versionName getVersionName(true)
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)
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)
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')
}
static def getVersionCode(boolean isDebug) {
if (isDebug) {
return Integer.parseInt(new Date().format("yyMMddHHmm"))
}
return getRevisionNumber()
}
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()
}
static def getRevisionNumber() {
Process process = "git rev-list --count HEAD".execute()
process.waitFor()
return process.getText().toInteger()
}
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
完