I just did this by a simple string replace with this task:
task setAndroidManifestVersion << {
logger.info "Setting Version to $version.full in Android Manifest at $androidManifest"
content = file(androidManifest).getText()
content = content.replace("##DEV#SNAPSHOT##", project.version.full)
content = content.replace('versionCode="0"', 'versionCode="' + project.version.build + '"')
file(androidManifest).write(content)
}
androidManifest is the property containing the location of the mainfest file, like "AndroidManifest.xml"
Your AndroidManifest.xml needs to look something like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package"
android:versionCode="0"
android:versionName="##DEV#SNAPSHOT##">
version is a custom version object, but you can also use 2 different properties.
Note that this is from a pre-1.0 Gradle project, so now (Gradle 1.2) you would need to have properties like this:
project.ext.versionName and project.ext.versionCode ...
Hope that helps.
This way, I was able to add the CI build number as a version code.
(btw: I saw that the new Android SDK Gradle-based build system has a mechanism with properties to do this, so this might be worth a look...)
Best regards
Stefan