What are .dex files in Android?
Asked Answered
L

3

233

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!

Louralourdes answered 13/10, 2011 at 6:53 Comment(3)
plz Saw this link #1250473Hong
how to run them on device: #10200363Bearwood
Yes, they are similar to java .class files from one side. Java VM interprets .class files while Delvik VM or Android Run Time interprets .dex files.Sophistic
E
274

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 Dalvik 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.

Euthenics answered 13/10, 2011 at 7:6 Comment(4)
My jar file contains some xml in assets folder. I want this to be a part of .dex Any idea how to implement this ?Salangia
@Salangia - Jar file doesn't contain any android resource package (/asset) you have to use Android Library Package for this.Euthenics
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.Mischief
One important thing to note is that dexdump is not a decompiler but a disassemblerMurry
F
97

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.

Fleischman answered 9/1, 2015 at 9:37 Comment(1)
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.Cherlycherlyn
F
94
.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.

Francoisefrancolin answered 13/10, 2011 at 6:55 Comment(1)
My jar file contains some xml in assets folder. I want this to be a part of .dex Any idea how to implement this ?Salangia

© 2022 - 2024 — McMap. All rights reserved.