Creating deployable MIDlets with free software

So what we want to do is create a MIDlet for running on a Mobile phone that supports Java ME (Mobile Edition) without using any proprietary software. Assumed is you have a working Java 6 environment, for example OpenJDK. Ingredients:

Traditionally one uses Sun’s Wireless Toolkit (WTK), but this has limited availability on 64 bit platforms, or even OS X. As an extra disadvantage it is non-free software (Update: the preverifier is available as GPL source as part of the PhoneME project. I tried to compile this on a 64 bit machine but it didn’t seem to work, I gave up for now.)
The first step is to configure Eclipse with the Eclipse MTJ plugin and make it use MicroEmulator as it’s SDK and find the relevant devices. This way the Java code can be verified while coding. Eclipse will take care of generating the class files that need to be packaged and preverified. Also the JAD file will be more or less available in Application Descriptor, minus the MIDlet-Jar-Size line. Integration with the preverification of ProGuard seems to not be included (yet) so for now it can be done using some command line tools (yeah, great…)

First we create the JAR file with the classes generated by Eclipse in them (run this from withing the project’s directory in the workspace):

jar -cfm MIDletName_not_preverified.jar Application\ Descriptor -C bin/ .

Now this needs to be preverified with ProGuard. A configuration file for ProGuard would look like this:

-injars MIDletName_not_preverified.jar
-outjars MIDletName.jar
-libraryjars [MicroEmulator Directory]/lib/midpapi20.jar
-libraryjars [MicroEmulator Directory]/lib/cldcapi11.jar
-repackageclasses ''

-keep public class * extends javax.microedition.midlet.MIDlet

You can of course add more libraryjars, for example for Bluetooth. Proguard is started like this:

java -jar [ProGuard Directory]/lib/proguard.jar @proguard.config

where proguard.config is the file described above. Now you can take Application Descriptor and add the JAR file size to it:

cp "Application Descriptor" MIDletName.jad
echo MIDlet-Jar-Size: `ls -l MIDletName.jar | awk {'print $5'}` >> MIDletName.jad

And now you can run it in the emulator:

java -jar [MicroEmulator Directory]/microemulator.jar MIDletName.jad

See this if you want to use this to test Bluetooth MIDlets. This was of great use for creating MIDlets with Bluetooth functionality!

Other interesting links: Antenna, a J2ME build system for Ant. It seems to have a “hard” dependency on SDK’s like the one from Sun looking for a bin/preverify so this was not of much use. Then there is the Maven J2ME Plugin which is much more interesting for serious projects, and it does support ProGuard as preverifier.

Hopefully someday soon support for ProGuard as preverifier can be added to Eclipse MTJ 🙂 Maybe I’ll work on getting ProGuard, MicroEmu and MTJ included in Fedora if BlueCove ever gets reviewed by someone 😉

Update: In case you want to use Bluetooth you can add -libraryjars [MicroEmulator Directory]/lib/microemu-jsr-82.jar. In case you want to use the BouncyCastle crypto library (look for a file named lcrypto-j2me-[version].zip) you can add -injars [BouncyCastle J2ME directory]/zips/ When using injars the code in there will also be obfuscated, and only the classes you need will be included in the output JAR file.