Monday, January 7, 2013

Android: How to version command line build?

Philosophy behind Continues Integration is, integrate often, build often, run often and test often. This post is about “build often” part. To build often, you need to have a slick build process. If you have one simple command line to build your entire project that may encourage you and also other developers in your team to build often.

If you are building android project in command line and not sure how to update the version code and version name attributes in AndroidManifest.xml file, this might help.

In your build.xml file, you should see this line.

This custom_rules.xml, helps you to keep your customization in a separate place so when build.xml file is changed by Android Team, you don’t have to worry about merging. If you already have one in place you are good to go, if not create it.

Initially your custom_rules.xml should look like this,

You can pass version info in two ways,

  • Using a property file
  • Using command line arguments

To pass version info using property file,

Create this target in custom_rules.xml file,

And create this file,

To pass version info using command line arguments,

Create this target in custom_rules.xml file,

In the above target, we are using variable Version.Code and Version.Name. Lets pass these variables from command line,

Now we have the targets to update the version info. This targets needs to be called from somewhere. You could do this in several ways. 1. custom_rules.xml has several hooks such as pre-build, post-build. We can call our desired target in pre-build, so that version info gets updated every time we build it. 2. You can call the target from command line right before you build your app like the code below.

Complete custom_rules.xml file,