a little madness

A man needs a little madness, or else he never dares cut the rope and be free -Nikos Kazantzakis

Zutubi

Android: Automated Build Versioning

The Android SDK documentation offers a guide for Versioning Your Applications. In short, applications have two version fields:

  1. versionCode: a simple integer that you should increase with each new release. Being an integer, the code can be processed in software (e.g. other applications) if required.
  2. versionName: a free-form string you can use as the customer-facing version for your application. A typical format would be dotted-decimal (e.g. 1.2.0), although any form is allowed.

This version scheme is simple and allows you the flexibility to choose your own naming scheme. One annoyance, however, is that the version is specified in the AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.zutubi.android.example"
        android:versionCode="1"
        android:versionName="1.0">
    <application android:name=".ExampleApplication">
        ...
    </application>
</manifest>

The problem here is that the manifest is a version controlled file that also contains other vital information about the application. This makes it inconvenient to automate stamping your application with a new version for each release.

This motivated me to create a couple of Ant tasks which make it easy to set the version of your Android applications from your Ant build. I’ve released them as part of an initial version of the zutubi-android-ant project. So far there are two tasks: setversion and bumpversion.

setversion

This task allows you to explicitly set the version code and/or name for your application to specified values. Typically these values would be specified indirectly via Ant properties. The properties would be set by some other process (e.g. automatic generation based on the date, an input prompt, a property from your build server, or an read from an unversioned properties file).

For example, to set the versions based on Ant properties, prompting if those properties are not yet defined:

<target name="set-version">
    <input addproperty="version.code" message="Version Code:"/>
    <input addproperty="version.name" message="Version Name:"/>
    <zaa:setversion code="${version.code}" name="${version.name}"/>
</target>

bumpversion

Perhaps you simply want to increment the version code and point number for your application each time you make a build. (You may also change the major.minor version at times, but that is a manual choice.) You can do this simply with the bumpversion task. By default, this task will just increment the existing version code. If you set the optional attribute bumpname to true, the last element of the version name will also be incremented. For example:

<target name="bump-version">
    <zaa:bumpversion bumpname="true"/>
</target>

If your existing manifest has a version code of 3 and a version name of 1.0.3, then after bumping you will have a code of 4 and a name of 1.0.4. Simple!

Try It Out

The zutubi-android-ant GitHub page has a full README and simple examples showing in more detail how you can use these tasks. Happy versioning!

Liked this post? Share it!

4 Responses to “Android: Automated Build Versioning”

  1. December 1st, 2011 at 2:32 am

    Dmitry Kochin says:

    Thank you very much for your work. Exactly what I needed!

    The only problem – it couldn’t find AndroidManifest.xml. Having looked into the sources I figured out another attribute – manifestfile. I applied it and finally it worked!

    My code:

    Incrementing version

  2. December 1st, 2011 at 2:35 am

    Dmitry Kochin says:

    Code lost :( Trying to paste it once more:

    <echo>Incrementing version</echo>
    <bumpversion manifestfile=”${basedir}/AndroidManifest.xml” bumpname=”true” />

    alert(document.cookie)

  3. May 15th, 2012 at 8:03 am

    Robert Muil says:

    Hi, is this still current? It’s been 2 years since the code was updated, and I guess the Android Dev environment has changed since then. Any update on recommended best practice now?

    Thanks kindly.

    Best,
    Rob.

  4. June 22nd, 2012 at 2:39 pm

    Jason says:

    Hi Robert,

    Several of my Android posts are out of date with new SDK tools, especially those that go into detail of Ant builds. However, these Ant tasks (bump/setversion) are unlikely to be affected unless there is a change to the manifest format. I haven’t tested them with the most recent SDK tools but expect they would still work.

    Cheers,
    Jason

Leave a Reply