2013-06-12 67 views
8

好的,我在新的Android build system上用Xavier Ducrohet觀看了YouTube視頻。我甚至轉而使用Android Studio,並對此感到滿意。現在我需要自定義構建規則以按自己的方式執行任務,其中一個自動設置清單文件中的codeVersioncodeNameAndroid Studio Gradle編程

澤維爾顯示如何做到這一點在他的幻燈片的一個開始:

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腳本沒有成功。它構建得很好,但我安裝的應用程序的App Info頁面中的版本名稱不會改變。

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'那麼它的工作原理和構建名字在App信息變得Some Text。那麼爲什麼我的getVersionName函數沒有工作?

更新2

仍然沒有工作 - 但幾乎!

shell腳本:

#/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仍然是空白的。我懷疑這是Gradle方面還沒有得到stdout的東西。

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() 
    } 
} 
+0

可能您必須將其包裝在shell腳本中,然後捕獲其輸出。請參閱:http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html –

+0

@Dhrubajyoti非常感謝您的建議。我試了一下,看看上面的內容,但還有一些關於Gradle腳本的問題,我沒有得到。 – Dobbo

回答

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 
    } 
} 

file()應該用來引用Gradle中的所有文件。

"<some-command".execute()將運行該命令,而.text可讓您簡單訪問 stdout。我發現我需要運行trim()刪除拖尾回車。我想我可以在我的腳本中使用echo -n ${VERSION},但我認爲trim()方法更好,因爲它允許腳本從命令行運行。

構建腳本只計算來自git的發佈標籤的數量。作爲標記我的發佈形式:'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ]它簡直是一個小寫的「V」後跟任意數字開始的標籤:

#/bin/bash 

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

您建立與此配置之前不要忘了至少創建一個發佈標籤。我以如下方式在項目開始時進行標記:

$ git tag -m "Start of development" v0.0 
0

您錯過了示例註釋。 versionName getVersionName.output()應該可以工作。

編輯:將您的任務代碼更改爲以下。

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

    ... 
} 

我也是新來的gradle,看來他們有一些bug或文檔缺乏。由於您試用的代碼完全來自文檔中提供的示例。

+0

感謝@Dhrubajyoti,versionName現在被替換,所以此修復程序似乎正在工作。然而,替換的文本是空白的。我的腳本有問題嗎?用「pwd」替換commandLine也會生成一個空白的版本名稱。 – Dobbo

+0

@Dobbo請檢查編輯 –

+0

感謝您的幫助到目前爲止。我現在一定很親密。但我仍然沒有輸出。如果你看「Update 2」並告訴我你的想法是錯誤的,我將不勝感激。 – Dobbo