DEX Decompiling
Java Source Code
Android Development
Reverse Engineering
Programming Languages

How to decompile DEX into Java source code?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Android applications are distributed as APK (Android Package Kit) files, which contain executables in the form of DEX (Dalvik Executable) files. Decomiling DEX files into Java source code can be essential for developers for debugging, analyzing other applications, or recovering lost source code. This process involves several steps and tools.

Understanding DEX Files

DEX files are compiled Android bytecode that the Dalvik virtual machine (or Android Runtime for newer versions) executes. They are designed to be highly optimized for memory and processing efficiency on mobile devices, which makes them quite different from standard Java class files.

Required Tools

To decompile DEX files into human-readable Java source code, you will generally need the following tools:

  • APKTool: To extract the APK contents and resources.
  • dex2jar: To convert DEX files into Java JAR files.
  • JD-GUI: To view and save the source code from JAR files.

Step-by-Step Decompile Process

1. Extract the APK File

First, use APKTool to extract the APK file. This tool helps in getting both the resource files and the DEX files which are necessary for a thorough examination of the application:

bash
apktool d myApp.apk -o myAppFolder

This command decompiles the APK into a folder named myAppFolder, containing various resources and DEX files.

2. DEX to JAR Conversion

Next, use dex2jar to convert the DEX files into Java Archive (JAR) files, which are easier to handle with Java decompilers.

bash
d2j-dex2jar.sh -f -o output.jar myAppFolder/classes.dex

This command creates a JAR file named output.jar from the classes.dex extracted in the previous step.

3. Decompiling JAR to Java Source Code

Once you have the JAR file, use JD-GUI to decompile it. Open JD-GUI, load your JAR file, and you should be able to browse the source code:

bash
jd-gui output.jar

JD-GUI allows you to browse the structure of the JAR file and save the source code to your local file system for further analysis.

Technical Considerations

When decompiling software, be aware that the resulting source code might not be perfect. Obfuscated code can often result in less readable and non-functional Java code. The efficiency of decompilation is highly dependent on the complexity of the application and the degree to which it has been obfuscated.

Decompiling software might breach the software’s end-user license agreement (EULA) and could have legal implications. It's essential to ensure that decompiling a specific APK is legally permissible and ethically justifiable.

Summary Table

StepToolProcess
1APKToolExtract APK files to retrieve DEX files.
2dex2jarConvert DEX files into intermediate JAR.
3JD-GUIDecompile JAR files to Java source code.

Conclusion

Reconstructing Java source code from APK files is feasible with the right tools and methodologies, providing valuable insights into Android applications' workings. However, the decompiled code must be handled responsibly and ethically, respecting the original software's copyright and usage terms. This process is beneficial for legitimate purposes such as security testing, educational purposes, or recovery of lost source code.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.