Wednesday, June 17, 2009

Java ME automated build

So far I have built my Java ME application using Sun Java ME SDK 3.0 and before the SDK was available with Sun Wireless Toolkit. But now we need to streamline the process and introduce automated build.

The tool we use for this is ant, with an add-on for Java ME called antenna. Antenna is an open source project, which provides support for operations like obfuscating, updating manifest and JAD files and signing.

Setup
To use antenna you need to setup the antenna tasks, using taskdef, and define some properties:

<taskdef resource="antenna.properties" classpath="${basedir}\tools\antenna.jar" />
<property name="wtk.home" location="C:\WTK2.5.2"/>
<property name="wtk.cldc.version" value="1.1"/>
<property name="wtk.midp.version" value="2.0"/>
<property name="wtk.proguard.home" value="${basedir}\tools\proguard4.3"/>

As you can see, we have a directory called tools, where we keep the antenna jar and also a proguard installation, which we need for obfuscation.
Antenna also needs to know where your wireless toolkit is installed and which versions of CLDC and MIDP you use. That's it!

Package
Antennas main function is packaging your application into jar and jad files, preverified, obfuscated and with correct manifest and jad files. You can do this step-by-step, with different tasks for each step, but you can also use the wtkpackage task to make them all.This is the approach we have taken.

<target name="package" depends="compile">
<mkdir dir="${dist.dir}"/>
<copy file="${basedir}/myapp.jad" overwrite="true" failonerror="true" tofile="${dist.dir}/myapp.jad"/>

<wtkpackage jarfile="${dist.dir}/myapp.jar"
jadfile="${dist.dir}/myapp.jad"
obfuscate="true"
preverify="true">

<fileset dir="${class.dir}"/>
<fileset dir="${res.dir}"/>

<exclude_from_manifest name="myapp-dir"/>
<exclude_from_manifest name="myapp-host"/>

</wtkpackage>
<copy file="${dist.dir}/myapp.jad" overwrite="true" failonerror="true" tofile="${dist.dir}/myappu.jad"/>

</target>

Comments:
  1. compilation is separate, our package task is made after the compilation
  2. we keep a jad template in the project basedir, which we copy to the dist dir
  3. wtkpackage will create the jar file and update the jad file. We also want it to preverify and obfuscate using proguard
  4. in ${class.dir} we have all classes that should be included
  5. all used resources are in ${res.dir}
  6. we have two parameters in our JAD template that should be only in the JAD file and not in the Manifest file. This allows for changing them without resigning.
  7. as a last step we copy the jad file. This is to have an unsigned copy for use in emulators and phones that don't support the Verisign certificate we use (Nokia S40..)
Signing the application
Antenna also supports signing using the very straightforward wtksign task:

<target name="sign" depends="package">
<wtksign
keystore="${basedir}/../cert/keystore.ks"
jarfile="${dist.dir}/myapp.jar"
jadfile="${dist.dir}/myapp.jad"
storepass="******"
certpass="******"
certalias="myalias"
/>

</target>


This will add signature information to the JAD file using the certificate in the keystore and the JAD and jar files are redy to use.

No comments: