Android 201: How To Automate Your Build

Android 201automate

Welcome to Android 201, a series for the Android developer that already has some experience, but wants to take things to the next level.  In other words, this is not an Android 101 column focused on teaching new developers how to make an Android app.  This is a column focused on teaching developers more about Android.

Android 201: Automate Your Build

Scripting out your build process so that you can build an apk with a single click or command can be very helpful to a developer.  This can save you time when you need to make a release build or even make a beta build for a friend, or even your boss.   I recently took the time to automate the builds of a few of my own apps and I am very glad I did it.  I have one app that I set up as a library project because I have to produce 6 different versions of the application using the same code!  Why do I have 6 different versions?  Well there is a free and paid version of this app, also the application is distributed on 3 separate app stores.  Each app store has its own quirks that require a separate build.  For example, apps released on the Amazon Appstore for Android cannot mention “Android Market” or “Google Play”.  If they do have those words anywhere, then the app will not be approved for release.  So whenever I did an update or bug fix I had to go to Eclipse and generate 6 different builds to make sure that every distribution channel got the latest code.  This was a logistical nightmare for my brain to keep track of and ensure all of the AndroidManifest.xml files were updated properly and that I had saved all the apk files in the same place, plus it is just extremely monotonous to go through a wizard interface 6 times in a row to do the same thing.  I now have a single script that I can run that produces all 6 versions of the application and backs them up so that I don’t have to bother ensuring that I built new code for everyone.  I just have to submit the new package to everyone.

Step 0: Put your code into a version control system

If your code is not already in some form of version control system, you should really look into it.  I don’t care what you use, Subversion, Git, Mercurial… they all will greatly help you when you need to fix bugs in previous builds or when you need to rollback your code because your current code broke some pre existing functionality.

Step 1: Get you ant build working

The Android SDK tools will generate a working ant build script for you so take advantage of it.  if you don’t already have a buld.xml file in the root of your project folder you can regenerate the file by running the command from within your project’s root directory to update your file and your build.xml file.

{ANDROID_SDK_DIRECTORY}/tools/android update project --path .


Now that you have a build.xml file, test it.  If you don’t already have one installed, go get it and install it on your system.  You may also need to ensure that you have the environment variable ‘JAVA_HOME’ set.  Once both of those steps are done, open a terminal and navigate to your project directory.  Run the command

ant release

Ideally everything should run without issue.  To create a true release build (signed) you will also have to create an file in your project root and add the following lines to that file{path to your keystore}
key.alias={the alias in your keystore that you use to sing this app}{your keystore password}
key.alias.password={your alias password}

If those 4 lines are in your file, the ant release command should produce a build named {your project name}-release.apk in the bin folder of your project.  It is up to you if you want to put your keystore alias or password into a file under version control because multiple people may have access to the file even though only a few need to know those passwords.  I personally save those passwords to environment variables on my machine and just have the ant script reference the environment variable.  Is this practice any safer than saving code in your repository?  I am not sure, but it makes me feel better knowing that if my vcs system was compromised, my keystore would not be compromised because someone would need to also compromise my laptop in order to get the passwords.

Step 2: Update the version attributes in your AndroidManifest.xml

Building a package with 1 command is nice, but there is more to a release than just producing a package.  You must update your application version attributes whenever you submit an update, so lets automate that as well.  ant may be able to take care of this, but since I already have an idea about how to do this with python, I use python.  The two relevant attributes that need to be updated are both in the manifest node of the AndroidManifest.xml file in your project directory, android:versionCode and android:versionString.  Here is the python script that I use;

About Sean Catlin

Sean Catlin is a software developer that current spends most of his development time creating applications for mobile devices. His company snctlnSoftware ( has been publishing Android applications since December of 2008 and has had over 5 million application downloads during that time.