sodcalculator

10 Tech How-To

Build a Custom Android Kernel: Step-by-Step Guide

Guide 10-31242 · Updated · Step by step

Building a custom Android kernel lets you tweak performance, battery life, or add features not in the stock kernel. This guide walks you through the entire process, from setting up your environment to flashing the finished kernel. You’ll need a Linux machine (or VM), the Android source tree, and a compatible device with an unlocked bootloader.

What You’ll Need

  • A Linux PC (Ubuntu 20.04+ recommended) with at least 100GB free disk space and 16GB RAM.
  • Your Android device with an unlocked bootloader and a custom recovery (e.g., TWRP) installed.
  • ADB and Fastboot tools installed on your PC.
  • The kernel source code for your device (usually from the manufacturer or a community project).
  • A toolchain to compile the kernel (e.g., Clang/LLVM from AOSP or a prebuilt GCC).
  • Basic familiarity with terminal commands and Git.

Step-by-Step

  1. Set up your build environment. Install required packages: sudo apt update && sudo apt install git-core gnupg flex bison build-essential zip curl zlib1g-dev libc6-dev-i386 x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig.
  2. Install the Android SDK platform-tools. Download and set up ADB and Fastboot so you can communicate with your device.
  3. Get the kernel source. Clone the kernel repository for your device. For example: git clone https://github.com/your-device/kernel.git. Make sure you check out the correct branch (e.g., git checkout android-13.0.0_r1).
  4. Set up the toolchain. Download the appropriate Clang/LLVM toolchain from AOSP or use a prebuilt one. Add it to your PATH: export PATH=/path/to/clang/bin:$PATH.
  5. Configure the kernel. Use the device’s defconfig. Usually located in arch/arm64/configs/ (or arch/arm/configs/ for 32-bit). Run: make ARCH=arm64 your_device_defconfig (replace with actual name).
  6. Customize (optional). If you want to change settings, run make ARCH=arm64 menuconfig to open the interactive configuration. Make your changes and save.
  7. Build the kernel. Run make ARCH=arm64 -j$(nproc). This will compile the kernel image (usually Image.gz-dtb or zImage-dtb).
  8. Create a flashable package. Use the mkbootimg tool or a script to package the kernel into a .img file. For example: mkbootimg --kernel arch/arm64/boot/Image.gz-dtb --base 0x80000000 --ramdisk_offset 0x01000000 --output boot.img.
  9. Flash the kernel. Reboot your device to bootloader: adb reboot bootloader. Then flash: fastboot flash boot boot.img. Reboot with fastboot reboot.
  10. Verify. After booting, check the kernel version in Settings > About phone or with adb shell uname -a.

Common Mistakes

  • Using the wrong defconfig for your device variant (e.g., international vs. US model).
  • Not cleaning the build tree when switching between different configurations.
  • Flashing a kernel built for a different Android version (e.g., building for Android 12 but flashing on Android 13).
  • Forgetting to include the DTB (device tree blob) in the boot image.
  • Using a toolchain that is too old or incompatible with the kernel source.

Tips

  • Always keep a backup of your stock boot image so you can restore it if something goes wrong.
  • Start with a minimal change (like just updating the kernel version) before making major modifications.
  • Use make savedefconfig to generate a minimal defconfig from your current .config – this helps track changes.
  • Join device-specific communities (XDA forums, Telegram groups) to get help with device-specific issues.
  • Test your kernel thoroughly – boot, Wi-Fi, Bluetooth, camera, etc. – before relying on it daily.

Troubleshooting

Device doesn’t boot after flashing: Boot back into recovery and restore your stock boot image. If you can’t get into recovery, reflash the stock boot.img via fastboot.

Kernel compiles but Wi-Fi doesn’t work: This often means the kernel modules aren’t matching. Make sure you built and included the correct modules (e.g., wlan.ko) and that they are loaded.

Build errors: Double-check your toolchain path and that you have the correct dependencies. Search the error message online – it’s likely someone else has hit it.

Touchscreen not working: This usually indicates a missing or wrong DTB. Ensure your device tree is correctly included in the boot image.

Battery drains fast: Some default kernel settings may be too aggressive. Check your CPU governor settings and revert to stock if needed.

Frequently Asked Questions

Q: Do I need to unlock the bootloader? Yes, you must unlock the bootloader to flash a custom kernel. This will wipe your device data.

Q: Will building a custom kernel void my warranty? In most cases, yes, but it depends on the manufacturer. Check your device’s warranty policy.

Q: Can I use the same kernel for different ROMs? Not always. Kernels are often tied to the Android version and sometimes to the ROM’s specific patches. Always check compatibility.

Q: How long does the build take? On a decent PC (8+ cores), the first build can take 30-60 minutes. Incremental builds are much faster.

Q: What if I don’t have 100GB of free space? You can reduce the source tree size by using repo init --depth=1 to do a shallow clone, but you may still need 30-50GB.

Q: Is it safe to flash a custom kernel? There’s always some risk. If you follow the steps carefully and keep a backup, the risk is low, but it’s not zero.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →