AOSP Foundations
3 min read

Build Optimization

Master the techniques required to drastically reduce AOSP compilation times, including ccache and parallel processing.

A complete ("clean") build of the Android Open Source Project can take anywhere from 1 hour on a massive enterprise workstation to 12 hours on a standard developer laptop. Because you will be compiling AOSP constantly, optimizing your build times is essential for maintaining your sanity and productivity.

Parallel Build Flags (-j)

By default, the modern AOSP build system (Ninja) is highly intelligent. When you run the standard build command m, Ninja will automatically detect how many CPU cores and threads your host machine has, and it will spawn an appropriate number of parallel compilation tasks to maximize your CPU utilization.

However, you can manually override this using the -j (jobs) flag.

  • Example: m -j16 forces the build system to run exactly 16 concurrent compilation jobs.
  • When to use it: If you have a 32-core machine but you want to continue browsing the web and writing code without your computer completely freezing, you might restrict the build to m -j24, leaving 8 cores free for your desktop environment.
# Build AOSP using exactly 4 concurrent threads
m -j4

The Compiler Cache (ccache)

The single most important optimization you can enable is ccache (Compiler Cache).

When you change a single line of Java code in the framework, you do not want the build system to recompile the 50 million lines of C++ code in the WebKit browser engine. While the build system is smart enough to skip unmodified files, ccache takes this a massive step further by caching the intermediate object files (.o) generated by the C/C++ compiler on your hard drive.

If you ever run a make clean (which wipes out the build output directory), ccache can restore the compiled C++ objects from its hidden cache, turning a 3-hour rebuild into a 20-minute rebuild.

Setting up ccache

To enable ccache in your terminal before building, you simply export an environment variable:

export USE_CCACHE=1
export CCACHE_EXEC=/usr/bin/ccache

You then need to allocate disk space for the cache (usually 50GB to 100GB is highly recommended):

ccache -M 50G

# To view your current cache hit rate and statistics:
ccache -s

Incremental Builds

When actively developing, you should almost never perform a full clean build. AOSP supports incremental builds natively. If you modify a file in frameworks/base/, you simply type m again. The Ninja build graph will quickly evaluate the timestamps of the files, realize only the framework has changed, and recompile only that specific module and anything that directly depends on it.

Module-Specific Builds

If you are working on a single application (like the Settings app), you can tell the build system to compile only that application rather than checking the entire OS tree.

By typing m Settings, the system will bypass the full OS check and instantly compile the Settings APK, saving immense amounts of time during rapid prototyping.

# Compile only the Settings app and push it directly to a connected phone
m Settings
adb install -r out/target/product/generic/system/app/Settings/Settings.apk