服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用proguard混淆j2me应用程序


  如果我们的开发的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。请参考下图
   使用proguard混淆j2me应用程序(图一)
  下面我们按照开发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();
        image image = image.createimage(imagedata, 0, imagedata.length);
        midlet.setimage(image);
        baos.close();
        is.close();
        httpconn.close();
  
      } catch (ioexception e)
      {
        e.printstacktrace();
      }
  
    }
  }
  
  }
  
  右键选择项目getimage-j2me-create obfuscated package,这样proguard就会把除midlet之外的class文件混淆。如下图所示:
  使用proguard混淆j2me应用程序(图二)
  在deploy文件夹内我们可以看到getimage.jar、getimage.jad和一些proguard产生的其他文件,你可以用jar命令解开getimage.jar,发现他的另一个class文件已经被混淆成a.class了,midlet类则没有任何改变,proguard除了混淆的功能之外同时会把我们的jar文件减小,提高一些运行效率。

扫描关注微信公众号