Building an existing Android source tree means turning a downloaded codebase into installable images. For a full platform build (AOSP or a custom ROM), you run Google's repo sync, install the host dependencies, pick a target with lunch or a device-specific setup script, then make or m. Expect roughly 150 GB of disk and a Linux machine; a typical 64-bit build on a fast workstation takes one to several hours depending on hardware and target.
The "existing source" part matters. You are not writing an app from scratch; you are compiling someone else's tree, so the branch, device codename, Java version, and vendor blobs all have to match. Most first-time failures come from mismatched versions, not from bad code.
First Decide What "Existing Source Code" You Have
The build steps diverge sharply depending on which of these you downloaded, so identify it before touching a terminal.
An AOSP tree is Google's reference platform, fetched with repo. It builds generic images and, for supported devices, device-specific targets. The official workflow is documented in Google's Download the Android source guide.
A custom ROM tree, like the ExtremeROM example in the search results, is a fork with device configs and helper scripts (buildenv.sh, a codename argument). It usually carries its own dependency list and supported-model table.
An app project (Gradle, Expo, Flutter, React Native) is a different animal. You build an APK or AAB, not a system image. Expo's APK build reference is the relevant path if your "source" is a mobile app.
A kernel tree is separate again; Google's Build kernels page covers pulling kernel sources with repo and embedding the result into a system image.
Each path shares the same opening moves (Linux, disk, RAM, dependencies) but diverges at the target-selection step. Picking the wrong guide wastes an afternoon.
Prerequisites You Should Have Ready Before Downloading
A full platform build is heavy. Plan for a Linux PC with a recent 64-bit distribution; Ubuntu 24.04 LTS is a common recommendation because its packaged toolchain versions line up with current Android requirements. WSL can work, but only on an ext4 filesystem and with a distro that supports f2fs; NTFS-mounted directories will break file permissions and symlinks mid-build.
Storage: around 150 GB free is the practical floor for a single target. Source plus out directory for one device easily crosses 100 GB, and ccache can add tens more. RAM: about 8 GB is a working minimum; 16 GB or more keeps parallel jobs from thrashing swap. Java matters more than people expect—Android 12-era trees want OpenJDK 11, and mixing 17 or 21 in produces cryptic javac errors before the build even reaches native code.
You also need a way to flash or test the result. An unlocked bootloader device, or an emulator for generic targets, plus adb and fastboot on your PATH.
Step 1: Install the Host Dependencies
Every guide starts here, and the exact package list depends on your distro. The pattern is the same: compiler, build tools, Java, compression and crypto libraries, and Python.
On Ubuntu or Debian:
The ExtremeROM guide lists a longer set including attr, golang, libbrotli-dev, libgtest-dev, libpcre2-dev, libunwind-dev, zipalign, and patchelf. Install what your tree's README requests; missing libraries surface as link errors late in the build.
On Arch, the same packages come from pacman, plus android-sdk-build-tools from the AUR. On Fedora, dnf with the -devel suffixes. Debian bookworm ships OpenJDK 17 by default, so you may need to add the bullseye repository to pull openjdk-11-jdk, then rerun the install.
Check: java -version should report 11.x. If it reports 17 or 21, fix it now; do not proceed.
Step 2: Fetch the Source Tree
For an AOSP tree, create a working directory and initialize repo:
The -b flag pins the branch. Change it to match your target—android-12.1.0_r* for 12L, or a newer release branch if you are building current AOSP. --partial-clone saves bandwidth by fetching history lazily, which is fine for building but bad if you plan to inspect old commits.
For a custom ROM, follow its own instructions. ExtremeROM's flow is a single git clone --recurse-submodules . The --recurse-submodules flag is not optional; missing submodules produce "file not found" errors deep in the build. Note that repository was archived in January 2026 and is read-only, so treat it as a reference example rather than a live project.
Set your Git identity before committing anything:
Check: repo status (AOSP) or git submodule status (custom ROM) should show a clean tree with all submodules populated. An empty submodule directory is the single most common cause of a build that fails twenty minutes in.
Step 3: Select the Build Target
This is where AOSP and custom ROMs part ways.
In AOSP, you source the environment and pick a target with lunch:
For a physical device, the target looks like aosp_-userdebug (for example, a Pixel or a Sony Xperia with an AOSP device config). Sony's AOSP build instructions for Xperia open devices walk through the device-specific variant for 12L. The build variant matters: userdebug keeps adb root and debugging, user is production-strict, eng adds extra tools.
In a custom ROM, target selection often happens inside a helper script. ExtremeROM uses:
Running . buildenv.sh with no argument prints the supported codenames. If yours is not listed, the tree does not support your device and no amount of make will fix that.
Check: after lunch or buildenv.sh, your shell prompt usually changes and echo $TARGET_PRODUCT returns a non-empty value. If the target is blank, the setup script failed silently—read its output before continuing.
Step 4: Run the Build
With the target selected, kick off the build:
m is AOSP's wrapper around make that picks up the lunch environment. $(nproc) sets parallelism to your core count; drop it to $(nproc)/2 if you have 8 GB of RAM or less, because each job can consume over a gigabyte during linking.
The build proceeds in stages: soong bootstrap, then framework, then system image assembly, then vendor and product images. You will see long silent stretches during individual module builds; that is normal. Real failures announce themselves with FAILED: and a module name.
For an app project rather than a platform, the command is different. Gradle uses ./gradlew assembleRelease for an APK; Expo uses eas build -p android --profile preview with android.buildType: "apk" set in eas.json, as the Expo reference describes. The AAB/APK distinction matters: Play Store wants AAB, direct device install wants APK.
Check: success ends with a line naming output files under out/target/product//, typically system.img, boot.img, vendor.img, and a flashable *-img.zip. If the build exits non-zero, scroll to the first FAILED: line, not the last—the final error is often a cascade.
Reading Failures Without Guessing
Most build failures fall into four buckets, and each has a cheap first check.
Missing dependency or wrong version. Symptoms: javac errors about source level, or linker errors about missing .so files. Check java -version and confirm every package your tree's README lists is installed. The Sony guide's purge-and-reinstall step for Java exists precisely because conflicting JDKs cause this.
Missing submodules or vendor blobs. Symptoms: No such file or directory for a device config or proprietary binary. Run git submodule status and re-sync. Vendor blobs usually come from a separate extraction step documented in the device tree, not from the main repo.
Out of memory. Symptoms: the kernel OOM killer terminates clang or javac, often with no clear error. Reduce -j and retry. This is reversible and costs only time.
Stale out directory after a branch change. Symptoms: bizarre type mismatches after you switch branches without make clean. Remove out/ and rebuild; a clean build is slow but deterministic.
Start with the reversible checks—parallelism, Java version, submodule status—before deleting anything. If the failure persists after a clean build with correct dependencies and a supported target, the tree likely does not support your device or branch, and that is where further work needs the ROM maintainer or device-specific documentation.
What "Done" Actually Looks Like
A finished platform build gives you images you can flash. For a device with an unlocked bootloader, reboot to bootloader and flash the relevant partitions, or use the generated *-img.zip with the vendor's flashing tool. For a generic target, boot the image in the emulator that ships with the Android SDK.
Before flashing anything, back up the device. Flashing a mismatched boot.img can soft-brick a phone, and recovery usually requires the stock firmware from the manufacturer—which you should download before you start, not after.
Verify the build by booting it and checking Settings > About phone for the expected build fingerprint and branch. If the device boots to a black screen, the vendor blobs or kernel likely do not match the source branch; that is a sign of a device-config mismatch, not a compiler problem.
Realistic Time and Hardware Expectations
A first full AOSP build on a 16-core machine with 32 GB RAM and an SSD typically runs one to three hours after the source is synced. Incremental builds after a small change take minutes because ccache and soong reuse artifacts. A cold sync of AOSP over a residential connection can take hours on its own, which is why --partial-clone exists.
Custom ROM trees are usually smaller and faster, but only if the device configs are complete. A ten-year-old laptop with 8 GB of RAM will finish eventually; it will just spend much of the build in swap, and you will want -j4 rather than -j16.
Budget for at least one failed build. Everyone gets one, usually from a Java mismatch or a missed submodule, and the fix is a version check rather than a code change.