Building Android Kernel

Android is built around a Linux kernel. By default, Android’s boot.img is build from a binary kernel Image located in:

~/src/khadas-aosp-16/device/amlogic/yukawa-kernel/6.12

This guide describes how to rebuild and customize a Linux kernel for Android.

Note

Android 16 supports two kernel versions:

  • 6.12 (default): Stable Android kernel 6.12

  • mainline: Latest mainline kernel for development

Fetching the kernel code

Fetch the code using repo:

For kernel 6.12 (default):

mkdir ~/src/khadas-kernel-6.12/ && cd $_
repo init -u https://gitlab.baylibre.com/baylibre/amlogic/atv/aosp/kernel/manifest.git -b yukawa-android16-6.12
repo sync

For kernel mainline:

mkdir ~/src/khadas-kernel-mainline/ && cd $_
repo init -u https://gitlab.baylibre.com/baylibre/amlogic/atv/aosp/kernel/manifest.git -b yukawa-mainline
repo sync

Note

To save same disk space, pass the --clone-depth=1 option to repo init:

# For 6.12
repo init -u https://gitlab.baylibre.com/baylibre/amlogic/atv/aosp/kernel/manifest.git -b yukawa-android16-6.12 --clone-depth=1

# For mainline
repo init -u https://gitlab.baylibre.com/baylibre/amlogic/atv/aosp/kernel/manifest.git -b yukawa-mainline --clone-depth=1

Building the kernel

In this section, we will assume that we already have an Android source tree which has been fully build and is located in:

~/src/khadas-aosp-16/

Build everything from scratch

For the 6.12 kernel (default):

cd ~/src/khadas-kernel-6.12/
export destdir=~/src/khadas-aosp-16/device/amlogic/yukawa-kernel/6.12
tools/bazel run //yukawa-device:yukawa_dist -- --destdir=$destdir

For the mainline kernel:

cd ~/src/khadas-kernel-mainline/
export destdir=~/src/khadas-aosp-16/device/amlogic/yukawa-kernel/mainline
tools/bazel run //yukawa-device:yukawa_dist -- --destdir=$destdir

Re-running this command will rebuild incrementally

Defconfig/menuconfig changes

To configure the kernel using menuconfig:

tools/bazel run --config=local //common:yukawa_with_kgdb_config -- menuconfig

Alternatively, you can manually edit the arch/arm64/configs/amlogic_gki.fragment file.

Rebuilding all involved Android images

To test the kernel changes, we have to re-generate the relevant Android images:

  • boot.img: contains the kernel binary and the main device tree

  • vendor.img: contains the kernel modules

To rebuild the Android images, do:

cd ~/src/khadas-aosp-16/
source build/envsetup.sh
lunch yukawa-bp2a-userdebug
m bootimage vendorimage

Flashing the kernel

With GKI and Android 16, flashing only the kernel is not sufficient. You need to reflash the entire device using the flash script:

cd $ANDROID_PRODUCT_OUT
adb reboot fastboot
./flash.sh