Role of classes.dex file in an apk file
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?
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.
.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.
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 What are .dex files 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