J2ME Polish
J2ME Polish 2.4 Documentation
Enough Software

Integrating a Custom Obfuscator

For integrating another obfuscator, you need to first create a class which extends the de.enough.polish.obfuscate.Obfuscator class. Secondly you need to integrate your obfuscator in the "build.xml" file.


Create a new project in your IDE and set the classpath so that it includes the "enough-j2mepolish-build.jar", which can be found in the "import" folder of the installation directory. Also include the "ant.jar" file from your Ant-installation into the classpath.

Creating the Obfuscator Class

Create a new class which extends de.enough.polish.obfuscate.Obfuscator and implement the "obfuscate" method:

	 * Obfuscates a jar-file for the given device.
	 * @param device The J2ME device
	 * @param sourceFile The jar-file containing the projects classes
	 * @param targetFile The file to which the obfuscated classes should be copied to 
	 * @param preserve All names of classes which should be preserved,
	 *                 that means not renamed or removed.
	 * @param bootClassPath A path to the library containing either the MIDP/1.0
 	 *                 or MIDP/2.0 environment.
	 * @throws BuildException when the obfuscation failed
	public abstract void obfuscate( 	Device device, 
						File sourceFile, 
						File targetFile, 
						String[] preserve, 
						Path bootClassPath )
	throws BuildException;

You can make use of following protected variables:

  • de.enough.polish.LibraryManager libraryManager: a manager for device-APIs
  • java.io.File libDir: the path to the "import" folder
  • org.apache.tools.ant.Project project: the Ant project in which the obfuscator is embedded

If you need further configuration settings, you can add them with the <parameter> construct, which is discussed below.

To call the obfuscator, you typically need to accomplish these tasks in the "obfuscate"-method:

  1. Set the classpath for the given device: You can get the device-specific APIs by calling device.getClasspath() which returns a String array containing the locations of the device-API-Jars. The usual behavior is to set the classpath for the obfuscator to the given bootClassPath and the device-specific APIs.
  2. Specify which classes should be spared from obfuscation: the classes specified by the preserve-array should not be obfuscated at all. These always include the MIDlet classes and maybe some other classes which are loaded with the Class.forName()-mechanism.
  3. Start the obfuscator: set the input to the provided sourceFile and the output to the specified targetFile. Both of these files are Jars.tart the obfuscator: set the input to the provided sourceFile and the output to the specified targetFile. Both of these files are Jars.

When there is an exception, abort the build by throwing a org.apache.tools.ant.BuildException explaining the details why the obfuscation was aborted.

Integrating the Obfuscator into the build.xml

You can integrate any thirdparty obfuscator with the usual <obfuscator> element in the build.xml file (compare the <obfuscator> documentation).
<obfuscator unless="test" enable="true" 	
	<keep class="com.company.dynamic.SomeDynamicClass" />
	<keep class="com.company.dynamic.AnotherDynamicClass" />
	<parameter name="scriptFile" value="../scripts/obfuscate.script" />

The "class"-attribute needs to be set to the new class. The classPath-attribute can be used for pointing out the location of the obfuscator. This is only needed when the obfuscator is not on the Ant-classpath.

Configuring the Obfuscator with Parameters

The obfuscator can be configured using <parameter> subelements in the build.xml. For each <parameter> a corresponding "set[parameter-name]"-method needs to be implemented, which either needs to have one File parameter, one boolean or one String parameter:

	<parameter name="scriptFile" value="../scripts/obfuscate.script" />

For the using the above parameter, the obfuscator needs to implement either the public method setScriptFile( File file ) or the public method setScriptFile( String value ). When a method with a File-parameter is used, relative file paths are resolved relative to the location of the build.xml file (or to be more precise relative to the project.getBaseDir() location). The file given as a parameter in the set-method uses an absolute path in that case.

JavaDoc Resources

Please refer to these JavaDoc resources for detailed information:

back to top