Why does Android need a Virtual Machine(DVM)? - android

Virtual Machines are used for running apps on different OS(Java compiles code into OS independent .class files which is interpreted into bytecode). Since android apps run on the same OS (i.e Android), it doesn't require a VM (I could be wrong here). Then why does it need a Dalvik VM?

Android Platform can run on different architectures - ARM, MIPs and x86. To abstract out the need to compile binaries for each architecture VM comes into play. It also does memory management for the developer.

We need someone to compile and convert the java classes into bytecode which can be executed by the interpreter.
It is similar to JVM ... you have .java files which will be compiled by java compiler into .class files. the .class files are nothing but bytecode which will be run by the JVM. JVM can reside on any platform(windows,linux or unix).
In android too the files are compiled into .dex files and run by DVM. just to give an idea, when is application is installed, the Android OS assigns unique linux user id, a DVM is assigned for each app. So in short each app has own linux process, DVM and linux user id.
The java files are compiled into .dex files which consume less memory compared to .class files.
Now assume 10 applications are having 10 individual DVM's and the OS has 10 process to handle.
The dispatcher or scheduler in the android OS is responsible for handling these 10 processes....which is why we have android activity life cycle.
You need DVM to maintain the running state of each process(each app).

Why android needs a virtual machine is on the basis that Google engineered Android API to vastly use a Java interface. Java itself is usually run on a virtual machine.
The JVM itself is a stack machine based VM while Android's VM (called Dalvik) is a register based VM (this is for the sake of less code generated and faster speed to get better performance out of whatever device is using Android)
The purpose of a virtual machine is to be able to abstract hardware by simulating it. If you make a virtual machine and compile it to work on every possible hardware, you get what originally made Java rise to popularity: write once run anywhere portability.
You could write code, without having to change it and run it on any hardware that your virtual machine can run on.
Digressing, Android is mostly built in C (and C++?) but the API that manipulates the OS is interfaced through Java, thus you require a virtual machine.

Related

Can you run the same application and code on Android and Linux?

I'm looking to build a new app written in Java, can I write one code that can be executed both on an OS running Linux and OS running android? Or do I need to write two different codes one for each system?
Theoretically yes.
Android apps run in a Dalvik Virtual Machine, that comes with a Java language implementation that compiles down to Dalvik bytecode, but not to JVM bytecode. So, the code has to be written in Java or some language that compiles to Dalvik VM bytecode that uses the Android API.
However, the virtual machine runs on top of the underlying Linux OS, and there are ways to call native code. See NDK documentation.
So, it is technically possible to run native Linux programs on Android, as there is a Linux kernel running beneath everything.

Where is java command located in Android?

I got my android phone rooted and I'm aware that the / doesn't contain the familiar linux /.
Is there a java command? Where is it located? I don't find one in /system/bin.
Is there a java command?
No. Such a command isn't shipped with stock Android images.
Where is it located?
It shouldn't exist on filesystem. What makes you think it should be there?
I don't find one in /system/bin.
You might consider installing any Java programming app for Android to have JDK/JRE installed. In this case the command will be located in the app's sandbox, not /system/bin.
Android does not use Java at all, at runtime.
Android runs programs that have been written in the Java language. Those programs are not compiled to Java bytecodes and will not run on a JVM.
Android executes Dalvik bytecodes. Until somewhere around Android 4, it used a virtual machine called Dalvik, to execute these bytecodes. Now it uses fantastically more complex virtual machine called ART.
Sorry. No Java.

Does Android abstract the device architecture?

I'm new to android programming, I was reading the answers on this question Why shouldn't an Android app be written in C/C++ because you "simply prefer to program in C/C++"?
in the first answer by Devunwired. he mentioned that "your native code must be built into .so files (one for armv5, armv7 and x86) all packaged up into the same APK. This duplication of executable code makes your app 3x the size (i.e. a "fat binary") unless you take on the task of building separate APKs for each architecture when you distribute the application"
My question is, I always thought that Operating Systems in general provide an abstraction over the underlying architecture. So if I want to deploy a c/c++ program on different environments, I need to re-compile the source code using a compiler written for different operating systems, this operating system however might have different versions that support different architecture.
If I'm right then why the case is different when it comes to Android?
At some point the operating system hands the code over to the CPU. The binary must have instructions for that particular CPU. With Java each OS and CPU makes the same virtual machine available so you don't have the same considerations.

