Building existing Android source code means compiling the Android Open Source Project (AOSP) tree you already have on disk into bootable or flashable images—or, if you only need one module, into a specific artifact such as system.img or an APK. The key condition is that your host machine, Java version, disk space, and target must match the branch you're building; a mismatch there causes most failures before compilation even starts. As of 2026, AOSP source is published on a twice-yearly cadence and Google directs builders to the android-latest-release manifest branch, so confirm your checkout's branch and release configuration before you run anything.
The steps below assume a Linux host with the source already synced, repo available, and enough free disk. They cover the standard platform build first, then kernel and single-module variants, with checks at each stage.
Before You Type a Command: Prerequisites That Actually Matter
AOSP builds are unforgiving about the host environment, and Google's own setup guidance is blunt about it: before downloading or building the android-latest-release manifest branch, make sure your hardware meets the requirements and the required software is installed. The four named dependencies are OpenJDK, Make, Python 3, and Repo. Recent release branches ship prebuilt versions of OpenJDK, Make, and Python 3, so you may not need to install those separately—but you do need repo on your PATH.
Three practical prerequisites deserve more attention than they usually get:
Disk space and inode count. A full AOSP checkout plus build output easily consumes several hundred gigabytes, and the count varies by branch, target, and whether you keep multiple out/ directories. If you are short on space, use --partial-clone when initializing (Google's download instructions include it) and build with -j values your RAM can support rather than swapping.
Case-sensitive filesystem. AOSP does not build reliably on case-insensitive filesystems. On Linux this is normally fine; on macOS or Windows-mounted volumes, verify before syncing.
Java consistency. Google's build page explicitly warns that if you're building Android 8.0 (API level 26) and lower, m might abort when it encounters an issue with your version of Java. Modern branches handle this through the prebuilt toolchain, but if you're building an older tree, check the required JDK version for that specific branch rather than assuming the newest JDK works.
If any of these checks fail, stop and fix the environment. Continuing produces long, confusing compilations that fail late.
Step 1: Enter the Source Tree and Set Up the Build Environment
From within your working directory—the top of the tree created by repo init and repo sync—source the environment setup script:
This imports several commands that let you work with the Android source code, including the commands used throughout the build, such as lunch and m. Google notes you must run source envsetup.sh once per shell and before the lunch command, because lunch is defined by envsetup.sh. Opening a new terminal means repeating this step.
To see what the script exposes, type hmm for the built-in help. The script's source lives at platform/build/envsetup.sh in the android17-release branch referenced by Google's documentation.
Success check: after sourcing, lunch and m are recognized commands in that shell. Failure symptom: lunch: command not found almost always means you skipped the source step or sourced the script from the wrong directory.
Step 2: Choose a Target
Before building Android, you must identify a target, which reflects the platform you're building for. Use the lunch command followed by a string representing the target. Google's example:
You should see a synopsis of your target and build environment, including lines such as:
The target string follows the format product_name-release_config-build_variant. The product_name is the product you want to build, such as aosp_cf_x86_64_only_phone or aosp_husky (the codename for Pixel 8 Pro). Google's naming convention includes aosp for the Android Open Source Project and, optionally, cf when the target runs in the Cuttlefish emulator, followed by architecture and hardware codename. The build variant controls debuggability: userdebug is the common choice for development, while user is closer to a production configuration and eng is for engineering builds with extra tooling.
Don't guess the target string. Run lunch with no argument in your shell to list the products your tree defines. If your device isn't in the list, its product configuration isn't in your checkout—you need the right manifest branch or a vendor tree, not a workaround.
Step 3: Build
With the environment sourced and a target lunched, run the build:
The m command builds from the top of the tree using whatever target you selected. For more control, m accepts module names and options; the older make invocation remains available but m is the documented path on current branches. Set parallelism according to your CPU and RAM—too aggressive a -j value on a low-memory machine causes the compiler to be killed mid-link.
What to expect: a full build from a cold out/ directory takes hours on a typical workstation; incremental rebuilds after small edits are much faster because outputs are cached in out/. Success check: the build finishes without errors and images appear under out/target/product//. Failure symptom: errors early in the run usually mean environment or target problems; errors deep into compilation usually mean branch-specific toolchain or source issues.
Step 4: Verify the Output Before You Flash Anything
A build that "succeeds" but produces unusable images is a real outcome, especially when you've mixed source trees or target definitions. Confirm three things:
The expected image files exist in out/target/product//, typically including system.img, boot.img, and vendor.img depending on the target and branch. The build variant in the lunch synopsis matches what you intended (userdebug versus user matters for what you can debug later). The BUILD_ID shown in the synopsis corresponds to the branch you synced; a mismatch suggests your checkout or lunch selection is stale.
For emulator-capable targets such as the Cuttlefish example Google documents, launching the emulator is the most direct functional check. For hardware targets, flashing is the real test—and the fastest way to discover a target mismatch is to flash before verifying.
Building the Kernel Separately
Your Android tree contains prebuilt kernel binaries; the kernel sources and the tools to build them live in a separate kernel checkout. Google's kernel documentation puts this plainly: the root of the kernel source checkout contains tools/bazel, and the Android tree contains only prebuilt kernel binaries. The kernel trees contain the kernel sources and all tools to build them, including Bazel.
For recent kernels, use repo to download the sources, toolchain, and build scripts:
Some kernels require sources from multiple Git repositories, while others—such as the common kernels—require only a single source; the repo approach ensures a correct source directory setup. You can switch among branches within one checkout, because the common kernel manifests and most others define the kernel git repository to be cloned fully rather than shallow. Android 13 introduced building kernels with Bazel, referred to as Kleaf, so check which build system your branch supports before choosing commands.
The build itself uses Kleaf/Bazel to produce a distribution for the GKI kernel for the target architecture. After building, embed the result into a system image built from AOSP—the kernel image is only useful to your platform build if it replaces the prebuilt binary your tree expects.
Building a Single Module Instead of the Whole Tree
Most day-to-day work doesn't need a full platform build. After sourcing envsetup.sh and lunching a target, you can build one module by name:
The module name is defined in the corresponding Android.bp (or legacy Android.mk) file, and m resolves it against the tree. This is much faster and avoids rebuilding unrelated parts of the platform. Two caveats: module builds still require a lunched target because they inherit its configuration, and some modules depend on generated artifacts that only a full build produces, so a first full build on a fresh checkout is often the pragmatic warm-up.
Check: the artifact appears in out/ under a path matching the module's install location. Stop condition: if a module build fails with missing generated headers or system libraries that exist in a full build, run the full build rather than trying to hand-stage dependencies.
Handling Branch Changes Mid-Build
Because AOSP publishes to android-latest-release on a twice-yearly schedule, your checked-out branch can fall behind while you're working. Switching branches inside the same repo checkout is supported—Google's download docs use -b to identify the branch during init, and the kernel docs describe re-initializing with a new -b and syncing. When you change branches:
Re-run repo sync to bring the tree in line. Clear or relocate out/, because object files from a different branch can produce confusing link errors. Re-run source build/envsetup.sh and lunch in a fresh shell.
Failure symptom: bizarre compile errors immediately after a branch switch, in code you never touched, almost always trace back to a stale out/ directory. Deleting it is reversible—a rebuild just takes time.
Common Failure Modes and How to Tell Them Apart
Most build problems fall into a few buckets, and distinguishing them saves hours:
Environment failures show up before or at the start of compilation: missing repo, wrong Java version on older branches, or a shell that never sourced envsetup.sh. These are fully reversible checks; fix them first. Target failures show up at lunch: an unknown target string. Fix by listing products and selecting a real one, or syncing the branch that includes your device. Disk and memory failures show up as killed compiler processes or "No space left on device." Reduce -j, free space, or both. Source failures show up as compile errors in code that should be stable. Re-sync the branch, check for local modifications with repo status, and rebuild from a clean out/.
A specific stop condition: if you're building for physical hardware and the images fail to boot after a verified clean build and correct target selection, stop flashing and consult the device's vendor or platform documentation. Repeated flashes of mismatched images can leave a device in a state that requires qualified recovery support—that work isn't something a generic build guide can safely walk you through.
What "Existing Source Code" Changes About the Process
The phrase matters. If you inherited a checkout rather than creating it, you may not know which branch, manifest revision, or local modifications it carries. Before building, establish those facts:
repo manifest shows the manifest and revisions pinned by the checkout. repo status reveals local edits that could explain unexpected failures. The .repo/manifests directory identifies the manifest repository and branch. The lunch synopsis's BUILD_ID and platform version confirm what the tree expects to produce.
Building an inherited tree without this audit is the most common source of "it builds on my machine" mismatches. Ten minutes of inspection beats a six-hour build that produces images for the wrong target.
FAQ
Can I build AOSP on Windows? The documented host is Linux; Google's build synopsis shows HOST_OS=linux and a Linux distribution, and the toolchain assumptions follow from that. Windows users typically work through WSL or a Linux virtual machine, with the case-sensitivity and disk caveats above.
Do I need to build the kernel to build Android? No. The Android tree contains prebuilt kernel binaries, so a platform build works without a kernel checkout. You only need the separate kernel build when you're changing kernel code or supplying a custom kernel for your target.
How often does the published source change? Google states that effective in 2026, source is published to AOSP in Q2 and Q4 under the trunk stable development model, and that builders should use the android-latest-release manifest branch, which always references the most recent AOSP release. Sync accordingly rather than assuming a continuous stream.
Why does my build fail after switching branches? Almost always a stale out/ directory combined with a shell still holding the old environment. Clear out/, source envsetup.sh in a fresh shell, and lunch the new target.