如果我们的开发的J2ME应用程序直接打包成JAR文件发布,那么存在被其他人反编译的可能。因为反编译class文件并不是一件困难的事情。为了保护我们的程序代码不被破解,我们可以使用混淆器Proguard。非常幸运的是eclipse已经把Proguard集成在一起了。 Proguard是开源的软件,他是基于java语言写成的,因此他的运行需要java2运行环境。我们可以从http://Proguard.sourceforge.net 免费下载到。目前的最新版本为proguard3.0。把他解压缩安装在C:\proguard3.0.1。运行eclipse,选择菜单windows-preferences-j2me-obfuscation,在这里我们应该指定正确的Proguard的根目录,由于我们混淆的时候要保留扩展了MIDlet的类,不然程序将无法执行。所以在Proguard keep expressions中应该写public class * extends javax.microedition.midlet.MIDlet。请参考下图  下面我们按照开发J2ME的应用程序一样编写代码、编译、预验证。为了节省时间,在这里略去这些步骤。以下面的代码为例演示如何使用proguard混淆J2ME应用程序: 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 implements 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 (MIDletStateChangeException e) { 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) { try { 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()[1] [2] 下一页
|
|