Oct 28, 2011

J2ME JAR size optimization: Decrease your JAR's size

J2ME devices have JAR File restrictions and this could vary from 32 KB to 200KB and over. Depending on the devices your application is ported to, JAR sizes have to be decreased, here are some of the tricks you can use for decreasing the JAR file sizes. Never start coding with optimization in mind. Only optimize at the fag end of the project after all code readability is also important. Intermediate device deployments can also be done to check the device compatibility and to get a fair idea of how your application works on the target devices. Thumb rule for coding J2ME applications is modular programming approach. Never use design patterns when the target devices consist of low end devices. We may also think about two code bases one for high end devices with colorful images etc and another for low end devices with bare bone functionality.
Obfuscation is a technique used not only to protect make your source code unreadable but also comes with added advantage of smaller JAR file memory footprint. The most popular open source obfuscators are
 
ProGuard and RetroGuard.
Reduce methods if possible. Condense multiple methods to a single method.
Reduce try-catch blocks to bare minimum.
Use lower versions of JDK while packaging JAR files, JAR File size created using JDK 1.3 is 5% to 20% lesser than those created with JDK1.4.
Remove redundant features. Some times you may have to sacrifice some of the essential features.

Split your application to multiple JAR files or services. 
Reduce number of classes. See if some of the classes can be clubbed together into a single class.
Remove Array instantiations and use Strings instead. This reduces class file size.
Ask your graphic designer if he can reduce the image sizes.

Unlike desktop or web applications J2ME comes with the extra baggage of file sizes. Make sure at the design stage all these issues are taken care and a careful analysis should be done regarding the JAR file sizes.

No comments: