The compiler is a very important part of the software development process. Its contribution cannot be over looked. Its what makes it all work. In a world with no compilers, we would have to write code directly in machine code, i.e 1’s and 0’s just like the geeks did in the 60’s and 70’s. The compiler lets us have a wide variety of languages and tool kits and use whichever one we fancy to get work done.
To fully understand how the compilation process works on Android, it is important to first understand how it originally worked in Java, the toolkit from which Android derives.
- First, the Java code is converted to .class files in byte code by the compilation process. It is important to understand that byte code is not machine readable code yet. This byte code is for the JVM to understand. More on that later.
- This byte code, which is platform independent can then be used as input to the JVM which uses a Just In Time (JIT) compiler to convert byte code to machine language, the 1’s and 0’s.
This 2 step process helps keep Java platform dependent, and this is only possible due to JVM which in turn is platform dependent.
So how does Android change this? Unlike its predecessors, Android was not built to be an OS for portables only, but there were a few changes needed to make the Java compilation process more memory and battery efficient for portable devices.
- Android stripped down the JVM to create the Dalvik Virtual Machine (DVM). This enables every process on Android to have its own DVM. It keeps processes separate for security.
- The android compiler compiles your app code into byte code which is quite similar to the one for JVM.
- This byte code is then minimized into .dex byte code which can then be compiled by the DVM on the target device to execute the program.
This is now the legacy build process for Android which was valid until Android Kitkat (Version 4.4). Newer versions of Android use Android Run Time (ART) which is must faster. We shall discuss ART in a future blog post.