Showing posts with label build. Show all posts
Showing posts with label build. Show all posts

Saturday, May 16, 2009

Development branches

How can I evolve two different versions of the same application? Using the same source code? For example, if one version uses MIDP 2.0 but the other uses MIDP 1.0? The answer is preprocessing.

Since October/2003 I use Antenna: an Ant plugin that allows a developer to easily build, preverify and package a Java ME application, but it also has a pre-processor built in. To use it you need to add special line comments in your source code and call the wtkpreprocess task before calling the build task.
Below is an example of such line comments:

//#ifdef midp20
list.setBackgroundColor(display.getColor(Display.COLOR_BACKGROUND));
//#endif

ifdef means If Defined.
My build.xml Ant script will have the following lines:

<wtkpreprocess src="./src" dest="./prep" symbols="midp20"/>
<wtkbuild src="./prep" dest="./bin"/>

With this the preprocessor will match the informed symbol "midp20" with the ifdef value and keep the original code unchanged.
To differ between the two versions I will need two targets in build.xml. One for MIDP 2.0 and the other for MIDP 1.0. The first will use the sample above, but the target for MIDP 1.0 will use:

<wtkpreprocess src="./src" dest="./prep"/>
<wtkbuild src="./prep" dest="./bin"/>

Without any symbols the preprocessor will change the source code and add comment lines between ifdef and endif. The changed code at prep folder will look like this:

//#ifdef midp20
//list.setBackgroundColor(display.getColor(Display.COLOR_BACKGROUND));
//#endif

You can also use preprocessing with NetBeans Mobility Pack and Eclipse MTJ (EclipseME has moved to this project).

Related Topics:

Wednesday, October 1, 2008

Obfuscate your classes

An obfuscator can shrink and optimize your classes. Unused classes, fields, methods and attributes may be removed. The remaining code may be renamed to short meaningless names.
I use Proguard as an Ant task since 2003 and have good results with it.
Below are the settings I often use on my projects:
usemixedcaseclassnames="off", if class names reach z the next name will be aa instead of A.
defaultpackage="", all obfuscated classes are moved to the default package.
overloadaggressively="true", multiple fields and methods can get the same names, as long as their arguments and return types are different.
optimizationpasses="3", number of optimization passes to be performed. This may vary from one application to another. Check the final jar size, when it does not shrink any further, stop incresing this value.
printmapping="./mapping.txt", print the mapping from old names to new names for classes and class members that have been renamed. Useful when exceptions happen.
printusage="./deadcode.txt", list dead code of the input class files. Gives you tips of code that is not needed.
The only classes I keep are those that extends javax.microedition.midlet.MIDlet. So, for best jar size, my MIDlets are placed on default package. This way all classes are found at jar root.