Java Reference
In-Depth Information
Only Include Classes You Need
You may be using third-party packages in your MIDlet suite, like a cryptography package (see
Chapter 18). For development, you might have simply dumped the whole package into your
MIDlet suite. But come deployment time, you should prune out the excess packages to reduce
the size of your MIDlet suite JAR. In some cases this will be fairly easy, like dumping out OBEX
classes if you're only using the Bluetooth support. Other times it will not be so obvious which
classes you need and which ones you can get rid of. However, if you really want to reduce your
MIDlet suite JAR size, this is a crucial step. You don't need to do this by hand; an obfuscator will
do the work for you.
Use an Obfuscator
Finally, a bytecode obfuscator can reduce the size of your class files. A bytecode obfuscator is a
tool that is supposed to make it difficult to decompile class files. Decompilation is a process by
which someone can re-create the source code that was used to make a particular class file.
People who are worried about competitors stealing their code use obfuscators to make decom-
pilation more difficult. However, obfuscation has the side effect of reducing class file size, mainly
because the descriptive method and variable names you created are replaced with small machine-
generated names. Some obfuscators will also remove unused code. If you're very serious about
reducing the size of your MIDlet suite JAR, try obfuscating your code. We suggest running the
obfuscator before preverifying the class files, but it's conceivable it would work the other way
around, too. Here are two obfuscators to get you started:
http://proguard.sourceforge.net/
http://www.retrologic.com/retroguard-main.html
Summary
MIDP applications are targeted to run on a small platform, which means that using memory
and processing power efficiently is important. Creating and destroying objects is expensive, so
one way to optimize your code is to reduce the number of objects you create. One common source
of new objects is code that creates String s. Consider optimizing String manipulation using
StringBuffer or character arrays. Similarly, you may be able to streamline code by using object
arrays in place of Vector s or Hashtable s. Remember that performance is as much about percep-
tion as anything else; provide a responsive, active user interface and handle failures gracefully.
You can also optimize the delivery of your application in several ways. First, partitioning the
functionality of your application intelligently can reduce the runtime footprint of your application.
Next, trimming out excess classes can reduce the size of your MIDlet suite JAR. Finally, a byte-
code obfuscator can further reduce the size of your MIDlet suite JAR.
Search WWH ::




Custom Search