If our development J2ME application is packaged directly into a JAR file, then there is possible to compile it by others. Because the anti-compilation Class file is not a difficult thing. To protect our program code is not broken, we can use the obfuscator Proguard. Very fortunate is Eclipse that has already been integrated with Proguard.
Proguard is an open source software, which is based on Java language, so his run requires a Java2 operating environment. We can download from http://proguard.sourceforge.net. The latest version is Proguard 3.0. Solve him to install him in C: /Proguard 3.0.1. Run Eclipse, select Menu Windows-Preferences-J2me-Obfuscation, where we should specify the correct proGuard root directory, because we are confused, you must reserve the class that extends the MIDlet, or the program will not be executed. So in Proguard Keep Expressions, you should write a public class * extend.midlet.micRoEdition.midlet.mIDlet. Please refer to the following figure
Below we write code, compile, pre-verified as developing J2ME applications. In order to save time, these steps are ourselves here. The following code as an example to demonstrate how to use proguard confusion J2ME application: import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui *; import java.io *; import.. Javax.microedition.io. *;
Public class imagegetter extends midlet imports commandListener {
private Display display; public static final Command connCommand = new Command ( "Connect", Command.ITEM, 1); public static final Command exitCommand = new Command ( "Exit", Command.EXIT, 1); private Form mainForm; private GetterThread GT;
Protected void startapp () throws midletStateChangeException {
display = Display.getDisplay (this); mainForm = new Form ( "Image Getter"); mainForm.append ( "Click Connect to get Image"); mainForm.addCommand (connCommand); mainForm.addCommand (exitCommand); mainForm.setCommandListener (this); Display.SetCurrent (mainform); gt = new getTerthread (this); gt.start ();
}
Public void setImage (image image) {
Mainform.Append (Image); Display.SetCurrent (Mainform);
protected void pauseapp () {}
Protected Void DestroyApp (Boolean Arg0) throws midletStateChangeException {
}
Public void CommandAction (Command CMD, Displayable Disp) {if (cmd == conncommand) {synchronized (this) {notify ();}} else if (cmd == exitcommand) {exitmidlet ();}}
Private void exitmidlet () {try {destroyApp (false); notifyDestroyed ();} catch (MIDletStateChangeExcection) {E.PrintStackTrace ();}}
class GetterThread extends Thread {private ImageGetter midlet; public static final String URL = "http: //localhost/j2medev.png"; private HttpConnection httpConn = null; private InputStream is = null;
Public GetTerthread (ImageGetter MIDlet) {this.midlet = midlet;}
Public void Run () {synchronized (midlet) {type {midlet.wait ();} catch (interruptedException e) {E.PrintStackTrace ();}} system.out.println ("Connect to Server ..."); try {httpConn = (HttpConnection) Connector.open (URL); is = httpConn.openInputStream (); ByteArrayOutputStream baos = new ByteArrayOutputStream (); int ch = 0; while ((ch = is.read ()) = -1! ) {Baos.write (ch);} byte [] imagedata = baos.tobyteaRray (); image image = image.createImage (imagedata, 0, imagedata.length); MIDlet.setimage (image); baos.close (); Is.close (); httpconn.close ();} catch (ooexception e) { E.PrintStackTrace ();
}
}
Right click on the project getImage-j2me-create obfuscated package, so ProGuard confuses the Class file except MIDlet. As shown below:
In the Deploy folder we can see getImage.jar, getImage.jad, and some other files generated by ProGuard, you can use the jar command to unclept getImage.jar, found that his other Class file has been confused into a.class. There is no change in the MIDlet class, which will reduce our JAR files in addition to confusing features and improve some running efficiency.