AOSP Foundations
3 min read

The build/ Directory

Discover the heart of the AOSP build system, including Make, Soong, Blueprint, and the environment setup scripts.

The build/ directory is the engine room of the Android Open Source Project. It does not contain the code that runs on an Android phone. Instead, it contains the intricate scripts, compilers, and tools required to transform the other 800+ repositories into a bootable operating system.

When you type m to compile Android, the entire process is orchestrated by the tools housed within this directory.

Core Build Systems

AOSP historically used a traditional Makefile-based build system, but it has since transitioned to a modern, highly parallelized system. Both exist within this directory.

build/make

This subdirectory contains the legacy build system components.

  • Android.mk: Even though AOSP has moved away from Make, many older components still use Android.mk files. The scripts here parse those legacy files and translate them into a format the modern build system understands.
  • envsetup.sh: This is the most famous script in AOSP. Sourcing this file injects crucial helper commands into your terminal (like lunch, m, croot, and mm).
# Sourcing the environment setup script is always step 1 of building AOSP
source build/envsetup.sh

build/soong

Soong is the modern, primary build system for Android, written almost entirely in Go.

  • Purpose: Soong was built to replace Make because Make was incredibly slow at parsing thousands of build files concurrently.
  • Execution: Soong reads configuration files (named Android.bp), resolves all the dependencies (e.g., figuring out that the Settings app requires a specific Java library), and generates a massive, highly optimized build graph.

build/blueprint

Blueprint is the language and parser that Soong relies on.

  • The .bp format: Blueprint defines the strict, JSON-like syntax used in Android.bp files.
  • Role: While Soong contains the Android-specific build logic (like how to compile an APK), Blueprint is a generic framework that parses the files and generates a build.ninja file.
// Example of a Blueprint definition in an Android.bp file
cc_binary {
    name: "my_custom_daemon",
    srcs: ["main.cpp"],
    shared_libs: ["liblog", "libbase"],
}

The Compilation Flow

When you compile AOSP, the build/ directory executes a multi-stage pipeline:

  1. Environment Setup: You source build/make/envsetup.sh and select your target device using lunch.
  2. Kati: A tool called Kati parses any legacy Android.mk files and converts them into Ninja build rules.
  3. Soong & Blueprint: Soong parses all modern Android.bp files across the 800+ repositories and converts them into Ninja build rules.
  4. Ninja: Finally, the Ninja build system (located in prebuilts/) takes the combined rules and executes the actual C++ and Java compilers in parallel, utilizing every available CPU core.

Understanding the layout of the build/ directory is essential if you plan to add new system services, integrate third-party libraries, or configure new hardware targets.

# You can force a specific build stage to debug Ninja rules
m generate_ninja_build