After hunting around I finally found a solution for this.
Groovy, the language of the build.gradle
file, allows commands to be easily run. Here is the solution:
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() should be used to reference all file within Gradle.
"<some-command".execute()
will run the command, and .text
gives you simple access to
stdout
. I found I needed to run trim()
to remove the trailing carriage return. I suppose I could have used echo -n ${VERSION}
in my script, but I think the trim()
method is better as it allows the script to be run from the command line.
The build script just counts the number of release tags from git. As I tag my releases in the form: 'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ]
it just could the tags that start with a lower case 'v' followed by any digit:
#/bin/bash
git tag | grep -c ^v[0-9]
Before you build with this configuration don't forget to create at least one release tag. I tag right at the start of the project in the following way:
$ git tag -m "Start of development" v0.0