Skip to content

Instantly share code, notes, and snippets.

@twiceyuan
Last active July 29, 2019 06:56
Show Gist options
  • Save twiceyuan/b51057d3fc6139e3e51d7036e72e73ff to your computer and use it in GitHub Desktop.
Save twiceyuan/b51057d3fc6139e3e51d7036e72e73ff to your computer and use it in GitHub Desktop.
[Git Tag 生成 Android 版本号] 根据 Git Tag 的名字和数量决定 Android App 的版本名和版本号 #Android #Git
android {
    //...
    defaultConfig {
        //...
        versionCode getAppVersionCode()
        versionName getLastTagName()
        //...
    }
    //...
}

// 根据应用提交记录数来生成版本号
def getAppVersionCode() {
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'tag', '--list'
        standardOutput = stdout
    }
    return stdout.toString().split("\n").size()
}

// 获取最新的 tag 名称
def getLastTagName() {
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--abbrev=0', '--tags'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

// 输出当前版本名和版本号
task printVersionInfo() {
    print "VersionName: ${getLastTagName()}, VersionCode: ${getAppVersionCode()}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment