Skip to main content

Build Linux Kernel for Android Device

Build Linux Kernel for Android Device
hahnavi hahnavi

This is a simple walk‑through for building an Android kernel from source. It assumes a Linux host and a device tree that already builds (LineageOS kernels are usually a good starting point).

Download the Kernel Source Code

Pick the kernel tree that matches the device. Example: Xiaomi Mi8937

git clone https://github.com/LineageOS/android_kernel_xiaomi_msm8937.git --depth=1

Install Cross Compiler

Android kernels for ARM64 need an AArch64 cross compiler. Use the package for the host distro:

Arch Linux

sudo pacman -S aarch64-linux-gnu-gcc

Ubuntu

sudo apt install gcc-aarch64-linux-gnu

Compile the Kernel

Set the basic build variables. CROSS_COMPILE points to the toolchain prefix and ARCH selects the target architecture.

export CROSS_COMPILE=aarch64-linux-gnu-
export ARCH=arm64

Create the output folder (keeps the source tree clean)

mkdir out

Find the device defconfig in arch/arm64/configs and use it to seed the build:

make O=out <defconfig>

Example for this device:

make O=out vendor/msm8937-perf_defconfig

Alternative: copy a defconfig from outside the tree into out/.config, then expand defaults:

cp <defconfig-file-path> out/.config
make O=out oldconfig

To tweak config options interactively:

make O=out menuconfig

Build the kernel using all CPU cores:

make O=out -j`nproc`

Output images land under: out/arch/arm64/boot/

Share

comments powered by Disqus