Follow the instructions on this page to build Android.
Set up your build environment
From within your working directory, source the envsetup.sh script to set
up your build environment:
source build/envsetup.shThis script imports several commands that let you work with the Android
source code, including the commands used on this page. To view the source of the
script, refer to
platform/build/envsetup.sh.
To view the built-in help, type hmm.
Choose a target
Before building Android, you must identify a target to build. A target
reflects the target platform you're building for. To identify your
target to build, use the lunch command followed by a string representing
the target. For example:
lunch aosp_cf_x86_64_only_phone-aosp_current-userdebugYou should see a synopsis of your target and build environment:
============================================
PLATFORM_VERSION_CODENAME=Baklava
PLATFORM_VERSION=Baklava
TARGET_PRODUCT=aosp_cf_x86_64_only_phone
TARGET_BUILD_VARIANT=userdebug
TARGET_ARCH=x86_64
TARGET_ARCH_VARIANT=silvermont
HOST_OS=linux
HOST_OS_EXTRA=Linux-6.10.11-1rodete2-amd64-x86_64-Debian-GNU/Linux-rodete
HOST_CROSS_OS=windows
BUILD_ID=BP1A.250305.020
OUT_DIR=out
============================================
The string representing the target has the following format:
lunch product_name-release_config-build_variantThe components of this string are:
The
product_nameis the name of the product you want to build, such asaosp_cf_x86_64_only_phoneoraosp_husky. Your specificproduct_namecan follow your own format for your device, but the format that Google uses for its devices has these components:aosprefers to the Android Open Source Project.- Optionally,
cfis included when the target is intended to be run within the Cuttlefish emulator. - Architecture and hardware codename, such as
x86_64_only_phoneorhusky, which is the codename for Pixel 8 Pro. For a list of codenames for Google devices, see Device codenames.
release_configis set to a release configuration, such as the development release configuration calledaosp_current. A release configuration identifies certain features and code that are behind feature launch flags and are either enabled or disabled for a build. For more on release configurations, see Set feature flag launch values.The
build_variantportion of the string can be one of the three values in the following table:build_variantDescription userThis build variant provides limited security access and is suited for production. userdebugThis build variant helps the device developers understand the performance and power of in-development releases. When developing with a userdebugbuild, follow the Guidelines for userdebug.engThis build variant has faster build time and is best suited for day-to-day development environments (where performance and power are less important).
If you run lunch without any arguments, lunch provides a list of common
targets. To create custom target strings, combine target string elements
with hardware codenames. For a list of codenames, see Device codenames.
View the current target
To see the current lunch settings, run:
$ echo "$TARGET_PRODUCT-$TARGET_BUILD_VARIANT"
Build the code
Run the following command to build your target. Depending on the specification of your workstation, the first build could take less than an hour and up to a few hours. Subsequent builds take significantly less time.
mThe output of your build appears in $OUT_DIR. If you
build different targets, each target build appears in
$OUT_DIR.
The m command builds from the top of the tree, so you can run m from within
subdirectories. If you have the TOP environment variable set, the m command
uses it. If TOP isn't set, the m command looks up the tree from the current
directory, trying to find the top of the tree.
The m command can handle parallel tasks with a -jN argument. If you don't
provide a -j argument, the build system automatically selects a parallel task
count that it thinks is optimal for your system.
You can build specific modules instead of the full device image by listing
module names in your m command line. In addition, the m command provides
some pseudo targets, called goals. For example, m nothing doesn't build
anything, but parses and validates the build structure. For a list of valid
goals, type m help.
Troubleshoot build errors
This section contains instructions for handling build errors.
Read-only file system errors (Android 17 and higher)
During the build, the AOSP source tree is read-only. If the build you're running attempts to modify the source tree during product config or any other part of the build, the build might fail and report read-only file system errors. You can use these options to temporarily change the source tree to read-write:
To change the entire source tree to read-write during the build, add
BUILD_BROKEN_SRC_DIR_IS_WRITABLE=trueto the build environment.To change a portion of the tree to read-write during the build, use
BUILD_BROKEN_SRC_DIR_RW_ALLOWLIST="path1, path2, ...". The paths should be the paths of the directories in which writes should be allowed, relative to the top of the workspace.
Wrong Java version (Android 8.0 and lower)
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. For example, you might get
this message:
************************************************************
You are attempting to build with the incorrect version of java.
Your version is: WRONG_VERSION.
The correct version is: RIGHT_VERSION.
Please follow the machine setup instructions at
https://source.android.com/source/initializing.html
************************************************************
Here are the likely causes and solutions:
- The correct JDK isn't installed. For more information, see Set up for AOSP development (2.3-8.0).
- There's another previously installed JDK appearing in your path. Prepend the correct JDK to the beginning of your path or remove the problematic JDK.