簡體   English   中英

用於通過git標簽自動執行Android版本控制的Gradle腳本

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

我陷入了這個腳本的問題。 我想實現三件事:

  1. 從git中獲取最新標簽,並將字符串分成3個值(Major,Mino,Patch)-每個標簽都將具有該格式。 將獲取的數據保存到屬性ext.versionMajor等。
  2. 生成一個versionCode
  3. 生成一個versionNumber

我的目標是永遠不要在乎手動構建版本。 通過僅通過git設置標簽,此gradle腳本應自動更新versionCode和versionNumber。

問題當我讓gradle編譯該腳本時,它失敗並在第77行出現錯誤,錯誤僅顯示0

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

我不明白,為什么它在那里失敗? 我是否將值錯誤分配給屬性?

我不是gradle專業人士,如果有人知道我在做什么錯,我會很高興。

鏈接到腳本1 鏈接到腳本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 這意味着不可能使用someString.split("."); 像在Java中一樣。 我發現def (major, minor, patch) = verByGit.tokenize("."); 做到了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM