What do you know about Android Runtime

2014-01-02

Android Platform: Native Layer

Android includes a set of native libraries written in C/C++ which are compiled to native machine code. These libraries directly interact with Android Linux kernel and export its facilities to rest of the Android stack. The functionalities offered by these libraries are exposed to developers through the application framework.[Working of android platform

These libraries can be divided into two categories: [

  • The libraries that are implemented as part of Android Open Source Project (ASOP) They include Bionic libc library, Dalvik virtual machine libraries, Media libraries, and Android OpenGL implementations, Android core libraries (UI, Android runtime, Binder Service, Sensor Service, Camera Service, GPS service, Logger, etc.)
  • The libraries that are integrated into ASOP from external open source projects like Open SSL, SQLite database engine, WebKit browser engine. Biconic library is a custom libc (standard C library code) implementation, highly optimized for Android. It has built-in support for Android-specific services like log capabilities, system properties. There are several reasons to build a custom libc library. Android is based on the Linux kernel which is under the GPL license (GPL). But Google wants Android stack to stay away from the effects of the GPL. So Biconic is made available under the friendly BSD license. Bionic is much smaller in size than the standard C system library. It is super-fast as it is specifically designed for CPUs at relatively low clock frequencies.

Media framework is based on PacketVideo OpenCORE platform. It supports standard audio, video, and still-frame (single captured frame within a video file) formats. It provides support for major audio/video codecs allowing the recording and playback of different media formats including AAC, AVC (H.264), H.263, MP3, and MPEG-4. It also provides support for static image file formats, including jpg and png.

Several other third-party open-source libraries, like

  • SQLite is a powerful and lightweight relational database engine. The same database engine is used in iPhone.
  • Webkit is a fast HTML-rendering engine used by browsers. This is the same engine used in Safari, Chrome, Apple iPhone, and Nokia’s S60 platform.
  • OpenSSL is the secure socket layer for Internet security.
  • Graphics libraries that include SGL and OpenGL for 2D and 3D graphics engines respectively. A surface manager provides a system-wide surface composer to render different drawing surfaces in a frame buffer. Instead of drawing directly to the screen, it makes use of the off-screen buffering. All the drawing commands go into off-screen bitmaps where they are combined with other bitmaps to form the final display the user will see. This allows Android to create visual effects like fancy transitions, transparent windows.

The interesting thing about Android is that you can combine 2-D and 3-D surfaces in a single UI. If a device has the hardware 3D acceleration available, the 3D library will use it. If it doesn’t, the library will use the highly optimized 3D software renderer.

The Java compiler compiles the program written in the Java language and generates the Java byte code. The Android SDK includes the dx tool which generates Dalvik bytecode from compiled Java classes. The output is in.dex (Dalvik executable) format. Dalvik VM is meant to run the Android applications. It executes the .dex files.

ADT (Android Developer Tools) is a plugin for Eclipse that provides a suite of tools that are integrated with the Eclipse IDE. It includes many features that let you develop Android applications easily.

To generate the Dalvik bytecode from the Java bytecode, you can invoke the dx tool directly. But the ADT plugin is pre-configured to generate Dalvik bytecode from your Java source directly. The dx tool is typically called by the ADT, so you rarely need to invoke it directly. In general, you should rely on the ADT plugin to call it as needed.

The dex files are zipped into an Android Package (APK). You can then install the apk file on the Android device.

You might be wondering why the Android team decided to base Dalvik on Java byte code instead of Java source code. Back in 2005, when work on Dalvik started, the Java byte code was more or less unchangeable. But the Java language was still going through numerous changes.

Android Runtime: Main Components

The runtime is what makes a device an Android device rather than a mobile version of the Linux kernel.

Dalvik VM: DVM focuses strictly on mobile devices whereas JVM is designed to be a one-size-fits-all solution. It has a highly CPU-optimized bytecode interpreter that relies on Linux for underlying functionality. Every application has its own instance of the DVM that ensures strict sandboxing of applications. It has a business-friendly license.

compile process with dalvik

For details, click here.(Make it hyper-link and connect it to the next blog)

Core libraries: They are 'close to' the VM and need to ported before the VM can be used. Each device is required to provide them. They form the basis of application framework. Android applications use the core libraries for networking, data structures, concurrency, I/O, etc.

JNI (Java Native Interface) bridge: It is the VM-neutral component of the runtime that defines a way for code written in the Java programming language to access the native code written in C/C++ for e.g. the Android native libraries (Surface Manager, Media framework, libc, etc.). The call signatures i.e. the count and types of the arguments, as well as return type, of your Java methods much match to the corresponding native functions.

Typically an Android application accesses the native libraries ‘indirectly’ through the Java-based core library APIs. But if you need to access the native libraries ‘directly’, you can do so by using the Android Native Development Kit (NDK). The purpose of NDK is to invoke native methods written in non-Java programming languages, for e.g. C/C++, from within Java code using the JNI bridge.

Just-in-time (JIT) compilation: It is the dynamic translation technique for improving the runtime performance of an application. JIT is tightly integrated with the Dalvik interpreter. Therefore, it is useful to consider the JIT as an extension of the interpreter. The JIT is discussed in detail later.