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
-overloadaggressively
-repackageclasses ''
-allowaccessmodification
-microedition
-printseeds

-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/cldc_classes.zip. 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.

2 thoughts on “Creating deployable MIDlets with free software

  1. Pingback: Easy MIDlet Suite construction « François’ Weblog
  2. This has been such a precious resource to me, even years later! Thank you for thinking so progressively even back then! I am having so much fun hacking J2ME with only Free tools now! I was faced with the prospect of possibly giving up on J2ME but found yours and others’ sites as well as hearing of phoneME (which still eludes me…)
    I have not been able to do the final processing of the JAD file to add the size, but making the .JAR with Proguard works like a charm. I have also found that exporting a midlet package from Eclipse (without checking obfuscate) makes a runnable .jar and .jad file automatically. The .jar’s processed by Proguard, however, are always smaller.

    Once again, thank you for hacking this so very long ago.

    -Jane

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s