Tuesday, 18 August 2015

Dalvik Virtual Machine And .apk File

Dalvik Virtual Machine is an open source software originally written by Dan Bornstein.

As its name suggests that it’s a virtual machine not hardware based machine. This is Software which helps to execute java byte code written for Android Application in Java Programming Language. It is middleware Software between java byte and executable file. It is almost same as JVM (Java Virtual Machine) but has some differences in architecture and working efficiency . Following are the steps converting a java file into .dex file.
Step 1:- We have some source code written in java. Source file with .java Extension for example (task.java)
Step 2:- Then this .java file will be compiled and converted into object code with file extension .class for example (task. Class)
Step 3:- Then this .class file will be translated into dex  file by the Dalvik Virtual Machin dex stands for Dalvik Executable.
Now the little bit about the Architecture of Dalvik Virtual Machin
Dalvik Virtual Machin is based on Registers, because registers take less memory to run and it is designed as because we have to run Multiple Virtual Machines at the same time. It requires fewer instructions to complete the execution. While JVM is stack based it requires more memory and instructions to complete the Execution.
DVM is Register based which is designed to run on low memory, uses its own byte code and runs .dex file.JVM is Stack based which uses java byte code and runs .class file having JIT.

For Example: Android programs are a .class file converted to dalvik executable using dx tool which are turn zipped into single .apk android package file on the device.


                                                    

As shown in the figure above how .class file of java can be converting in to android .apk file by using a tool called AAPT (Android Asset Packaging Tool).

.apk file Extention:

.apk file is an installer package file which install on google’s Operating System Called Android. Apk stands for Android Application Package.  It can also be run on certain other operating systems like Blackberry Version 10.2.1 or higher than this version.
.apk package file contain the following directories:-
META-INF directory:
MANIFEST.MF: The Manifest file
CERT.RSA: The certificate of the application.
CERT.SF: The list of resources and SHA-1 digest of the corresponding lines in the MANIFEST.MF file;
LIB: the directory containing the compiled code that is specific to a software layer of a processor, the directory is split into more directories within it:
ARMEABI: compiled code for all ARM based processors only
ARMEABI-V7A: compiled code for all ARMv7 and above based processors only
X86: compiled code for x86 processors only
MIPS: compiled code for MIPS processors only
RES: the directory containing resources not compiled into resources.arsc (see below).
ASSETS: a directory containing applications assets, which can be retrieved by AssetManager.
ANDROIDMANIFEST.XML: An additional Android manifest file, describing the name, version, access rights, referenced library files for the application. This file may be in Android binary XML that can be converted into human-readable plaintext XML with tools such as AXMLPrinter2, android-apktool, or Androguard.
CLASSES.DEX: The classes compiled in the dex file format understandable by the Dalvik virtual machine
RESOURCES.ARSC: a file containing precompiled resources, such as binary XML.
Below figure show the clear how .class can conveted into .dex file.

Explanations to the above figure:
In standard Java environment, Java source code is compiled into java bytecode, which is stored within .class files. The .class file are read by Java Virtual Machine(JVM)  at runtime.
Each class in  your java code will result in one .class file. This means if you  have,say,one .java source file that contains one public class,one static inner class,and three anonymous classes, the compilation process will output five .class files.
 
 On the Android platform,
 Java source code is still compiled into .class file.But after .class files are generated, the dx tool is used to convert the .class files into a .dex, or Dalvik executable, file. Whereas a .class file contains only one class,a .dex file contains multiple classes.The .dex file executed on the Dalvik VM.
The compact dalvik executable format is designed to be suitable for systems that are constrained in terms of memory and speed.




 Figure below explains difference between JVM and DVM.


 The above figure is clearly showing that how a normal java code and an android code is get complied. Left side of the java source code can be complied as normal JAVA.in the right side of the figur it clearly showing how the java code can converting into .dex file by the complier with the help of Dalvik.

Please Send Your Comments To ambilpura.sunil@gmail.com

NEXT Tutorial:  Android Life Cycle

No comments:

Post a Comment