简体   繁体   English

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

[英]Gradle Script to Automate Android Versioning by git tags

I got stuck on a Problem with this script. 我陷入了这个脚本的问题。 I want to achieve 3 things: 我想实现三件事:

  1. fetch the latest tag from git and split the string up into 3 values (Major, Mino, Patch) - Every tag will have that format. 从git中获取最新标签,并将字符串分成3个值(Major,Mino,Patch)-每个标签都将具有该格式。 Save the fetched data to the properties ext.versionMajor etc. 将获取的数据保存到属性ext.versionMajor等。
  2. generate a versionCode 生成一个versionCode
  3. generate a versionNumber 生成一个versionNumber

My goal is to never care about versioning manually my builds. 我的目标是永远不要在乎手动构建版本。 By just keep setting tags via git, this gradle script should automatically update the versionCode and versionNumber. 通过仅通过git设置标签,此gradle脚本应自动更新versionCode和versionNumber。

The Problem When I let gradle compile that script it fails with an Error on Line 77 and the Error just says 0 . 问题当我让gradle编译该脚本时,它失败并在第77行出现错误,错误仅显示0

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

I don´t get it, why does it fail there? 我不明白,为什么它在那里失败? Am I assigning the value wrong to the properties? 我是否将值错误分配给属性?

Im not a gradle pro, I would be really happy if someone has an idea what I am doing wrong. 我不是gradle专业人士,如果有人知道我在做什么错,我会很高兴。

Link to script 1 link to script 2 链接到脚本1 链接到脚本2

Here is the code of the build.gradle file in the app folder of my Adnroid Project. 这是我的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]);
}

found a solution 找到了解决方案

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);
}

In gradle files groovy is used. 在gradle文件中使用groovy That means its not possible to use someString.split("."); 这意味着不可能使用someString.split("."); like normal in java. 像在Java中一样。 I found out, that def (major, minor, patch) = verByGit.tokenize("."); 我发现def (major, minor, patch) = verByGit.tokenize("."); did the trick. 做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM