## Chapter 3

## Creating an Obfuscator Plug-in

The J2ME Wireless Toolkit allows you to use a bytecode obfuscator to reduce the size of your MIDlet suite JAR. The toolkit comes with support for ProGuard and RetroGuard, as described in the J2ME Wireless Toolkit User’s Guide.

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

## 3.1 Writing the Plug-in

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

The `Obfuscator` interface contains two methods that you must implement:

    public void createScriptFile(File jadFilename, File projectDir);

    public void run(File jarFileObfuscated, String wtkBinDir,
        String wtkLibDir, String jarFilename, String projectDir,
        String classPath, String emptyAPI) throws IOException;

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%;\WTK22\wtklib\kenv.zip
    javac NullObfuscator.java

## 3.2 Configuring the Toolkit

Once you’ve written an obfuscator plug-in, you have to tell the toolkit where to find it. To do this, edit {toolkit}`\wtklib\Windows\ktools.properties`. You’ll need to 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 KToolbar and open a project. Now choose Project > Package > Create Obfuscated Package. In the KToolbar console, you’ll see the output of `NullObfuscator`:

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

| J2ME Wireless Toolkit Basic Customization Guide J2ME Wireless Toolkit 2.2

Copyright © 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to the license terms.