Before delving into the text, it’s important to note that there may be some grammar or syntax errors present. This could be due to translation errors, typing mistakes, or the author’s unique writing style ( I’m a Chinese native speaker 😅 ).
Environment
Linux
Android kernel is a variant of Linux kernel, and due to the compilers and make tools, it’s necessary to have a Linux shell environment.
Theoretically all forms of Linux are able to compile an Android kernel.
Example
WSL, VMware, Termux on Android, Physical machine
Info
In this guide, Arch Linux is used as the demonstration.
cd .. mkdir -p toolchains/zyc-19 cd ./toolchains/zyc-19 wget https://github.com/ZyCromerZ/Clang/releases/download/19.0.0git-20240601-release/Clang-19.0.0git-20240601.tar.gz tar -xzf Clang-19.0.0git-20240601.tar.gz -C zyc-19
Write a Build Script
Building Android Kernel is a two-steps process:
Generate kernel config
1
make ARCH=<architecture> O=<output_dir> <config_name>
xxx_defconfig is at kernel_root/arch/<architecture>/configs
Buide the kernel
Here is an build shell script example (From my gist):
# Omit large amounts of output... GEN .version LD vmlinux.o MODPOST vmlinux.o CHK include/generated/compile.h UPD include/generated/compile.h CC init/version.o LD init/built-in.o aarch64-linux-gnu-ld: warning: -z norelro ignored aarch64-linux-gnu-ld: warning: .tmp_vmlinux1 has a LOAD segment with RWX permissions KSYM .tmp_kallsyms1.o aarch64-linux-gnu-ld: warning: -z norelro ignored aarch64-linux-gnu-ld: warning: .tmp_vmlinux2 has a LOAD segment with RWX permissions KSYM .tmp_kallsyms2.o LD vmlinux aarch64-linux-gnu-ld: warning: -z norelro ignored aarch64-linux-gnu-ld: warning: vmlinux has a LOAD segment with RWX permissions SORTEX vmlinux SYSMAP System.map OBJCOPY arch/arm64/boot/Image DTC arch/arm64/boot/dts/qcom/polaris-p3.dtb DTC arch/arm64/boot/dts/qcom/polaris-p2-v2.1.dtb DTC arch/arm64/boot/dts/qcom/sdm845-v2-mtp.dtb DTC arch/arm64/boot/dts/qcom/polaris-mp-v2.1.dtb DTC arch/arm64/boot/dts/qcom/sdm845-4k-panel-qrd.dtb DTC arch/arm64/boot/dts/qcom/sdm845-mtp.dtb DTC arch/arm64/boot/dts/qcom/polaris-p3-v2.1.dtb DTC arch/arm64/boot/dts/qcom/polaris-mp.dtb DTC arch/arm64/boot/dts/qcom/polaris-p1-v2.1.dtb DTC arch/arm64/boot/dts/qcom/sdm845-rumi.dtb DTC arch/arm64/boot/dts/qcom/sdm845-v2-qrd.dtb DTC arch/arm64/boot/dts/qcom/sdm845-cdp.dtb DTC arch/arm64/boot/dts/qcom/polaris-p2.dtb DTC arch/arm64/boot/dts/qcom/polaris-p2-v2.dtb DTC arch/arm64/boot/dts/qcom/polaris-p1.dtb DTC arch/arm64/boot/dts/qcom/sdm845-v2-cdp.dtb DTC arch/arm64/boot/dts/qcom/polaris-p1-v2.dtb DTC arch/arm64/boot/dts/qcom/polaris-p3-v2.dtb DTC arch/arm64/boot/dts/qcom/polaris-mp-v2.dtb DTC arch/arm64/boot/dts/qcom/sdm845-4k-panel-cdp.dtb DTC arch/arm64/boot/dts/qcom/polaris-p0.dtb DTC arch/arm64/boot/dts/qcom/sdm845-v2-rumi.dtb DTC arch/arm64/boot/dts/qcom/sdm845-sim.dtb DTC arch/arm64/boot/dts/qcom/sdm845-4k-panel-mtp.dtb DTC arch/arm64/boot/dts/qcom/polaris-p0-v2.dtb DTC arch/arm64/boot/dts/qcom/sdm845-qrd.dtb Building modules, stage 2. GZIP arch/arm64/boot/Image.gz MODPOST 0 modules CAT arch/arm64/boot/Image.gz-dtb
Info
It may take a very long time. In my PC, it costs 2m 51s .
Output Files
The kernel binary files is at kernel_root/$O/arch/$ARCH/boot .
Strongly recommend to enable Build a concatenated Image.gz/dtb by default (CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE=y) Appended DTB Kernel Image name (Image.gz-dtb) (IMG_GZ_DTB=y) in config file.
Output files may include:
Image: kernel image
Image.gz: kernel image
Image.gz-dtb: kernel image
dtbo.img: dtbo partition, relevant to fingerprint, refresh rate, etc.
Package
Clone AnyKernel3 Repository
Write an Pakage Script
Choose one of Images ( Image.gz-dtb > Image.gz > Image ) and dtbo.img ( Optional, may causing bugs ) to package.