更具體地說,我有幾個構建CONFIGS:如何實現gradle代碼版本自動遞增?
signingConfigs {
debug {
keyAlias ''
keyPassword ''
storeFile file('')
}
release {
keyAlias ''
keyPassword ''
storeFile file('')
storePassword ''
}
}
....
defaultConfig {
applicationId ""
minSdkVersion 21
targetSdkVersion 23
versionCode code
}
我想gradle這個每一次「釋放」運行時爲自動增量代碼版本。
我有什麼至今:
def code = 1;
//Get all the gradle task names being run
List<String> runTasks = gradle.startParameter.getTaskNames();
for (String item : runTasks) {
//Get the version.properties file. Its our custom file for storing a code version, please don't remove it
def versionPropsFile = file('version.properties')
def Properties versionProps = new Properties()
//This will prevent the gradle from exploding when there's no file yet created
if (versionPropsFile.exists())
versionProps.load(new FileInputStream(versionPropsFile))
//It will insert the "0" version in case the file does not exist
code = (versionProps['VERSION_CODE'] ?: "0").toInteger()
if (item.contains("release")) {
// If we're building up on Jenkins, increment the version strings
code++
versionProps['VERSION_CODE'] = code.toString()
//It will overwrite the file even if it doesn't exist
versionProps.store(versionPropsFile.newWriter(), null)
}
}
問題:
我似乎無法進去if (item.contains("release"))
。它總是虛假的,但我絕對可以看到Gradle運行這個taks。我怎樣才能解決它或至少在控制檯輸出由gradle運行的所有任務(他們的名字)?
這是一個重複http://stackoverflow.com/questions/23516090/android-studio-gradle-version-increment?rq=1? – k3b
@ k3b感謝您指出這一點。在這篇文章中接受的解決方案不適合我。讓我再檢查一次。 –