Substituting version in AndroidManifest.xml during build

65 views
Skip to first unread message

Nathan

unread,
Oct 30, 2012, 7:26:37 AM10/30/12
to gradle-android-p...@googlegroups.com
I want to define my application version outside of the AndroidManifest.xml. This is explicitly mentioned as a limitation in the readme so I'm wondering if anyone has any ideas for a workaround. I already attempted to generate this file as part of my build but the android plugin fails to be applied when the manifest file is missing. Would it be possible to make the android plugin not require the manifest while being applied on a project?

 - Nathan

Stefan Simroth

unread,
Nov 1, 2012, 8:07:00 AM11/1/12
to gradle-android-p...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages