Continuous integration for phonegap android application

A few days back I wrote about getting started with Phonegap applications.

As a strong proponent of automated builds and continuous integration, it was imperative that I automate the building of a phonegap application that we were developing for android platform.

We were already set up with jenkins on a RHEL 6 cloud instance and I was prepared to write necessary scripts.   The first order of duty was to set up the linux box to build phonegap apps.  The steps are pretty much the same as it is for Windows, except for the installation of Android SDK.  I did phase some tricky issues which I will document in a separate write-up.

We were building an application in phonegap which would work fine on mobile browsers as well as as a native android application.  To this extent, we needed to 

a) deploy the relevant files to the web server
b) create an android release build for testing.

The web server deployment is straightforward - just copying over a bunch of html/css/js files.   So the problem boiled down to automating the creation of the android build.

As per phonegap best practice, we did not want to check-in platform-specific code.  We wanted to use cordova command-line utility to generate the android code.   This also ensured that there was no possibility of change made to the web application not making it to the android app and vice versa.

There were three cordova commands which pretty much did what we wanted.

cordova platform add android  - add android platform and copy over necessary files

cordova build android - build the android code

cordova platform delete android - remove the android platform and the files 

These commands in turned called other commands or scripts to do their job, but that was not our concern.

A simple automation for continuous integration would have been to put these lines in a shell script, check it into version control and call the same.  I chose ant though - for a couple of reasons.

a) android build itself was happening using ant
b) ant is platform-independant (so easier to test on my Windows box and then deploy it to linux)
c) flexible build tool (I am a maven fan)

It was a simple script with three tasks - clean, add-platform and build, which in turn called the cordova command-line utility.

This worked great and we could create debug builds.  (cordova build android generates debug builds).

The next step was to generate a release build.  The steps to do a release build are well documented in android developer pages.  It is either using Eclipse IDE or using ant or using a combination of ant and commands.

Four things were required for a release build -

  • keystore containing the private key and certificate for signing
  • key alias to be used
  • keystore password
  • key alias password

Once these were specified in ant.properties of the android folder, an ant release command did the job.

Since the android folder was generated in our case, we created this properties file and copied it over after the generation.  We also placed the keystore file in our repository.

Now we could do two things.

From our own ant script, call the generated ant script in android folder and run release task.

Could we do it using cordova?  Cordova command-line did not offer a way to run release build.

cordova build android --release

However, Alternatively, cordova platform add android, creates a cordova subfolder inside android folder with a bunch of scripts, one of which is build.  This had a command-line switch to do release build.  In fact, the script in turn, just invoked the generated android ant build script.

I tried both ways and both worked.   Here is the relevant build script in case someone finds it useful...

<project name="mobile-app" default="debug-build" basedir=".">
    <description>
        Build script for mobile application
    </description>

  <target name="add-platform" depends="clean">
    <!-- Add android platform -->
    <exec executable="cordova">
        <arg value="-d"/>
        <arg value="platform"/>
        <arg value="add"/>
        <arg value="android"/>
    </exec>
  </target>

  <target name="debug-build" depends="add-platform"
        description="make a debug build " >
    <!-- Build the android platform -->
    <exec executable="cordova">
        <arg value="-d"/>
        <arg value="build"/>
        <arg value="android"/>
    </exec>
  </target>

  <target name="release-build" depends="add-platform" 
      description="make a release build ">
      <copy file="ant.properties" overwrite="true" todir="platforms/android"/>
      <!-- Build the android platform -->
      <exec executable="cordova">
        <arg value="-d"/>
        <arg value="build"/>
        <arg value="android"/>
        <arg value="--release"/>
      </exec>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete android platform -->
    <exec executable="cordova">
        <arg value="-d"/>
        <arg value="platform"/>
        <arg value="remove"/>
        <arg value="android"/>
    </exec>
  </target>
</project>

Comments

  1. I have this error message trying to execute your bild for the exec cordova
    /usr/bin/env: node: No files or folders of this type
    Any idee?

    ReplyDelete
    Replies
    1. Sorry for disturbing the answer just was the fact that that the user executing the script did not have NODE_HOME in his PATH

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Opening a safe deposit locker in SBI

Opening a Kannada Word document

Automating a cordova ios build