Most Android apps are written in the Java programming language. Google’s Android software development system converts “source code” (a text file) written in Java, into the code that runs on the Android device.
In many programming language systems, source code is converted into the “machine instructions” of the processor. The processor does not speak “Java” but speaks its own language. A program called a “compiler” converts the original program source code into the “machine language” of the processor.
Many programs for Windows, for example, have been converted into the individual instructions that are processed by an Intel or AMD processor. The “compiler” converts the program source code into a .exe file that contains the machine language instructions of the Intel and AMD processor.
But what if you wanted your program to run on a hardware device that has a Qualcomm or ARM processor?
You’d need to create a separate executable file for each type of processor. Every program in the Google Play store would need to be in multiple “flavors”, one for each type of processor in each phone or tablet or other devices that run Android software.
Java solves this problem by converting source code into instructions for a “virtual machine”. A “virtual machine” is not a real piece of hardware but a simulation of a computer system (sort of – its more complex than we will get into here). A program, known as a “virtual machine” or “VM” handles the processing of the “virtual machine instructions”. These instructions are sometimes called “bytecodes”.
In the case of Java, a piece of software called the “Dalvik VM” (named after a city in Iceland) interprets the “virtual machine” instructions. A version of the Dalvik VM has been created for every device that runs Java programs.
Using Java and a VM, one program can run on different kinds of hardware.
The Android development system performs the conversion of Java into files that contain instructions for the Dalvik VM. The Dalvik VM software reads and executes the virtual instructions in the executable program file.
With a trick known as “just-in-time compiling”, the Dalvik VM instructions can also be converted into machine hardware instructions, helping to reduce the overhead of processing virtual instructions.
The above is an overview of some of the magic that goes on “under the covers” of software written for Android devices.
When you use the Connect or Build options in App Inventor, the App Inventor system does a compilation step behind the scenes, producing code that is set up either to run with the AI 2 Companion – or by building .apk files, which are the normal executable files used in Android.
So now you know a bit more about how Java source programs are “compiled” for running on a “Virtual machine”. And you probably just learned the name of a city in Iceland that you had never heard of!
If you have not seen what Java source code looks like, here is a short and incomplete snippet (badly formatted due to WordPress):
Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
int rawlevel = batteryIntent.getIntExtra(“temperature”, -1) / 10;int level =
int scale = batteryIntent.getIntExtra(“scale”, -1);
int voltage = batteryIntent.getIntExtra(“voltage”, -1);
float batteryPct = Math.round ( level / (float)scale * 100) ;
battStrTV.setText( Integer.toString(rawlevel) + “, ” + Double.toString(batteryPct) + “%”
+ “,” + Integer.toString(voltage));