Building Android APK’s with TeamCity

24 July 2010 , , ,    1 Comment

I’m slowly getting my various Android projects up to bitbucket, codeplex or another open/public DVCS and I’m slowly making sure that all of them can have auto-magic continuous integration. As my Windows Home Server is running TeamCity for MahTweets, its natural I want to keep it all contained in that.

Originally I was trying to use the Ipr build runner, as I’m currently using IntelliJ IDEA, which is picked up by TeamCity rather well except it only generates a bunch of .classes (so it is compiling) but none of the dex/APK’s required for Android.

The only solution I could find is to use Ant!

Generating the Build.Xml

When I created projects in NetBeans, Eclipse or IntelliJ, none of them created the Build.xml file for your project. Luckily the Android SDK does.

You’ll find "android" under the tools subfolder in the SDK. From a command line:

android create project –n <ProjectName> –t <Target> –p <PathOnDesk> –k <Package> –a <Activity>

All values are required. As I’d already created my project, I stuck in some dummy values and just grabbed the build.xml out of that folder, and placed it into my project folder. I used:

android create project –n OhHai –t 1 –p D:\OhHai\ –k MahApps.OhHai –a OhHaiActivity

Make sure you commit the build.xml to your VCS!

BuildAgents

Your BuildAgents are going to have to have a few extra requirements – primarily the Android SDK as well as the SDK for the particular version you are building against (run the SDKSetup inside the SDK download).

Once you’ve done that, you need to add a property to your BuildAgent to point to the location of the SDK you’ve just downloaded. Edit your BuildAgent configuration (by default on Windows, its C:\BuildAgent\conf\buildAgent.properties) by adding

android.platform.base=pathToSDK

To the end of the file. This will disconnect and then reconnect your BuildAgent from your BuildServer. For me, it was

android.platform.base=D\:\\SDKs\\android-sdk-windows

You’ll notice this isn’t the path to the individual SDKs, and that’s because the ant tools build.xml calls are under <SDKRoot>/tools/lib

New Configuration

Most of the values can be configured as normal, ie, give it a name, select your VCS, trigger, dependencies etc There are only a few extra values you’ll need to throw in

  1. General Settings
    Artifact path: /bin/YourProjectName-debug.apk
  2. Runner
    1. Select Ant as the build runner
    2. put build.xml as the path to your build file (unless you’ve changed the name of it)
    3. put ‘debug’ as your targets (unless you want another build target)
    4. Add an Additional Ant command line parameters, put in -Dsdk.dir="%android.platform.base%"
      Ant properties are passed in via the command line as –DparamName=ParamValue.

Personally, I’ve added an additional Configuration Parameter Requirement, of android.platform.base to make sure only the BuildAgents with it configured attempt to build it.

Voila, build the project (and providing it doesn’t have compile issues), your APK should be the only file in your artifacts!