Build kernels

This page details the process of building custom kernels for Android devices. These instructions guide you through the process of selecting the right sources, building the kernel, and embedding the results into a system image built from the Android Open Source Project (AOSP).

Download sources and build tools

For recent kernels, use repo to download the sources, toolchain, and build scripts. Some kernels (for example, the Pixel 3 kernels) require sources from multiple git repositories, while others (for example, the common kernels) require only a single source. Using the repo approach ensures a correct source directory setup.

Download the sources for the appropriate branch:

mkdir android-kernel && cd android-kernel
repo init -u https://android.googlesource.com/kernel/manifest -b BRANCH
repo sync

For a list of repo branches (BRANCH) that can be used with the previous `repo init` command, see Kernel branches and their build systems.

For details on downloading and compiling kernels for Pixel devices, See Building Pixel Kernels.

Build the kernel

Build with Bazel (Kleaf)

Android 13 introduced building kernels with Bazel.

To create a distribution for the GKI kernel for the aarch64 architecture, check out an Android Common Kernel branch no earlier than Android 13 and then run the following command:

tools/bazel run //common:kernel_aarch64_dist [-- --destdir=$DIST_DIR]

Thereafter the kernel binary, modules, and corresponding images are located in the $DIST_DIR directory. If --destdir is unspecified, see output of the command for the location of the artifacts. For details, refer to the documentation on AOSP.

Build with build.sh (legacy)

For branches at or below Android 12, OR branches without Kleaf:

build/build.sh

The kernel binary, modules, and corresponding image are located in the out/BRANCH/dist directory.

Build the vendor modules for the virtual device

Android 13 introduced building kernels with Bazel (Kleaf), replacing build.sh.

To create a distribution for the virtual_device's modules, run:

tools/bazel run //common-modules/virtual-device:virtual_device_x86_64_dist [-- --destdir=$DIST_DIR]

For more details on building Android kernels with Bazel, see. Kleaf - Building Android Kernels with Bazel.

For details on Kleaf support for individual architectures, see Kleaf support for devices and kernels.

Build the vendor modules for the virtual device with build.sh (legacy)

In Android 12 Cuttlefish and Goldfish converge, so they share the same kernel: virtual_device. To build that kernel's modules, use this build configuration:

BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 build/build.sh

Android 11 introduced GKI, which separates the kernel into a Google-maintained kernel image and vendor maintained-modules, which are built separately.

This example shows a kernel image configuration:

BUILD_CONFIG=common/build.config.gki.x86_64 build/build.sh

This example shows a module configuration (Cuttlefish and Emulator):

BUILD_CONFIG=common-modules/virtual-device/build.config.cuttlefish.x86_64 build/build.sh

Run the kernel

There are multiple ways to run a custom-built kernel. The following are known ways suitable for various development scenarios.

Embed into the Android image build

Copy Image.lz4-dtb to the respective kernel binary location within the AOSP tree and rebuild the boot image.

Alternatively, define the TARGET_PREBUILT_KERNEL variable while using make bootimage (or any other