Sunday, September 25, 2011

Automating Builds on Android - Part 2

In the previous post, Automating Builds on Android - Part 1, we saw how to setup our projects to automate the build process. In this post, we will automate the process further to make Ant automatically use the passwords for our keystore and alias, label our builds to whatever name we like and put it in a specific location on your system (from where others can access it).

1. To make Ant use the passwords for our keystore automatically, we will create a separate properties file in our project folder where. Let's name it "passwords.properties". This file would contain two properties as listed below.
key.store.password=sample
key.alias.password=sample
2. We will need to make a small change in our build.xml file to tell Ant to load this property file while building. To do this, we simply add this one line in the build.xml file.
<property file="passwords.properties" />
If you try to run "ant release" command in your terminal, you will notice that Ant doesn't ask you to enter your passwords anymore. It has picked up the passwords from your passwords.properties file.

3. Adding a name and a time-stamp to your builds while making a release build needs a little more effort. We will now create another properties file called "extras.properties". This file would contain two more properties as listed below.
file_name_format=test_build_hello_world
build_location=C:/Builds/HelloWorld
The file_name_format would be used to name your output builds. Sometimes, you would want to name your builds differently based on whether it is a "daily_build" or "weekly_build" or a "release_build". Instead of renaming the output file manually, you could just change this property accordingly just before your type in "ant release", and your builds would automatically be named as you like. The other property, build_location is again a configurable value here, where Ant would put all your builds in the specified location. This could be a shared folder on your system from where anyone else can access it easily.

4. Now comes the lengthy part. To accommodate these changes in our build process, our original build.xml is not enough. We will have to tweak the actual underlying build.xml to make our build process more streamlined. Let's see how to do it. Pick up the build.xml from the sample project and replace the previous build.xml with this one. It will have a whole lot of Ant scripts. You don't have to worry about most of it, except for these lines.

    <!-- Date/Time format for naming the file-->
    <tstamp>
        <format property="time" pattern="dd_MMM" />
    </tstamp>
    <property file="local.properties" />
    <property file="passwords.properties" />
    <property file="extras.properties" />

Notice, that, we are using a time-stamp property which we would be using in our build.xml to version our builds based on the specific format. Our builds will be names as "test_build_hello_world_22_SEP.apk".

The last thing you need to change is this piece of code. You will find it somewhere in the new build.xml.

    <property name="out.release.file.name" value="${file_name_format}_${time}.apk" />
    <property name="out.release.file" location="${build_location}/${out.release.file.name}" />

5. Here, you can see that we have changed the property "out.release.file.name" to a value that we want the output file to be named as. And also, we changed the "out.release.file" to use our configured directory from the extras.properties file.

The final step is to run "ant release" for the last time, and you will see your final release build placed at your desired location.

You can find the sample project here.

2 comments:

  1. You should make sure passwords.properties is excluded from your repository, for example by listing it in .gitignore.

    ReplyDelete
  2. @Lawrence: You are right. There are a few more files which should not be pushed into your source control system, but that's perhaps another post for me. :)

    ReplyDelete