8

わかりました。新しいAndroid ビルド システムに関する Xavier Ducrohet の YouTube ビデオを見てきました。私は Android Studio の使用に切り替えましたが、満足しています。ここで、ビルド ルールをカスタマイズして、自分のやりたいように処理する必要があります。そのうちの 1 つで、マニフェスト ファイルにcodeVersionandが自動的に設定されます。codeName

Xavier は、スライドの 1 つでこれを行う方法の開始を示しています。

def getVersionCode() {
    def code = ...
    return code
}

android {
    defaultConfig {
        versionCode getVersionCode()
    }
}

それで、ドットを埋めるための良いリソースを教えてくれるような親切な人がいるでしょうか?

git describe --dirty | sed -e 's/^v//'より具体的には、 を判別しversionNamegit tag | grep -c ^vを取得するようなスクリプトを実行したいと考えていversionCodeます。

ありがとう

アップデート

gradle.build次のスクリプトを試しましたが、成功しませんでした。問題なくビルドできますが、インストールしたアプリの [アプリ情報] ページのバージョン名が変わりません。

task getVersionName(type:Exec) {
  commandLine '../scripts/version-name.sh'

  //store the output instead of printing to the console:
  standardOutput = new ByteArrayOutputStream()

  //extension method stopTomcat.output() can be used to obtain the output:
  ext.output = {
    return standardOutput.toString()
  }
}

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile project(':Common')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16

        versionName getVersionName()
    }
}

versionName getVersionName()構成を置き換えるversionName 'Some Text'と、機能し、ビルド名がSome Textアプリ情報になります。では、なぜ getVersionName 関数が機能しないのでしょうか?

更新 2

まだ機能していませんが、ほとんどです!

シェルスクリプト:

#/bin/bash

NAME=`git describe --dirty | sed -e 's/^v//'`
COMMITS=`echo ${NAME} | sed -e 's/[0-9\.]*//'`

if [ "x${COMMITS}x" = "xx" ] ; then

    VERSION="${NAME}"

else

    BRANCH=" (`git branch | grep "^\*" | sed -e 's/^..//'`)"
    VERSION="${NAME}${BRANCH}"

fi

logger "Build version: ${VERSION}"

echo ${VERSION}

これは機能し、ログ行は、プロジェクトの作成時にスクリプトが複数回呼び出されることを確認します。しかし、versionName はまだ空白のままです。まだ stdout を取得していないのは Gradle 側だと思います。

task getVersionCode(type: Exec) {
    exec { commandLine '../scripts/version-code.sh' }

    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()

    ext.output = {
        return standardOutput.toString()
    }
}

task getVersionName(type: Exec) {
    exec { commandLine '../scripts/grMobile/scripts/version-name.sh' }

    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()

    ext.output = {
        return standardOutput.toString()
    }
}

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile project(':Common')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16

        versionCode getVersionCode()
        versionName getVersionName.output()
    }
}
4

2 に答える 2

2

探し回った後、私は最終的にこれに対する解決策を見つけました。

ファイルの言語である Groovy をbuild.gradle使用すると、コマンドを簡単に実行できます。解決策は次のとおりです。

def buildCode
     = file("../scripts/version-code.sh")
         .toString().execute().text.trim().toInteger()
def buildName
     = file("../scripts/version-name.sh")
          toString().execute().text.trim()

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 12
        targetSdkVersion 16

        versionCode buildCode
        versionName buildName
    }
}

Gradle 内のすべてのファイルを参照するには、file() を使用する必要があります。

"<some-command".execute()はコマンドを実行し、への.text簡単なアクセスを提供します stdouttrim()末尾の改行を削除するには、実行する必要があることがわかりました。スクリプトで使用できecho -n ${VERSION}たと思いますtrim()が、スクリプトをコマンドラインから実行できるため、この方法の方が優れていると思います。

ビルド スクリプトは、git からのリリース タグの数をカウントするだけです。リリースに次の形式で'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ]タグを付けると、小文字の「v」で始まり、その後に任意の数字が続くタグが可能です。

#/bin/bash

git tag | grep -c ^v[0-9]

この構成でビルドする前に、少なくとも 1 つのリリース タグを作成することを忘れないでください。次の方法で、プロジェクトの開始時にタグを付けます。

$ git tag -m "Start of development" v0.0
于 2013-06-28T06:51:48.600 に答える