2017-09-02 91 views
0

我遇到了這個腳本的問題。我想實現3件事:通過git標籤自動化Android版本的Gradle腳本

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

我的目標是從來不計較手動版本我的構建。通過保持通過git設置標籤,這個gradle腳本應該自動更新versionCode和versionNumber。

的問題 當我讓gradle這個編譯腳本失敗與錯誤第77行和錯誤只是說。

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

我不明白,爲什麼它在那裏失敗?我是否將錯誤的值分配給屬性?

我不是一個gradle pro,如果有人知道我做錯了什麼,我會很開心。

Link to script 1 link to script 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]); 
} 

回答

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