Is a Dalvik virtual machine instance created for each application?

Is a Dalvik virtual machine instance created for each application, or all Android applications share the same Dalvik virtual machine instance?
From the developer docs:
Every Android application runs in its own process, with its own
instance of the Dalvik virtual machine. Dalvik has been written so
that a device can run multiple VMs efficiently.
The Dalvik VM executes files in the Dalvik Executable (.dex) format
which is optimised for minimal memory footprint.
The VM is register-based, and runs classes compiled by a Java language
compiler that have been transformed into the .dex format by the
included dx tool.
Also have a look at What is... The Dalvik Virtual Machine for detailed description about DVM.
The Dalvik virtual machine is built specifically for Android. It was built to address the battery life and processing power problems, and it is free.
We are using Dalvik VM instead of Java Virtual Machine (JVM) because the Java compiler, the Java tools are free, but the JVM is not free, so the Android developers from Google have made their own virtual machine, and made it as free.
A virtual machine is necessary because the virtual machine helps in debugging, as a virtual computer so that my applications can run different devices the same way
Pictorial Representation
A .java file is given to the java compiler (javac) to generate the .class file.
All .class files are given to dx tool to generate a single dex file.
The dex file is given to the Dalvik VM to generate the final machine code.
The final machine code is given to the CPU to execute.
All apk's basic source code is in java language . When you build this project all .java files get converted to .class now the dx tool of adk converts all .class files to classes.dex file .And this classes.dex file is executed on dalvik virtual machine.
For more info on dalvik virtual machine: http://www.slideshare.net/jserv/understanding-the-dalvik-virtual-machine
Dalvik virtual machine is intended to run multiple VMs at a time .
So every app runs in its own process, with its own instance of the Dalvik virtual machine as said by #sahilMahajanMj .
And this classes.dex file is further optimized to odex file and saved in /dalvik/dalvik-cache
To know more about odex click this .
If you want to know why DVM for android why not JVM click this
Dalvik is a virtual machine where every android application runs. Through Dalvik, device is able to run multiple virtual machines through better memory management as Dalvik VMs are register based and hence memory efficient
Every android app runs in its own process, with its own instance of Dalvik VM.
First, Java files are connverted to .class file by java compiler
.class files are given to "dx" tool which converts it to .dex format
.dex file is given to DVM to produce machine code
Machine code is executed by CPU
.apk file contains .dex file in zip format which can be run on Dalvik VMs
Why Dalvik ?
1. DVM are built for battery life, processing power and its free
2. We're using DVM instead of JVM as JVM is not free
Dalvik VMs gives consistency across all the mobile devices i.e. one application will run across different devices in the same way

Why porting Android to x86 is difficult?

I'd like to know the technical difficulty in porting Android to x86 architecture.
Since its source is open, what is the main difficulty in getting to run on x86?
Or is it correct to ask why it cannot be compiled to a Java bytecode to run on a JVM on a PC? Is it because of the Dalvik VM?
I'd appreciate if somebody could explain this.
Thank you.
Actually, it is already ported to x86: http://www.android-x86.org/
Also, Android does not run in a JVM. The Android Kernel is a modified Linux Kernel and written in C. You can't compile it to run in a JVM.
The DVM (used by Android) is a modified JVM based on the OpenSource JVM-implementation Apache Harmony. All Java-Applications on Android run in a DVM.
There isn't a problem. VirtualBox can run an android OS guest machine just fine.
The main difficulty is that x86 and ARM are totally different architectures. They have a totally different mentality about them, different instructions, different registers, different behaviours, different memory architecture, etc. Even the way they interface with other hardware is different.
In terms of technical difficulty, the architectures don't have instructions or mechanisms that are compatible with each other, so the behaviour that is relied upon by a compiler for ARM does not exist on x86, and vice versa.
Knowing enough about one of them to implement anything major usually means you've not spent much time dealing with the other, which just adds to the difficulty.

Categories

Resources