Gradle 脚本通过 git 标签自动执行 Android 版本控制



我卡在这个脚本上。我想实现 3 件事:

  1. 从 git 获取最新的标签并将字符串拆分为 3 个值(主要、米诺、补丁) - 每个标签都有这种格式。将获取的数据保存到属性 ext.versionMajor 等。
  2. 生成版本代码
  3. 生成版本号

我的目标是永远不要关心手动版本控制我的构建。只需通过 git 继续设置标签,此 gradle 脚本应该自动更新版本代码和版本号。

问题当我让 gradle 编译该脚本时,它失败并在第 77 行出现错误,错误只显示0

ext.versionMajor = Integer.parseInt(v[0]);

我不明白,为什么它在那里失败?我是否错误地为属性分配了值?

我不是 gradle 专业人士,如果有人知道我做错了什么,我会很高兴。

链接到脚本 1 链接到脚本 2

这是我的Adnroid项目的应用程序文件夹中的build.gradle文件的代码。

apply plugin: 'com.android.application'
ext.versionMajor = 0
ext.versionMinor = 0
ext.versionPatch = 0
ext.versionClassifier = null
ext.isSnapshot = true
ext.minimumSdkVersion = 21
//fetch version tag
setVersionNumberByTag()
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.webdesign.crf.eins"
minSdkVersion minimumSdkVersion
targetSdkVersion 25
versionCode generateVersionCode()
versionName generateVersionName()
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.volley:volley:1.0.0'
testCompile 'junit:junit:4.12'
}

private Integer generateVersionCode() {
return minimumSdkVersion * 10000000 + versionMajor;
}
private String generateVersionName() {
String versionName = "${versionMajor}.${versionMinor}.${versionPatch}"
if (ext.versionClassifier == null) {
if (isSnapshot) {
versionClassifier = "SNAPSHOT"
}
}
if (ext.versionClassifier != null) {
versionName += "-" + versionClassifier
}
return versionName;
}
private String setVersionNumberByTag() {
/*
* Gets the version name from the latest Git tag
*/
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
String verByGit = stdout.toString().trim()
String[] v = new String[3];
v = verByGit.split(".");
ext.versionMajor = Integer.parseInt(v[0]);
ext.versionMinor = Integer.parseInt(v[1]);
ext.versionPatch = Integer.parseInt(v[2]);
}

找到了解决方案

apply plugin: 'com.android.application'
ext.versionMajor = null
ext.versionMinor = 0
ext.versionPatch = 1
ext.versionClassifier = null
ext.isSnapshot = true
ext.minimumSdkVersion = 21

android {
//fetch version tag
setVersionNumberByTag()
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.webdesign.crf.eins"
minSdkVersion minimumSdkVersion
targetSdkVersion 25
versionCode generateVersionCode()
versionName generateVersionName()
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.volley:volley:1.0.0'
testCompile 'junit:junit:4.12'
}

private Integer generateVersionCode() {
return ext.minimumSdkVersion * 10000000 + ext.versionMajor * 10000 + ext.versionMinor * 100 + ext.versionPatch
}
private String generateVersionName() {
String versionName = "${versionMajor}.${versionMinor}.${versionPatch}"
if (ext.versionClassifier == null) {
if (isSnapshot) {
versionClassifier = "SNAPSHOT"
}
}
if (ext.versionClassifier != null) {
versionName += "-" + versionClassifier
}
return versionName;
}
private String setVersionNumberByTag() {
/*
* Gets the version name from the latest Git tag
*/
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
def String verByGit = stdout.toString().trim()
def (major, minor, patch) = verByGit.tokenize(".");
ext.versionMajor = Integer.parseInt(major);
ext.versionMinor = Integer.parseInt(minor);
ext.versionPatch = Integer.parseInt(patch);
}

在gradle文件中使用时髦。这意味着在java中不可能像普通人一样使用someString.split(".");。我发现,def (major, minor, patch) = verByGit.tokenize(".");做到了。

最新更新