Gradle 获取svn版本号
- Project层下的 * build.gradle * 文件
buildscript {
repositories {
maven{ url 'http://maven.oschina.net/content/groups/public/'}//OSC的maven仓库镜像
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11' //引入svn插件
}
}
- Module层下的 ** build.gradle ** 文件 加入以下
- 第一行导包
import org.tmatesoft.svn.core.wc.*
- 定义函数
def getSvnRevision() {
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNRevision revision = status.getCommittedRevision();
return revision.getNumber();
}
附
自己项目中使用的Gradle文件
- project.build
buildscript {
repositories {
maven{ url 'http://maven.oschina.net/content/groups/public/'}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'
}
}
allprojects {
repositories {
maven{ url 'http://maven.oschina.net/content/groups/public/'}
jcenter()
mavenCentral()
maven {
url "https://jitpack.io"
}
}
}
- moudle.build
import org.tmatesoft.svn.core.wc.*
apply plugin: 'com.android.application'
//配置android annotations
//apply plugin: 'com.neenbedankt.android-apt'
def getSvnRevision() {
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNRevision revision = status.getCommittedRevision();
return revision.getNumber();
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "cn.mobilizer.wenxun"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0.2.${getSvnRevision()}"
ndk {
//设置支持的SO库架构
abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
}
//签名文件最好不放在这里
signingConfigs {
release {
keyAlias 'release'
keyPassword 'release'
storeFile file('sign/release.keystore')
storePassword 'release'
}
}
buildTypes {
release {
println 'release -- build'
//自定义字段
buildConfigField "boolean", "LOG_DEBUG", "false"
//混淆
minifyEnabled false
//移除无用的res文件(必须混淆)
shrinkResources true
zipAlignEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//签名文件
signingConfig signingConfigs.mobilizer
//对生成的apk 进行重命名,及制定到相对目录
applicationVariants.all { variant ->
println "release baseName --> ${variant.baseName}" //xxx-release,xxx-debug
println "release name --> ${variant.name}"
variant.outputs.each { output ->
def outputFile = output.outputFile
println "release outputFile.name --> ${outputFile.name}"
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName;
if (variant.baseName.endsWith('release')) {
fileName = "xxxxx_${versionName}.apk"
} else if (variant.baseName.endsWith('debug')) {
fileName = "xxxxx_debug_${versionName}.apk"
}
def rootDir = (new File(outputFile.parent)).parentFile.parentFile.parentFile;
output.outputFile = new File("${rootDir}/apk/${getSvnRevision()}", fileName)
println "release after change outputFile.name --> ${outputFile}"
}
}
}
}
debug {
buildConfigField "boolean", "LOG_DEBUG", "true"
signingConfig signingConfigs.xxxxx
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res', 'src/main/res-lib', 'src/main/res-config']
}
}
productFlavors {
play {
}
gfan{
}
//...
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
}