繁体   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