jp

AOSP architecture, build and boot

28 April 2025, 5 minute read

Android's layers from kernel to apps, how the build moved from Make to Soong, and every stage from power-on to the launcher.

The complete stack

Android is a stack of layers, and each one only talks to its neighbours. Apps see the framework, the framework sees native libraries and the runtime, and everything below the HAL belongs to the hardware.

  1. Applications

    • System apps
    • Third-party apps
  2. Application framework

    • Activity Manager
    • Package Manager
    • Window Manager
    • Telephony Manager
    • Location Manager
    • Resource Manager
    • Content Providers
    • View System
  3. Libraries and runtime

    Native C and C++

    • libc (bionic)
    • Media framework
    • OpenGL ES
    • SQLite
    • SSL
    • WebKit

    Android Runtime

    • ART, ahead of time
    • JIT compiler
    • Core libraries
    • Memory management
  4. Hardware abstraction layer

    • Audio HAL
    • Camera HAL
    • Sensors HAL
    • and the rest
  5. Linux kernel

    • Drivers
    • Memory management
    • Process management
    • I/O
Read it from the bottom up: that is the order a device comes to life in.

From Make to Soong

Android grew too large for plain Make. The build first sped Make up, then replaced the way modules are described altogether.

  1. Android.mk

    Modules described in Make

  2. Kati

    Google's Make clone, much faster at parsing

  3. Ninja

    Runs the actual commands

  4. Images

    system.img, vendor.img and friends

The legacy path, still used for Android.mk modules.
  1. Android.bp

    Declarative Blueprint files

  2. Soong

    A Go program that reads them

  3. Ninja

    Runs the actual commands

  4. Images

    The same output

The modern path, for Android.bp modules.

Both paths meet at Ninja, which is why a mixed tree of .mk and .bp files still builds as one.

Build commands

Set up the environment once per shell, then pick what to build.

Shell
source build/envsetup.sh
hmm
lunch aosp_arm64-eng

Build everything, one module, or a directory:

Shell
m
m Settings
mm
mma
mmm packages/apps/Settings packages/apps/Launcher3
mmma packages/apps/Settings frameworks/base
m -j8
m -k

mm builds the modules in the current directory, mma adds their dependencies, and mmm and mmma do the same for directories you name. -k keeps going when some targets fail.

Search the tree and move around it:

Shell
cgrep "pattern"
jgrep "pattern"
resgrep "pattern"
mangrep "pattern"
sepgrep "pattern"
sgrep "pattern"
godir filename
gomod module_name
pathmod module_name
refreshmod
allmod

Inspect the build and clean it:

Shell
m showcommands
m dist
m checkbuild
m modules
m clean-Settings
m installclean
m clobber

Where things live in the source

  • art/The Android Runtime
  • compiler/AOT and JIT compilers
  • runtime/The runtime itself
  • dex2oat/Turns DEX into OAT
  • bionic/Android's C library
  • libc/The C library
  • libm/Maths
  • linker/The dynamic linker
  • frameworks/System services and APIs
  • base/Framework APIs and services
  • core/java/android/
  • services/core/
  • native/
  • av/Audio and video
  • system/Core OS pieces
  • core/init, logd and friends
  • sepolicy/SELinux policy
  • vold/The volume daemon
  • hardware/HAL implementations
  • interfaces/HAL interfaces
  • libhardware/The legacy HAL

The boot process

From the power button to the home screen, each stage hands over to the next.

  1. Power on

    The power management unit brings the hardware up.

  2. Bootloader

    Basic hardware checks, loads the kernel into memory, and offers recovery and fastboot.

  3. Kernel

    Initialises drivers, sets up memory management and starts the core subsystems.

  4. init

    Mounts filesystems, loads the SELinux policy, starts the property service and parses init.rc.

  5. Zygote

    Creates the runtime, preloads common classes and resources, and waits to launch apps.

  6. system_server

    Starts ActivityManager, PackageManager, WindowManager and PowerManager.

  7. System UI

    Brings up the status bar and navigation, then launches the home screen.

How system_server starts services

system_server starts its services in three passes, so that the ones everything else depends on are ready first.

startBootstrapServices()

  • Installer
  • ActivityManager
  • PowerManager
  • DisplayManager
  • PackageManager
  • SensorService

startCoreServices()

  • BatteryService
  • UsageStatsService
  • WebViewUpdateService
  • GpuService

startOtherServices()

  • CameraService
  • AlarmManager
  • InputManager
  • WindowManager
  • BluetoothService
  • AudioService
  • NotificationManager

Device and board configuration

A device is described in two makefiles. device.mk says what the product is and what goes into it:

Makefile
PRODUCT_NAME := aosp_device
PRODUCT_DEVICE := device
PRODUCT_BRAND := vendor
PRODUCT_MODEL := Device Name
PRODUCT_MANUFACTURER := Vendor

$(call inherit-product, device/vendor/device/device-common.mk)

DEVICE_PACKAGE_OVERLAYS += device/vendor/device/overlay

BoardConfig.mk describes the hardware it runs on:

Makefile
TARGET_ARCH := arm64
TARGET_CPU_ABI := arm64-v8a
TARGET_CPU_VARIANT := cortex-a53

TARGET_KERNEL_SOURCE := kernel/vendor/device
TARGET_KERNEL_CONFIG := device_defconfig

BOARD_BOOTIMAGE_PARTITION_SIZE := 67108864
BOARD_SYSTEMIMAGE_PARTITION_SIZE := 3221225472

Runtime resource overlays

An overlay replaces resources in another package without touching its source. At runtime the app sees one merged set.

The app's resources

  • res/values/
  • res/layout/
  • res/drawable/

The overlay

  • overlay/res/values/

What the app sees

  • values, from the overlay
  • layout/
  • drawable/

Making builds faster

Cache C and C++ compilation:

Shell
export USE_CCACHE=1
export CCACHE_DIR=/path/to/ccache
prebuilts/misc/linux-x86/ccache/ccache -M 50G

Then rebuild only what changed, on every core:

Shell
m installclean && m
m -j$(nproc)
mmm frameworks/base/services

A change, start to finish

  1. Change

    • git status
    • git add
    • git commit
  2. Build

    • mm
    • mma
  3. Test

    • atest
    • CTS and VTS
  4. Debug

    • logcat
    • debuggerd
    • systrace
  5. Submit

    • repo upload
    • Gerrit review