使用Gradle自动化生成VersionCode与VersionName,使用VersionName与git版本库关联,并且区分开发版本与发布版本的解决方案。
详情请看文章:
http://my.oschina.net/mengshuai/blog/551356
第[1]段代码
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
mavenCentral()
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
mavenCentral()
}
def getVersionCode = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', '--all', '--count'
standardOutput = stdout
}
return Integer.parseInt(stdout.toString().trim())+400
}
catch (ignored) {
return -1;
}
}
def getVersionName = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
android {
compileSdkVersion = 21
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.a.b.c"
minSdkVersion 14
targetSdkVersion 21
multiDexEnabled true
versionCode getVersionCode()
versionName getVersionName()
}
signingConfigs {
release {
storeFile file("anl.keystore")
storePassword "*****"
keyAlias "***** "
keyPassword "***** "
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.release
applicationIdSuffix 'debug'
versionNameSuffix '-debug'
}
}
compileSdkVersion 21
productFlavors {
}
dexOptions{
incremental true
javaMaxHeapSize "4g"
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
compile fileTree(include: '*jar', dir: 'libs')
compile project(':qavlib')
compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
transitive = true;
}
compile 'com.squareup:otto:+'
compile 'com.github.anrwatchdog:anrwatchdog:1.1.+'
compile 'com.alibaba:fastjson:+'
}
详情请看文章:
http://my.oschina.net/mengshuai/blog/551356
第[1]段代码
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
mavenCentral()
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
mavenCentral()
}
def getVersionCode = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', '--all', '--count'
standardOutput = stdout
}
return Integer.parseInt(stdout.toString().trim())+400
}
catch (ignored) {
return -1;
}
}
def getVersionName = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
android {
compileSdkVersion = 21
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.a.b.c"
minSdkVersion 14
targetSdkVersion 21
multiDexEnabled true
versionCode getVersionCode()
versionName getVersionName()
}
signingConfigs {
release {
storeFile file("anl.keystore")
storePassword "*****"
keyAlias "***** "
keyPassword "***** "
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.release
applicationIdSuffix 'debug'
versionNameSuffix '-debug'
}
}
compileSdkVersion 21
productFlavors {
}
dexOptions{
incremental true
javaMaxHeapSize "4g"
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
compile fileTree(include: '*jar', dir: 'libs')
compile project(':qavlib')
compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
transitive = true;
}
compile 'com.squareup:otto:+'
compile 'com.github.anrwatchdog:anrwatchdog:1.1.+'
compile 'com.alibaba:fastjson:+'
}