C H A P T E R  3

Creating an Obfuscator Plug-in

The Sun Java Wireless Toolkit for CLDC allows you to use a bytecode obfuscator to reduce the size of your MIDlet suite JAR file. The toolkit comes with support for ProGuard, as described in the Sun Java Wireless Toolkit for CLDC User's Guide.

If you want to use a different obfuscator, you can write a plug-in for the Sun Java Wireless Toolkit for CLDC.


3.1 Writing the Plug-in

Obfuscator plug-ins extend the com.sun.kvem.environment.Obfuscator interface. The interface itself is contained in toolkit\wtklib\kenv.zip.

The Obfuscator interface contains two methods that you must implement:

To compile your obfuscator plug-in, make sure to add kenv.zip to your CLASSPATH.

For example, here is the source code for a very simple plug-in. It doesn't actually invoke an obfuscator, but it shows how to implement the Obfuscator interface.


import java.io.*;
public class NullObfuscator
    implements com.sun.kvem.environment.Obfuscator {
  public void createScriptFile(File jadFilename, File projectDir) {
    System.out.println("NullObfuscator: createScriptFile()");
  }
 
  public void run(File jarFileObfuscated, String wtkBinDir,
    String wtkLibDir, String jarFilename, String projectDir,
    String classPath, String emptyAPI) throws IOException {
      System.out.println("NullObfuscator: run()");
    }
}

Suppose you save this as toolkit\wtklib\test\NullObfuscator.java. Then you can compile it at the command line like this:

set classpath=%classpath%;toolkit\wtklib\kenv.zip
javac NullObfuscator.java


3.2 Configuring the Toolkit

Once you've written an obfuscator plug-in, you must tell the toolkit where to find it. To do this, edit toolkit\wtklib\Windows\ktools.properties. Edit the obfuscator plug-in class name and tell the toolkit where to find the class. If you're following along with the example, edit the properties as follows:

obfuscator.runner.class.name: NullObfuscator
obfuscator.runner.classpath: wtklib\\test

Restart the toolkit and open a project. Now choose Project > Package > Create Obfuscated Package. In the console, the output of NullObfuscator displays, as follows:

Project settings saved
Building "Tiny"
NullObfuscator: createScriptFile()
NullObfuscator: run()
Wrote C:\WTK252\apps\Tiny\bin\Tiny.jar
Wrote C:\WTK252\apps\Tiny\bin\Tiny.jad
Build complete