What are .dex files in Android?

177,049

Solution 1

About the .dex File :

One of the most remarkable features of the Dalvik Virtual Machine (the workhorse under the Android system) is that it does not use Java bytecode. Instead, a homegrown format called DEX was introduced and not even the bytecode instructions are the same as Java bytecode instructions.

Compiled Android application code file.

Android programs are compiled into .dex (Dalvik Executable) files, which are in turn zipped into a single .apk file on the device. .dex files can be created by automatically translating compiled applications written in the Java programming language.

Dex file format:

 1. File Header
 2. String Table
 3. Class List
 4. Field Table
 5. Method Table
 6. Class Definition Table
 7. Field List
 8. Method List
 9. Code Header
10. Local Variable List

Android has documentation on the Dalvik Executable Format (.dex files). You can find out more over at the official docs: Dex File Format

.dex files are similar to java class files, but they were run under the Dalkvik Virtual Machine (DVM) on older Android versions, and compiled at install time on the device to native code with ART on newer Android versions.

You can decompile .dex using the dexdump tool which is provided in android-sdk.

There are also some Reverse Engineering Techniques to make a jar file or java class file from a .dex file.

Solution 2

dex file is a file that is executed on the Dalvik VM.

Dalvik VM includes several features for performance optimization, verification, and monitoring, one of which is Dalvik Executable (DEX).

Java source code is compiled by the Java compiler into .class files. Then the dx (dexer) tool, part of the Android SDK processes the .class files into a file format called DEX that contains Dalvik byte code. The dx tool eliminates all the redundant information that is present in the classes. In DEX all the classes of the application are packed into one file. The following table provides comparison between code sizes for JVM jar files and the files processed by the dex tool.

The table compares code sizes for system libraries, web browser applications, and a general purpose application (alarm clock app). In all cases dex tool reduced size of the code by more than 50%.

enter image description here

In standard Java environments each class in Java code results in one .class file. That means, if the Java source code file has one public class and two anonymous classes, let’s say for event handling, then the java compiler will create total three .class files.

The compilation step is same on the Android platform, thus resulting in multiple .class files. But after .class files are generated, the “dx” tool is used to convert all .class files into a single .dex, or Dalvik Executable, file. It is the .dex file that is executed on the Dalvik VM. The .dex file has been optimized for memory usage and the design is primarily driven by sharing of data.

Solution 3

.dex file

Compiled Android application code file.

Android programs are compiled into .dex (Dalvik Executable) files, which are in turn zipped into a single .apk file on the device. .dex files can be created automatically by Android, by translating the compiled applications written in the Java programming language.

Share:
177,049

Related videos on Youtube

Nikunj Patel
Author by

Nikunj Patel

Updated on March 29, 2020

Comments

  • Nikunj Patel
    Nikunj Patel about 4 years

    I have some questions regarding dex files

    • What is a dex file in Android?
    • How does dex work for Android?
    • How are they used in debugging an Android app?
    • Are they similar to java class files?

    I need specific information please help on this and any real examples are welcome!

  • AndroidGuy
    AndroidGuy about 11 years
    My jar file contains some xml in assets folder. I want this to be a part of .dex Any idea how to implement this ?
  • AndroidGuy
    AndroidGuy about 11 years
    My jar file contains some xml in assets folder. I want this to be a part of .dex Any idea how to implement this ?
  • user370305
    user370305 about 11 years
    @AndroidGuy - Jar file doesn't contain any android resource package (/asset) you have to use Android Library Package for this.
  • android developer
    android developer over 9 years
    How does it work on ART, on Android Lollipop? Which files are used there, and in which path? Some said it will take more space. Is it true? If so, how come? I mean, native code seems to always be much smaller than byte-code, and dex files are similar to byte-code in terms of how it works.
  • Igor Čordaš
    Igor Čordaš over 8 years
    Good info. Just one thing to point out. Sometimes there will be multiple dex files because of method invocation instruction number limit. This limit is 65k methods. In that case your code will not be inside a single file but in 2 or more but everything else applies still. Also since dynamic class loading is also available you can load code (again in dexed form) from files and thus code will be inside multiple dex files.
  • genghiskhan
    genghiskhan almost 5 years
    One important thing to note is that dexdump is not a decompiler but a disassembler