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

时间:2017-09-02 14:08:11

标签: android git gradle build.gradle versioning

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

  1. 从git中获取最新的标签并将该字符串拆分为3个值(Major,Mino,Patch) - 每个标签都具有该格式。将获取的数据保存到属性ext.versionMajor等。
  2. 生成一个versionCode
  3. 生成versionNumber
  4. 我的目标是永远不关心手动版我的版本。只需通过git设置标签,这个gradle脚本应该自动更新versionCode和versionNumber。

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

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

    我不明白,为什么会失败呢?我是否为属性赋值错误了?

    我不是一个gradle pro,如果有人知道我做错了什么,我会很高兴。

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

1 个答案:

答案 0 :(得分:0)

找到了解决方案

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(".");做了伎俩。