[英]Android build version automation in Git
I used to automate build version in Subversion using following Gradle script: 我曾经使用以下Gradle脚本在Subversion中自动构建版本:
import org.tmatesoft.svn.core.wc.*
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
classpath 'com.android.tools.build:gradle:1.0.0'
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.7.11'
}
}
def getSvnRevision() { //getting SVN revision #
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNRevision revision = status.getRevision();
return revision.getNumber();
}
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
versionName = "1.0." + getSvnRevision()
versionCode = 1000000 + getSvnRevision()
}
}
Essense of this script is to get from SVN repo current revision # and use it as version # to generated AndroidManifest.xml
, like: 这个脚本的本质是从SVN repo当前版本号获取并将其用作版本#来生成
AndroidManifest.xml
,例如:
<!--
Version number encoding: XXYYZZZ
XX 2 digits - major version
YY 2 digits - minor version
ZZZ 3 digits - build (SVN revision)
Example: 0123456 is version 1.23.456
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package"
android:versionCode="1000650"
android:versionName="1.0.650" >
It makes my life significantly easier, eg when I receive bug report I can easily locate revision with issue. 这使我的工作变得更加轻松,例如,当我收到错误报告时,可以轻松找到有问题的修订。 It really matters especially in case when one has several testers/distribution points where different versions are uploaded.
尤其重要的是,如果一个人有多个测试人员/分发点并上传了不同的版本。
Now question: 现在问题:
How can I make something similar with Git? 如何使用Git做类似的事情? In Git there's no single revision #, instead there's only hash of branch - but it's alphanumeric - Android permits usage only of integers as version code.
在Git中,没有单个修订版本号,而是仅存在分支的哈希-但它是字母数字-Android允许仅使用整数作为版本代码。
In the end I've figured how to do it. 最后,我知道了如何做。 Here's code:
这是代码:
def getGitRevision = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
standardOutput = stdout
commandLine 'git', 'rev-list', '--first-parent', '--count', 'master'
}
println("Building revision #"+stdout)
return asInteger(stdout.toString("ASCII").trim())
}
catch (Exception e) {
e.printStackTrace();
return 0;
}
}
android {
/*
Version number encoding: XXYYZZZB
XX 2 digits - major version
YY 2 digits - minor version
ZZZ 3 digits - build (VCS revision)
B 1 digit - build variation/patch
Example: 01234561 is version 1.23.456a
*/
def char[] patches='abcdefghijklmnopqrstuwxyz'.toCharArray()
def majorVersion=1
def minorVersion=3
def revision=getGitRevision()
def patch=0
defaultConfig {
applicationId "com.my.package"
versionName = majorVersion + '.' + minorVersion + '.' + revision + patches[patch]
versionCode = 10000000*majorVersion+10000*minorVersion + 10*revision+patch
}
I used a shell script to generate the version, and inject it as environment variables: 我使用了一个shell脚本来生成版本,并将其作为环境变量注入:
# the build command (bash) in CI server
# versionCode: number of commits that are reachable from here
# *supposedly* monotonically increasing for each release build
export ANDROID_VERSION_CODE="$(git rev-list HEAD --count)"
# versionName: first 8 chars in commit SHA1
export ANDROID_VERSION_NAME="${GIT_COMMIT:0:8}"
// build.gradle
// read env variable as string or integer
def env = { System.getenv it }
def envInt = { Integer.parseInt(env(it)) }
android {
defaultConfig {
if (env("ANDROID_VERSION_CODE")) {
versionCode envInt("ANDROID_VERSION_CODE")
versionName env("ANDROID_VERSION_NAME")
} else {
// fallback
versionCode 1
versionName "1.0"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.