Java Reference
In-Depth Information
in an application and simply remove them. This is important with third-party libraries, with
which you may only be using a fraction of the available functionality. Bouncy Castle includes
all sorts of stuff that isn't used in PasswordMIDlet and StealthMIDlet .
There is another reason an obfuscator is necessary when using Bouncy Castle and some
other third-party APIs. Bouncy Castle includes implementations of classes in the core java.*
namespace, like java.math.BigInteger . MIDP implementations will fail to load classes from
this namespace in an application. An obfuscator can be used to rename these classes out of the
forbidden namespace.
The sample code from this chapter (available from the Downloads section of the Apress
web site [ http:// www.apress.com ]) contains an Ant build file that invokes the ProGuard 3.2
obfuscator. ProGuard is an excellent piece of software. It is written entirely in Java and may be
used freely. For more information, see http://proguard.sourceforge.net/ .
The Ant target that runs ProGuard looks something like this:
<target name="obfuscate_proguard" depends="compile, copylib">
<mkdir dir="build/proguard"/>
<jar basedir="build/classes"
jarfile="build/proguard/$wj2-crypto-input.jar"/>
<java fork="yes" classname="proguard.ProGuard"
classpath="${proguard}">
<arg line="-libraryjars ${midp_lib}"/>
<arg line="-injars build/proguard/${project}-input.jar"/>
<arg line="-outjar build/proguard/${project}-output.jar"/>
<arg line="-keep
'public class * extends javax.microedition.midlet.MIDlet'"/>
<arg line="-defaultpackage"/>
<arg line="-dontusemixedcaseclassnames"/>
</java>
<mkdir dir="build/obfuscated"/>
<unjar src="build/proguard/${project}-output.jar"
dest="build/obfuscated"/>
</target>
ProGuard expects its input classes to be packaged in a JAR, so the first thing to do is create
a JAR based on the package name, wj2-crypto-input.jar . Note that this JAR includes the
Bouncy Castle classes.
Next, ProGuard is run by forking a Java process. The first argument, -libraryjars , tells
ProGuard where to find the MIDP classes. The next argument, -injars , points ProGuard to the
JAR of input files. The output file name is specified using -outjar . Next come three important
options. It's important that the MIDlet classes themselves retain their names so that MIDlet
management software on a device can load and run the classes. The -keep argument makes
this happen for all subclasses of MIDlet . The package renaming (moving things out of java.* )
is accomplished using the -defaultpackage argument. Finally, -dontusemixedcaseclassnames
works around asinine behavior in Windows where obfuscated class files like a.class and A.class
cannot exist in the same directory.
Search WWH ::




Custom Search