2
votes

I know that Android uses its own Virtual Machine called Dalvik, and it run .dex file instead of .class file.

According to Wikipedia,

Programs are commonly written in Java and compiled to bytecode for the Java virtual machine, which is then translated to Dalvik bytecode and stored in .dex (Dalvik EXecutable)

Why Android needs to first compiled into bytecode for the JVM (.class) and then translates into Dalvik bytecode (.dex), why can't it directly compiled to Dalvik byte code from .java file ? ?

and If it uses two VM, first JVM and then Dalvik, then doesn't it make the compiling process slow ?

Update:

If i run a program in Java which will compile .java file into bytecode (.class) and then interpret it, and If i run the same program in Android, which will first compile .java file into .class and that .class into .dex and then run it...wouldn't Android be slower in comparison ?

2

2 Answers

3
votes

Why Android needs to first compiled into bytecode for the JVM and then translates into Dalvik bytecode.? why can't it directly compiled to Dalvik byte code from .java file ?

It could, if you wanted to write a full compiler for that. The new Jack compiler for Android apparently does this.

In general, it is easier to write a bytecode converter than it is to write a full compiler.

then doesn't it make the compiling process slow ?

The conversion process does add overhead. "Slow" is a statement of opinion, and so I do not know whether the bytecode conversion makes the "compiling process slow" for your definition of "slow".

2
votes

The primary reason Android uses Dalvik is for app and system wide resources. The way Dalvik runs your app is by forking a process that already has most of the resources your app needs to run (not to mention this method makes sandboxing trivial).

There is a great (albeit old, since ART is the new kid on the block) article on why Dalvik is so useful in the context of mobile devices: http://davidehringer.com/software/android/The_Dalvik_Virtual_Machine.pdf