Decompiling DEX to Java

The DEX code, as we know, is compiled from Java, which is a pretty semantic, easy-to-read language, and I'm sure some of you are wondering by now whether it's possible to decompile the DEX code back into Java? Well, the good news is that this is possible, of course, depending on the quality of the decompiler you are using and the complexity of the DEX code. This is because unless you understand how the DEX code actually works, you will always be at the mercy of your DEX decompiler. There are many ways to thwart the popular decompilers such as reflection and non-standard DEX opcode variants, so if you're hoping that this recipe means you can call yourself an Android reverse engineer even though you are unable to read the DEX code, you are mistaken!

With that said, most DEX code in Android applications are pretty stock standard, and decompilers, such as the one we are about to use, can handle an average DEX file.

Getting ready

Before we start, you will need to grab a few tools from the Internet.

  • Dex2Jar: This is a tool that grabs the DEX files from the APK files and outputs a JAR containing the corresponding class files; you can get this at http://code.google.com/p/dex2jar/. Visit this URL and download the version appropriate for your operating system.
  • JD-GUI: This is a Java class file decompiler; you can get this at http://jd.benow.ca/. It has support for Linux, Mac, and Windows.

How to do it...

To decompile a sample DEX file into some Java code, you will need to perform the following steps:

  1. Let's assume we are starting from either an APK or DEX file. In that case, you would start out by interpreting the DEX files into the Java CLASS files. Here's how you do that with Dex2jar:
    dex2jar [Dex file].dex
    

    Or for our running example, you would execute the following statement:

    dex2jar Example.dex
    

    The output should look something like the following screenshot:

    How to do it...

    If you've executed this correctly, you should have a file called Example_dex2jar.jar in your working or current directory:

    How to do it...
  2. So now that we have our class files, we need to work them back into the Java code. JD-GUI is the tool that we will be using to sort this out. To launch JD-GUI, all you need to do is execute the JD-GUI executable that comes with the JD-GUI tool. Here's how you do it from Linux; execute the following command from your terminal:
    jd-gui
    

    It should spawn a window that looks like the following screenshot:

    How to do it...
  3. Once this window shows up, you can open a class file by clicking on the folder icon; the following file selection dialog box should show up:
    How to do it...

    Once this dialog box is open, you should navigate to the path with the Example.class file we parsed from the Example.dex file. If you manage to find it, JD-GUI will display the code as follows:

    How to do it...
  4. You can use JD-GUI to save the source files; all you need to do is click on the File menu on the toolbar, select Save All Sources, and then provide a directory to save it in:
    How to do it...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.60