svn号:
1、classpath增加依赖:
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'
(可查找下最新的版本)
2、在主项目(app的模块目录)下的build.gradle中增加:
android {....}
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.getRevision();
def svnNum = revision.getNumber();
println("Svn version: " + svnNum);
println("============");
return svnNum;
}
注意下,revision要本地更新后才能取得。status中还有其他的数据,例如最后提交等。需要的可自行更改。
App 版本号:
App版本号可以在 defaultConfig 中直接配,但有时候写在 manifest.中会不容易忘。
def getVerName() {
String manifestText = file("AndroidManifest.xml").getText();
def matcherVersion = Pattern.compile("android:versionName=\"([\\d.]+).*\"").matcher(manifestText);
if (matcherVersion.find()) {
def verName = matcherVersion.group(1);
println("============");
println("App version: " + verName);
return verName;
} else {
throw new GradleException('Not find versionName in AndroidManifest');
}
}
注意下 AndroidManifest 的路径,是相对与当前的 build.gradle的,标准的AS创建是在 src/main/AndroidManifest.xml
使用 可以直接 = ,或者用 ${getVerName()}
编译后输出路径:
这有很多中做法,一种是build后拷贝,一种是直接修改variants的输出路径,这里暂时先提供后面一种:
buildTypes {
release {
// .....
applicationVariants.all { variant ->
if ("release".equals(variant.buildType.name)) {// Only Release
variant.outputs.each { output ->
def targetDir = "D:/App"; // 目标路径
output.outputFile = new File(targetDir,
"App_${getVerName()}_${getSvnRevision()}.apk");
println(output.outputFile)
}
}
}
}
}