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.
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:
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.
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:
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.
Ethical and Legal Implications
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
| Step | Tool | Process |
| 1 | APKTool | Extract APK files to retrieve DEX files. |
| 2 | dex2jar | Convert DEX files into intermediate JAR. |
| 3 | JD-GUI | Decompile 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
- How to decompile DEX into Java source code?
- How to define a custom AuthenticationEntryPoint without XML configuration
- How to define a List bean in Spring?
- How to define custom exception class in Java, the easiest way?
- How to define a circle shape in an Android XML drawable file?
- How to define a circle shape in an Android XML drawable file?
- How to define different dependencies for different product flavors
- How to define Servlet filter order of execution in Spring Boot application

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.