Role of classes.dex file in an apk file

AndroidApk

Android Problem Overview


When opening an APK file with WinRar (software to open compressed files). I got a bunch of files packed inside the APK. Among them classes.dex is one. My question is what is the role of this file and if we modify/delete the same file will it affect the APK?

Android Solutions


Solution 1 - Android

To make an APK file, a program for Android is first compiled, and then all of its parts are packaged into one file. This holds all of that program's code (These are the .dex files), resources, assets, certificates, and manifest file.

Programs are commonly written in Java and compiled to bytecode. They are then converted from Java Virtual Machine-compatible .class files to Dalvik-compatible .dex (Dalvik Executable) files before installation on a device. The compact Dalvik Executable format is designed to be suitable for systems that are constrained in terms of memory and processor speed.

  • classes.dex: The classes compiled in the dex file format understandable by the Dalvik virtual machine

> As the .dex files holds the APK resources, any edit on these files > will directly effect the APK.

Solution 2 - Android

.dex file Compiled Android application code file.

From Android API GUIDES

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.

And yes if you will delete those files it will effect APK.

Solution 3 - Android

classes.dex is essentially all of the application logic. Code of the given application is written in java and then compiled to class files, then these class files are cross compiled (with many optimisations) to dalvik VM format. Note that there also might be some .so files which are also application code but these are generated when NDK is used. You can not delete this file. You could however change it by first running this utility <https://github.com/JesusFreke/smali> which will generate smali code from this compiled dex which is somewhat similar to java and could be understood. You could also use tools ApkOneClick or ApkMultiTool to get Java source from the smali files but these would probably not be perfect and will require further fixing. When you change the code you want you should build the classes.dex again and put them into existing zip/apk file you have. Note that then existing certificate files (META-INF) will not be valid anymore and you will need to delete this folder and resign the apk package in order to instal it on the phone or emulator.

For more info you could check this question too https://stackoverflow.com/questions/7750448/dex-file-in-android

Also this is a great tutorial on disassembling dex files using existing tools http://blog.vogella.com/2011/02/14/disassemble-android-dex

Solution 4 - Android

> What is the role of this file?

The role of classes.dex in Android is similar to that of JAR files in plain Java. It's a file containing bytecodes. In Android case, the bytecode is Dalvik bytecode, which is different from Java bytecode.

> If we modify/delete the same file will it effect the apk?

If you modify classes.dex, you are modifying the programs behavior, which may or may not work after a repackage. If you delete classes.dex, then your application doesn't have code and you shouldn't expect it to work.

Solution 5 - Android

.dex file in the apk is the compress file which is made up of all the java classes in the application code. Its different than jar file. A jar file is a collection of .class files which are isolated. If we unzip .jar, we get all the classes separately. On the other side, .dex file is a single file made up with all .class file from application code.

Code compilation flow : multiple .java files --> multiple .classes files --> a single .dex file

.dex files are the executables which are executed by the DVM...Dalvik Virtual Machine, which is a Runtime for Android.

.dex will never include resources. Resources are separately maintained in the /res folder in .apk

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
QuestionJainendraView Question on Stackoverflow
Solution 1 - AndroidSahil Mahajan MjView Answer on Stackoverflow
Solution 2 - AndroidBBdevView Answer on Stackoverflow
Solution 3 - AndroidIgor ČordašView Answer on Stackoverflow
Solution 4 - AndroidCykerView Answer on Stackoverflow
Solution 5 - AndroidSuyog GunjalView Answer on Stackoverflow