Android Architecture
Covers the deck 3.Basic Android Architecture: the Android story, the layered software stack (Linux kernel, HAL, native libraries, Dalvik and ART, Java API framework, system apps) and what each layer does.
IntroductionSearch on YouTube
Any operating system manages the resources of a device. It gives applications a way to talk to the hardware so they can finish their tasks.
The Android OS powers mobile phones. It does the following:
- manages memory and processes
- enforces security
- handles networking
Android is open source. Most of its code is released under the Apache 2.0 license. Device manufacturers can access it freely, modify it and use it on any device. This freedom is one of the main reasons for its popularity.
The Android storySearch on YouTube
| Year | Event |
|---|---|
| 2007 | The Open Handset Alliance (OHA) forms. It is a group of technology companies, device makers, chipset makers and wireless carriers. Its aim is open standards for the mobile platform. Together they build Android, the first open and free mobile platform, on Linux kernel 2.6. |
| 2008 | HTC Dream is released. It is the first phone to run Android. |
Android ArchitectureSearch on YouTube
Android is a stack of layers that run one above the other.
- Each layer does several operations that support specific OS functions.
- Each layer provides services to the layers above it.
The deck shows the stack in two versions. The newer diagram adds the Hardware Abstraction Layer.
| Layer (top to bottom) | Components shown on the slide |
|---|---|
| System apps | Dialer, Email, Calendar, Camera (native Android apps and third-party apps) |
| Java API framework | Content Providers, View System, Managers (Activity, Location, Package, Notification, Resource, Telephony, Window), XMPP Service |
| Native C/C++ libraries | SQLite, WebKit, OpenGL ES, FreeType, Surface Manager, Media Framework, SSL, SGL, libc |
| Android Runtime | Android Runtime (ART), Core Libraries (older diagram: Dalvik Virtual Machine) |
| Hardware Abstraction Layer (HAL) | Audio, Bluetooth, Camera, Sensors |
| Linux kernel | Drivers: Audio, Binder (IPC), Display, Keypad, Bluetooth, Camera, Shared Memory, USB, WiFi; Power Management, Process Management, Memory Management |
Kind Humans Like Reading Funny Apps
Kernel, HAL, Libraries, Runtime, Framework, Apps (bottom to top).
Linux Kernel LayerSearch on YouTube
Android is built on top of the Linux kernel, with some architectural changes made by Google.
Google chose Linux because it is a portable platform. It compiles easily on different hardware.
The kernel is an abstraction layer between software and hardware. It holds the drivers that control the hardware.
Camera click exampleSearch on YouTube
- The user presses the camera button. This is a hardware instruction.
- The instruction goes to the camera driver in the kernel.
- The driver sends the needed commands to the camera hardware.
- The picture is taken and stored in the gallery. The hardware action has become a software action.
This is the same as what happens when you press a key on a keyboard. The drivers in the kernel command the hardware.
The kernel has drivers for Wi-Fi, Bluetooth, USB, audio, display and more.
The Linux kernel manages the core functions of Android:
- process management
- memory management
- security
- networking
Linux is a proven platform for security and process management.
Hardware Abstraction LayerSearch on YouTube
The Hardware Abstraction Layer (HAL) exposes the device hardware to the high-level Java framework.
- The HAL has several library modules.
- Each module implements an interface for one type of hardware component, such as the camera or Bluetooth module.
- It gives hardware vendors a standard interface. Vendors add device-specific functions without changing higher-level Android code.
Native LibrariesSearch on YouTube
Android's native libraries are written in C or C++. They help the device handle different kinds of data.
| Library | Use |
|---|---|
| SQLite | Store and retrieve data from a database |
| Media Framework | Play and record audio and video |
| WebKit | Browser engine to display web pages |
| Surface Manager | Manage the display surfaces |
| SSL | Secure network communication |
The same layer holds the Android Runtime and the core libraries. The Android runtime runs the applications on the device. Runtime means the time from when an app is launched until it is shut down.
Dalvik Virtual MachineSearch on YouTube
Android apps are written in Java or Kotlin. Compiling them gives bytecode. The JVM is a virtual machine that runs Java bytecode.
Android does not use the JVM. It uses the Dalvik Virtual Machine (DVM).
- DVM runs Dalvik bytecode. This is Java bytecode converted by the Dex compiler.
- The
.classfiles are converted to.dexfiles with the dx tool.
Java source -> javac -> .class (Java bytecode) -> dx tool -> classes.dex (Dalvik bytecode) -> DVM| Point | JVM (Java bytecode) | DVM (Dalvik bytecode) |
|---|---|---|
| Output files | One or more .class files, one per Java file | Only one .dex file |
| Suited to | General computers | Low-memory, low-processing devices |
Each Android app runs in its own instance of the DVM. This is a crucial part of Android security. One app cannot reach into another app's VM.
Android Runtime (ART)Search on YouTube
From Android 5.0 Lollipop, Dalvik was replaced by the Android Runtime (ART).
| Point | Dalvik | ART |
|---|---|---|
| Compilation | Trace-based just-in-time (JIT) | Ahead-of-time (AOT) |
| When code is compiled | Every time the app is launched | Once, at install time, into native machine code |
| Install time | Shorter | Longer |
| Runtime efficiency | Interpretation and JIT at every launch | No interpretation, so faster and lower power use |
| Optimised file | .odex (optimised dex) | ELF executable |
ART uses a utility called dex2oat. It takes DEX files as input and produces a compiled executable for the target device. With ART, the .odex files are replaced by Executable and Linkable Format (ELF) executables.
A common short question: why does ART increase install time but improve performance? Answer: AOT compiles the whole app to native code once at install. This removes interpretation and JIT at each launch, so the app runs faster and uses less power.
Java API Framework LayerSearch on YouTube
The application framework handles the basic functions of the phone, such as resource management and call handling. Installed apps talk directly to this layer.
| Framework block | Role |
|---|---|
| Activity Manager | Controls all parts of the app lifecycle and the activity stack |
| Telephony Manager | Manages all voice calls |
| Content Provider | Manages publishing and sharing of data between apps |
| Resource Manager | Manages resources used in apps: strings, colour settings, UI layouts |
| Notification Manager | Lets apps show alerts and notifications to the user |
| Package Manager | Lets apps find information about other apps installed on the device |
| Location Manager | Location-based services and updates |
System Apps LayerSearch on YouTube
This is the topmost layer. The user interacts with the device directly here. It has two kinds of apps:
- Preinstalled apps (system apps) come with the device. Examples are the dialer, web browser and contacts.
- User-installed apps come from places such as the Google Play Store and Amazon Marketplace.
Step 1. A spy app is a user-installed app in the System Apps layer.
Step 2. It asks the Java API framework (Telephony Manager, Location Manager, Content Provider) for calls, location and contacts.
Step 3. The framework uses native libraries such as SQLite to read stored data.
Step 4. The request reaches hardware through the HAL and kernel drivers (camera, microphone, GPS).
Tracing a request down the stack like this answers scenario questions on which layers are involved.
Quick self-testSearch on YouTube
Who formed the Open Handset Alliance and when?
A group of technology companies, device makers, chipset makers and wireless carriers formed it in 2007. Its aim was open standards for mobile. It built Android on Linux kernel 2.6.
Which was the first Android phone?
The HTC Dream, released in 2008.
List the layers of the Android architecture from bottom to top.
Linux kernel, Hardware Abstraction Layer, native C/C++ libraries with the Android Runtime, Java API framework, system apps.
Why did Google build Android on the Linux kernel?
Linux is portable and compiles easily on different hardware. It is also proven for security and process management.
What is the role of the HAL?
It exposes hardware capabilities to the Java framework. Its library modules implement a standard interface per hardware type, so vendors add device-specific code without changing higher-level Android code.
How does Dalvik bytecode differ from Java bytecode?
Java bytecode is one or more .class files. Dalvik bytecode is a single .dex file made by the dx tool. Dalvik bytecode suits low-memory, low-processing devices.
Compare JIT in Dalvik with AOT in ART.
Dalvik compiles at every launch with trace-based JIT. ART compiles the whole app to native code at install with dex2oat. ART installs slower but runs faster and saves power.
What replaces .odex files under ART?
ELF (Executable and Linkable Format) executables produced by dex2oat.
Name four managers in the Java API framework and their jobs.
Activity Manager (app lifecycle and activity stack), Telephony Manager (voice calls), Package Manager (information on installed apps), Notification Manager (alerts to the user). Also Location and Resource Manager.
Why is a separate VM instance per app important?
It isolates apps from each other. This is a crucial part of Android security.
- Android is open source under Apache 2.0; OHA built it in 2007 on Linux kernel 2.6; HTC Dream (2008) was the first phone.
- The stack is Linux kernel, HAL, native libraries plus runtime, Java API framework, system apps.
- The kernel holds the drivers and does process, memory, security and network management.
- The HAL gives vendors a standard hardware interface per component type.
- Native libraries are C/C++: SQLite, Media Framework, WebKit, Surface Manager, SSL.
- DVM runs one
.dexfile per app (made by dx); each app has its own VM instance. - From Android 5.0, ART uses AOT compilation with dex2oat and ELF executables.
- The framework managers (Activity, Telephony, Package, Location, Notification, Resource, Content Provider) are what apps call directly.