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 useAndroid.mkfiles. 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, andmm).
# 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
MakebecauseMakewas 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
.bpformat: Blueprint defines the strict, JSON-like syntax used inAndroid.bpfiles. - 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.ninjafile.
// 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:
- Environment Setup: You source
build/make/envsetup.shand select your target device usinglunch. - Kati: A tool called Kati parses any legacy
Android.mkfiles and converts them into Ninja build rules. - Soong & Blueprint: Soong parses all modern
Android.bpfiles across the 800+ repositories and converts them into Ninja build rules. - 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