Skip to content

Proguard as alternative to precompiler

With all the variety of JSRs and their availability on different devices, porting of mobile applications ends up being quite a challenge. The traditional approach is to use precompiler, that is still one of the best options and in my mind is the last tool that I use. What I ended up using to turn on/off application features for different ports, is to use public static final boolean constants to flag weather the feature is available on this phone, here is the example:

….. MyMidlet.java….

import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;

public Vector getContacts() {

if (PhoneConstants.JSR75_INCLUDED) {

// retrieve the contacts ….

} else {

return null;
}

During the build time the PhoneConstants file is generated and the TRUE or FALSE constant is set. Then once the obfuscator runs (Proguard in my case), if PhoneConstants.JSR75_INCLUDED is set to false, the obfuscator completely removes the block of code that does access to the contacts, and because there in no longer any code that uses PIM api, all the imports are dropped as well.

The beauty of this approach is the readability and maintainability of the code.

technorati tags: j2me porting proguard

UPDATE:

As a response to the comment here is one way to structure your build:

  1. Create properties file for each device, in the properties file define all the configuration.
  2. in build.xml, load ${device}.properties
  3. create target “constants”
    1. copy all “src” to “tmpsrc”
    2. for each property run replaceregexp
  4. compile using tmpsrc

One Comment

  1. bobes wrote:

    That is an excelent idea. I’d like to know though how are your generating the constants file. I’d like to create several jars for different phone vendors/models and I’m looking for the best way to do this. Thanks for the idea…

    Friday, December 21, 2007 at 4:28 am | Permalink

Post a Comment

You must be logged in to post a comment.