Skip to content

AAOS - Build Android Linux Kernel

The AOSP tree contains only prebuilt kernel binaries. However, vendors always need to add customized modules into the kernel.

For Android Kernel, refer to https://source.android.com/docs/setup/build/building-kernels,
For Cuttlefish device, refer to https://source.android.com/docs/devices/cuttlefish/kernel-dev.

Prebuilt Kernel


When and AVD image is built, it copies the prebuilt kernel image to the output folder:

mgrep "sdk_car_x86_64"
./device/generic/car/AndroidProducts.mk:24: $(LOCAL_DIR)/sdk_car_x86_64.mk \
./device/generic/car/AndroidProducts.mk:32: sdk_car_x86_64-trunk_staging-userdebug \

$(LOCAL_DIR)/sdk_car_x86_64.mk is expanded to device/generic/car/sdk_car_x86_64.mk.
This makefile inherates from other makefiles:

device/generic/car/sdk_car_x86_64.mk
$(call inherit-product, $(SRC_TARGET_DIR)/product/core_64_bit_only.mk)
$(call inherit-product, packages/services/Car/car_product/build/car_generic_system.mk)
$(call inherit-product, packages/services/Car/car_product/build/car_system_ext.mk)
$(call inherit-product, device/generic/car/emulator/car_emulator_product.mk)
$(call inherit-product, device/generic/car/emulator/car_emulator_vendor.mk)
$(call inherit-product, device/generic/goldfish/board/emu64x/details.mk)

Kernel is defined in the makefile device/generic/goldfish/board/emu64x/details.mk:

device/generic/goldfish/board/emu64x/details.mk
include device/generic/goldfish/board/kernel/x86_64.mk
PRODUCT_COPY_FILES += \
$(EMULATOR_KERNEL_FILE):kernel-ranchu \
cat device/generic/goldfish/board/kernel/x86_64.mk
TARGET_KERNEL_USE ?= 6.6
KERNEL_ARTIFACTS_PATH := kernel/prebuilts/$(TARGET_KERNEL_USE)/x86_64
VIRTUAL_DEVICE_KERNEL_MODULES_PATH := kernel/prebuilts/common-modules/virtual-device/$(TARGET_KERNEL_USE)/x86-64
BOARD_SYSTEM_KERNEL_MODULES := $(wildcard $(KERNEL_ARTIFACTS_PATH)/*.ko)
EMULATOR_KERNEL_FILE := $(KERNEL_ARTIFACTS_PATH)/kernel-$(TARGET_KERNEL_USE)

The variable EMULATOR_KERNEL_FILE is finally expanded to kernel/prebuilts/6.6/x86_64/kernel-6.6:

file kernel/prebuilts/6.6/x86_64/kernel-6.6
Linux kernel x86 boot executable bzImage, version 6.6.9-android15-0-g515a956763d8-ab11275718 (build-user@build-host) #1 SMP PREEMPT Thu Jan 4 21:38:14 UTC 2024, RO-rootFS, swap_dev 0X13, Normal VGA

Compile Kernel


Android Kernel is hosted in a different repo from the AOSP project. Check the branches and their build systems at https://source.android.com/docs/setup/reference/bazel-support.

  • Android 13 introduced building kernels with Bazel (Kleaf) - it is super fast
  • Older Android version is build.sh, which is quite slow

For Cuttlefish device, refer to https://source.android.com/docs/devices/cuttlefish/kernel-dev.

Cuttlefish supports the following kernel manifests on aosp-main repo,
branches: common-android14-6.1, common-android14-5.15, common-android-mainline

Define macros:

export AOSP_HOME="$HOME/aosp"
export KERNEL_BRANCH="common-android14-6.1"

Initialize the repo:

mkdir -p ${AOSP_HOME}/${KERNEL_BRANCH}
cd ${AOSP_HOME}/${KERNEL_BRANCH}
repo init --depth=1 \
-u https://android.googlesource.com/kernel/manifest \
-b ${KERNEL_BRANCH}

Download source code:

repo sync -c -j$(nproc)

Build Virtual kermel and modules:

tools/bazel run //common-modules/virtual-device:virtual_device_x86_64_dist
[dist] INFO: Copying to /home/trongvq/aosp/kernel-common-android14-6.1/out/virtual_device_x86_64/dist

Check the Kernel image:

file out/virtual_device_x86_64/dist/bzImage
Linux kernel x86 boot executable bzImage, version 6.1.99-android14-11-maybe-dirty (build-user@build-host) #1 SMP PREEMPT Thu Jan 1 00:00:00 UTC 1970, RO-rootFS, swap_dev 0X11, Normal VGA

Run with new Kernel


Cuttlefish Device


It is easy to tell Cuttlefish to use new kernel and initramfs (included all virtual kernel modules):

launch_cvd --gpu_mode=gfxstream \
-kernel_path=${AOSP_HOME}/${KERNEL_BRANCH}/out/virtual_device_x86_64/dist/bzImage \
-initramfs_path=${AOSP_HOME}/${KERNEL_BRANCH}/out/virtual_device_x86_64/dist/initramfs.img

cuttlefish

Android Virtual Device


This needs to be rebuilt to run with new kernel:

device/generic/goldfish/board/kernel/x86_64.mk
TARGET_KERNEL_USE ?= 6.6
KERNEL_ARTIFACTS_PATH := kernel/prebuilts/$(TARGET_KERNEL_USE)/x86_64
VIRTUAL_DEVICE_KERNEL_MODULES_PATH := kernel/prebuilts/common-modules/virtual-device/$(TARGET_KERNEL_USE)/x86-64
TARGET_KERNEL_USE ?= 6.1
KERNEL_ARTIFACTS_PATH := /home/trongvq/aosp/common-android14-6.1/out/virtual_device_x86_64/dist
VIRTUAL_DEVICE_KERNEL_MODULES_PATH := $(KERNEL_ARTIFACTS_PATH)
...
EMULATOR_KERNEL_FILE := $(KERNEL_ARTIFACTS_PATH)/kernel-$(TARGET_KERNEL_USE)
EMULATOR_KERNEL_FILE := $(KERNEL_ARTIFACTS_PATH)/bzImage

Compilation may fail due to a missing kernel module. Remove it in the make file:

device/generic/goldfish/board/kernel/x86_64.mk
vmw_vsock_virtio_transport_common.ko \
#vmw_vsock_virtio_transport_common.ko \

Emulator