Build Android

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.sh

This 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_phone-trunk_staging-userdebug

You should see is a synopsis of your target and build environment:

============================================
PLATFORM_VERSION_CODENAME=VanillaIceCream
PLATFORM_VERSION=VanillaIceCream
PRODUCT_INCLUDE_TAGS=com.android.mainline
TARGET_PRODUCT=aosp_arm
TARGET_BUILD_VARIANT=eng
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=generic
HOST_OS=linux
HOST_OS_EXTRA=Linux-6.5.13-1rodete2-amd64-x86_64-Debian-GNU/Linux-rodete
HOST_CROSS_OS=windows
BUILD_ID=AOSP.MAIN
OUT_DIR=out
============================================

The string representing the target has the following format:

lunch product_name-release-build_variant

The components of this string are:

  • The product_name is the name of the product you want to build, such as aosp_cf_x86_64_phone or aosp_husky. Your specific product_name can follow your own format for your device, but the format that Google uses for its devices has these components:

    • aosp refers to the Android Open Source Platform.
    • (optional) cf is included when the target is intended to be run within the Cuttlefish emulator.
    • Architecture and hardware (codename), such as x86_64_phone or husky which is the codename for Pixel 8 pro. For list of codenames for Google devices, see Device codenames.
  • The release is set to trunk_staging.

  • The build_variant portion of the string can be one of the three values in the following table:

    build_variant Description
    user This build variant provides limited security access and is suited for production.
    userdebug This build variant helps the device developers understand the performance and power of in-development releases. When developing with a userdebug build, follow the Guidelines for userdebug.
    eng This build variant has faster build time and is best suited for day-to-day development if you don't care about performance and power.

If you run lunch without any arguments, a list of common targets is provided. You can also create your own target strings by piecing together the elements of the target string using the information on this page and the codenames that represent specific Google hardware at 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.

m

The 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 (8.0 or earlier)

If you're building AOSP 8 or earlier, 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:

  • You failed to install the correct JDK as specified in the JDK sections of 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.