簡體   English   中英

Git中的Android構建版本自動化

[英]Android build version automation in Git

我曾經使用以下Gradle腳本在Subversion中自動構建版本:

import org.tmatesoft.svn.core.wc.*

buildscript {
    dependencies {
         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.7.11'
    }
}

def getSvnRevision() { //getting SVN revision #
    ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
    SVNClientManager clientManager = SVNClientManager.newInstance(options);
    SVNStatusClient statusClient = clientManager.getStatusClient();
    SVNStatus status = statusClient.doStatus(projectDir, false);
    SVNRevision revision = status.getRevision();
    return revision.getNumber();
}

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        versionName = "1.0." + getSvnRevision()
        versionCode = 1000000 + getSvnRevision()
    }
}

這個腳本的本質是從SVN repo當前版本號獲取並將其用作版本#來生成AndroidManifest.xml ,例如:

<!--
 Version number encoding: XXYYZZZ
    XX 2 digits - major version
    YY 2 digits - minor version
    ZZZ 3 digits - build (SVN revision)
    Example: 0123456 is version 1.23.456
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package"
    android:versionCode="1000650"
    android:versionName="1.0.650" >

這使我的工作變得更加輕松,例如,當我收到錯誤報告時,可以輕松找到有問題的修訂。 尤其重要的是,如果一個人有多個測試人員/分發點並上傳了不同的版本。

現在問題:

如何使用Git做類似的事情? 在Git中,沒有單個修訂版本號,而是僅存在分支的哈希-但它是字母數字-Android允許僅使用整數作為版本代碼。

最后,我知道了如何做。 這是代碼:

def getGitRevision = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            standardOutput = stdout
            commandLine 'git', 'rev-list', '--first-parent', '--count', 'master'
        }
        println("Building revision #"+stdout)
        return asInteger(stdout.toString("ASCII").trim())
    }
    catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

android {
    /*
    Version number encoding: XXYYZZZB
    XX 2 digits - major version
    YY 2 digits - minor version
    ZZZ 3 digits - build (VCS revision)
    B 1 digit - build variation/patch
    Example: 01234561 is version 1.23.456a
    */

    def char[] patches='abcdefghijklmnopqrstuwxyz'.toCharArray()
    def majorVersion=1
    def minorVersion=3
    def revision=getGitRevision()
    def patch=0
    defaultConfig {
        applicationId "com.my.package"
        versionName = majorVersion + '.' + minorVersion + '.' + revision + patches[patch]
        versionCode = 10000000*majorVersion+10000*minorVersion + 10*revision+patch
    }

我使用了一個shell腳本來生成版本,並將其作為環境變量注入:


# the build command (bash) in CI server

# versionCode: number of commits that are reachable from here
# *supposedly* monotonically increasing for each release build
export ANDROID_VERSION_CODE="$(git rev-list HEAD --count)"
# versionName: first 8 chars in commit SHA1
export ANDROID_VERSION_NAME="${GIT_COMMIT:0:8}"

// build.gradle

// read env variable as string or integer
def env = { System.getenv it }
def envInt = { Integer.parseInt(env(it)) }

android {
    defaultConfig {
        if (env("ANDROID_VERSION_CODE")) {
            versionCode envInt("ANDROID_VERSION_CODE")
            versionName env("ANDROID_VERSION_NAME")
        } else {
            // fallback
            versionCode 1
            versionName "1.0"
        }
    }
}

暫無
暫無

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

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