好的,我在新的Android build system上用Xavier Ducrohet觀看了YouTube視頻。我甚至轉而使用Android Studio,並對此感到滿意。現在我需要自定義構建規則以按自己的方式執行任務,其中一個自動設置清單文件中的codeVersion
和codeName
。Android Studio Gradle編程
澤維爾顯示如何做到這一點在他的幻燈片的一個開始:
def getVersionCode() {
def code = ...
return code
}
android {
defaultConfig {
versionCode getVersionCode()
}
}
所以可能有人會這麼好心點我很好的資源在點填充?
更具體的我想運行一個腳本像git describe --dirty | sed -e 's/^v//'
確定versionName
和git 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()
}
}
可能您必須將其包裝在shell腳本中,然後捕獲其輸出。請參閱:http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html –
@Dhrubajyoti非常感謝您的建議。我試了一下,看看上面的內容,但還有一些關於Gradle腳本的問題,我沒有得到。 – Dobbo