What are ODEX files in Android?

AndroidVirtual MachineAndroid SourceDalvik

Android Problem Overview


After some android apps installed, I found that it will change to odex file (not apk) in smartphone. How does it happens? Who can teach me, I am very interested about it.

Android Solutions


Solution 1 - Android

The blog article is mostly right, but not complete. To have a full understanding of what an odex file does, you have to understand a little about how application files (APK) work.

Applications are basically glorified ZIP archives. The java code is stored in a file called classes.dex and this file is parsed by the Dalvik JVM and a cache of the processed classes.dex file is stored in the phone's Dalvik cache.

An odex is basically a pre-processed version of an application's classes.dex that is execution-ready for Dalvik. When an application is odexed, the classes.dex is removed from the APK archive and it does not write anything to the Dalvik cache. An application that is not odexed ends up with 2 copies of the classes.dex file--the packaged one in the APK, and the processed one in the Dalvik cache. It also takes a little longer to launch the first time since Dalvik has to extract and process the classes.dex file.

If you are building a custom ROM, it's a really good idea to odex both your framework JAR files and the stock apps in order to maximize the internal storage space for user-installed apps. If you want to theme, then simply deodex -> apply your theme -> reodex -> release.

To actually deodex, use small and baksmali:

https://github.com/JesusFreke/smali/wiki/DeodexInstructions

Solution 2 - Android

This Blog article explains the internals of ODEX files:

> WHAT IS AN ODEX FILE? > > In Android file system, applications come in packages with the > extension .apk. These application packages, or APKs contain certain > .odex files whose supposed function is to save space. These ‘odex’ > files are actually collections of parts of an application that are > optimized before booting. Doing so speeds up the boot process, as it > preloads part of an application. On the other hand, it also makes > hacking those applications difficult because a part of the coding has > already been extracted to another location before execution.

Solution 3 - Android

ART

According to the docs: http://web.archive.org/web/20170909233829/https://source.android.com/devices/tech/dalvik/configure an .odex file:

> contains AOT compiled code for methods in the APK.

Furthermore, they appear to be regular shared libraries, since if you get any app, and check:

file /data/app/com.android.appname-*/oat/arm64/base.odex

it says:

base.odex: ELF shared object, 64-bit LSB arm64, stripped

and aarch64-linux-gnu-objdump -d base.odex seems to work and give some meaningful disassembly (but also some rubbish sections).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1253435View Question on Stackoverflow
Solution 1 - AndroidNathan StrongView Answer on Stackoverflow
Solution 2 - AndroidtheomegaView Answer on Stackoverflow
Solution 3 - AndroidCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow