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

Gradle Script to Automate Android Versioning by git tags

我遇到了这个脚本的问题。我想实现 3 件事:

  1. 从 git 中获取最新的标签并将字符串拆分为 3 个值(Major、Mino、Patch)——每个标签都将采用该格式。将获取的数据保存到属性ext.versionMajor等
  2. 生成版本代码
  3. 生成一个版本号

我的目标是从不关心手动版本控制我的构建。通过 git 继续设置标签,这个 gradle 脚本应该会自动更新 versionCode 和 versionNumber。

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

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

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

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

Link to script 1 link to script 2

这是我的Adnroid项目的app文件夹中的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 个文件中使用 groovy。这意味着它不可能像在 java 中那样正常使用 someString.split(".");。我发现 def (major, minor, patch) = verByGit.tokenize("."); 成功了。