bytes (?static byte[] data = {(byte)0x02, (byte)0x4A, …};’) costs 4 bytes per value, not 1 byte, in the resulting class file.
2.2 Using an obfuscator
An obfuscator is a program that modifies your compiled Java program to remove all unnecessary information (like long method and variable names), making it hard to understand the result of ―decompiling‖ it. As a protection method it is of less value for MIDlets, since they‘re so small that with some work even a decompiled obfuscated MIDlet can be figured out.
However, removing all of that unnecessary information makes your JAR file smaller, which is very helpful. The size reduction varies from obfuscator to obfuscator and from MIDlet to MIDlet, but tests on a few of our own MIDlets showed that a 10% reduction is typical. This is less than usually claimed for obfuscators, perhaps because MIDlets are small and there are some fixed overheads, and also because resources like PNG bit map files make up a larger proportion of a MIDlet‘s JAR file.
2.3 Libraries
In normal software development it is wise to develop and use libraries of frequently needed functionality. However, if the entire library is included in your MIDlet suite, you‘re likely to be paying for a lot of functionality you don‘t actually n eed.
You may do better to fall back on ―cut and paste‖ reuse. It is a less efficient use of a developer‘s time, but a more efficient use of the JAR file space.
If you do use libraries, consider whether you need all the classes in the library. Perhaps you can remove several of the class files. You may even want to recompile some classes with unused methods removed.
Bearing this in mind, if you‘re writing a MIDP library you should aim to reduce dependencies between classes, so that unneeded classes can indeed be safely removed. Unfortunately, one typical approach to this is to use Java interfaces, which again expands your library size
2.4 Keeping resources small
MIDlet suites often contain associated PNG bit maps, etc. Keep these as small and as few as possible.
There are significant differences in the size of a PNG bit map when saved with different bit map editing tools — not all optimize for size. Try a few of these tools and save with whichever gives the smallest result (even if you prefer to edit with another).
2.5 Combining image files
Just as it helps to minimize the per-file JAR overhead by having as few classes as possible, it also helps to have as few image files as possible. One often-used trick is to combine many images into one file:
Once that large image has been loaded from the file, individual frames can be drawn as follows:
g.setClip(x, y, FRAME_WIDTH, FRAME_HEIGHT); g.drawImage(fiveMenImage, x –FRAME_WIDTH * frameNumber, y, Graphics.TOP | Graphics.LEFT);
Here frameNumber is from 0 to 4; by cycling it in the sequence {0, 1, 2, 3, 4, 3, 2, 1} you will produce an animation of a walking man. If you do any more drawing in your paint method after the