Android Developer Tutorials and Blog

When you want to create two different builds/configurations for your development environment and your live build, it can be really annoying having to change all those constants; or you don’t have them as constants and you’ve got some horrible if else logic in your code.
Here I’ll show you how to use Ant to swap in and out your different configs when you run and build your APKs.
I’m not going to go over the inner workings of Ant because 1) I suck at it and 2) You won’t need to learn it (hopefully) it’ll just be simple and self explanatory.
So what are we going to do?

3) Switch the constants in the config file with the running of 1 Ant script

This Ant script will also create your APK, this is optional but necessary if you are going to move on to a fully automated Android build.

Ok here we go! First off I’ve created a simple application that shows a message on the screen declaring which set of constants we are running with. It also displays the version number of the app if we are running in development mode and hides this version if we are running in live mode.

This tutorial is not just about Android classes, we are going to be creating some configuration files that live outside of the build path. Here is a screenshot of the completed project for your reference:

Below is the Config.java file, this is used to keep all our app constants in one place, I’ve just used three consts but you can add anything you can think of (logging on/off, port numbers, admob keys, market variant, beta):

Config.java:

package com.blundell.tut;
// ** This build is currently using DEVELOPMENT properties **
public class Config {
public static final boolean LIVE = false;
public static final String SERVER = "http://development.server.net";
public static final String GMAPS_API_KEY = "aaaaaaa";
}

Now the trick is we mirror the Config.java on the build path with a file in a ‘template’ file that is not on the build path. We use the template with Ant to create a new file that will be copied over the original Config.java to change the properties (bear with me).

Here is what the template looks like, in the project tree structure it is /config/Config.java. The main thing to notice is the package has to match the package of the compiled Config.java.

The @ symbols are used as placeholders inbetween each pair of @’s is what we will be replacing.

Ok so we have a constants file, we have a template of this constants file, now we need a list of what to replace the @properties@ with in the template. Just a plain text file like so: (this is also kept in /config/)

The above Ant script has been commented heavily so I’ll try not to repeat myself here. If you need further explanation please leave a comment.
This build.xml can now be used to switch your configs, the below tutorial explains how to run your Ant script.

As the above link shows, to switch your configs you run either ‘buildDev’ or ‘buildLive’ this will also create you an apk in the /bin/ directory. If you just wanted to swap the configuration you could run ‘loadDevProperties’ or ‘loadLiveProperties’

As a side note, I have packaged the keystore in with the configuration. This allows you to 1) keep it in subversion, where you won’t lose it! 2) it can be used by the build.xml to automatically build your live application (You can also add your keystore password if you want full automation but not in this tutorial). Therefore if you download the demonstration project below the live build actually won’t work, but it will change your configuration.

Finally if your using Eclipse, don’t forget your F5(refresh) key to check the values have changed!

I strongly recommend you download the source project below and check it our yourself, it’s a much more visual explanation than my poor attempt at literacy.

6 thoughts on “[TUT] Switching Android build constants using Ant”

Hi, I have tried to compile the build target that you just created. buildDev & buildLive. The problem is the System tried to compile Config.java under config folder outside the src directory. And it said “Duplicate Class”. Any Idea ?

Just maybe some comment to add to your build.xml about the ” ” to replace with something else to avoid it to be override by the android update cmd (this comment is present in the default build.xml file generated).

Also a link to another tutorial with info about the ant.properties and the use of the keystore would be the perfect thing.