DEV Community

Cover image for How to Build a Custom Android ROM and Kernel: A Practical Guide
Padmakar Garg
Padmakar Garg

Posted on

How to Build a Custom Android ROM and Kernel: A Practical Guide

How to Build a Custom Android ROM and Kernel

Building your own Android is one of the best ways to learn how the platform works below the app layer. This guide covers the full path: the build machine, AOSP, a device ROM based on LineageOS, and a custom kernel.

Warning: Flashing unofficial software can erase your data, stop the phone from booting, break features such as banking apps or contactless payments, and may affect your warranty. Use a spare device that you own. Commands and partition layouts differ between devices, so your exact model's instructions take priority over this guide.

1. ROM vs kernel

Part What it contains Typical changes
ROM Android framework, system apps, services, device configuration Features, UI, bundled apps
Kernel Linux kernel plus Android changes Drivers, kernel configuration, debugging options
Vendor components Hardware-specific binaries and modules Usually taken from the device maker

You can build a ROM without modifying the kernel. Change one at a time, so that you know what caused a failure.

2. Check your device first

  • Bootloader: Can it be unlocked? Some manufacturers and carriers do not allow it.
  • Exact model and codename: Similar-looking variants can need different builds.
  • Device support: Is there a maintained device tree, kernel source and set of vendor files? The LineageOS Wiki is a good place to check.
  • Recovery path: Download the official factory or recovery firmware and learn how to restore it before flashing anything.
  • Backup: Copy your data off the phone.

Without device support, you are porting Android to new hardware. That is a much larger project than this guide covers.

3. Prepare the build machine

For current AOSP, plan for:

  • A 64-bit x86 Linux system (Ubuntu is a common choice)
  • At least 400 GB of free disk space (about 250 GB for source, 150 GB for the build)
  • 64 GB of RAM
  • A fast SSD and a reliable internet connection

Install the packages listed by AOSP:

sudo apt-get update
sudo apt-get 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 repo
repo version
Enter fullscreen mode Exit fullscreen mode

If your distribution's repo package is missing or outdated, use the manual install steps in the AOSP documentation. Set your Git identity:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Enter fullscreen mode Exit fullscreen mode

4. Learn the flow with AOSP

Start with a virtual device. It lets you practise without risking a phone.

mkdir -p ~/aosp && cd ~/aosp
repo init --partial-clone --no-use-superproject \
  -b android-latest-release \
  -u https://android.googlesource.com/platform/manifest
repo sync -c -j8

source build/envsetup.sh
lunch aosp_cf_x86_64_only_phone-aosp_current-userdebug
m
Enter fullscreen mode Exit fullscreen mode

The lunch target has three parts: product, release configuration and build variant. userdebug is convenient for development. user is the production-style variant.

The target above is for Cuttlefish, Android's virtual device. After installing the Cuttlefish host packages (see its documentation), launch it with:

launch_cvd --daemon
# Open https://localhost:8443 in a browser
stop_cvd
Enter fullscreen mode Exit fullscreen mode

As of 2026, Google publishes AOSP source in Q2 and Q4. The android-latest-release manifest points to the most recent release.

5. Build a ROM for a real phone (LineageOS)

A generic AOSP checkout does not contain everything your phone needs. For a supported device, LineageOS is a practical starting point.

Open the build page for your device on the LineageOS Wiki. Use its package list, branch and special steps. Below, replace <BRANCH> and <CODENAME> with your values.

5.1 Download the source

Install the packages from your device's build page, including git-lfs.

git lfs install
mkdir -p ~/android/lineage && cd ~/android/lineage
repo init -u https://github.com/LineageOS/android.git -b <BRANCH> \
  --git-lfs --no-clone-bundle
repo sync
Enter fullscreen mode Exit fullscreen mode

5.2 Optional: enable ccache

ccache speeds up repeat builds.

export USE_CCACHE=1
export CCACHE_EXEC=/usr/bin/ccache
ccache -M 50G
Enter fullscreen mode Exit fullscreen mode

5.3 Prepare device code

source build/envsetup.sh
breakfast <CODENAME>
Enter fullscreen mode Exit fullscreen mode

This fetches the device configuration and kernel.

5.4 Get proprietary vendor files

Many devices need proprietary files ("blobs") for hardware such as the camera or modem. The usual method extracts them from a phone that already runs the matching LineageOS branch, connected with ADB and root enabled. From the device directory, run whichever script exists:

./extract-files.py    # or ./extract-files.sh
Enter fullscreen mode Exit fullscreen mode

The Wiki also describes extraction from an installable zip. If breakfast failed because vendor files were missing, run it again afterwards.

5.5 Build

croot
brunch <CODENAME>
cd $OUT
Enter fullscreen mode Exit fullscreen mode

On success, look for a package named like lineage-<version>-<date>-UNOFFICIAL-<CODENAME>.zip, along with device-specific images such as a recovery or boot image.

6. Customize the ROM

Make one small change, build, and test.

  • Add or remove apps: edit the product makefiles (PRODUCT_PACKAGES). Removing an app may also require changing the makefile that includes it.
  • Change default settings or resources: use resource overlays.
  • Change framework behavior: start in frameworks/base.
  • Keep your work: use a Git branch in each repository you edit. Use a local manifest for your own forks so that repo sync knows about them.

Keep SELinux enforcing. If something is denied, read the denial in the logs and write a narrow policy instead of disabling SELinux.

7. Build a custom kernel

7.1 Understand GKI

Android's Generic Kernel Image (GKI), introduced with Android 11, separates the Google-maintained core kernel from vendor modules. Vendor modules depend on the Kernel Module Interface (KMI). A change that breaks it can stop modules from loading, which can break display, touch, storage or boot.

Not every phone uses GKI. Many devices need the manufacturer's kernel source and its own build instructions. On LineageOS, start with the kernel that breakfast downloaded.

First, record what your phone runs:

adb shell uname -r
adb shell getprop ro.product.device
Enter fullscreen mode Exit fullscreen mode

7.2 Download the Android Common Kernel

Choose the branch that matches your device's Android release tag and kernel version. A matching version number alone does not guarantee compatibility. common-android16-6.12 is only an example.

mkdir -p ~/android-kernel && cd ~/android-kernel
repo init -u https://android.googlesource.com/kernel/manifest \
  -b common-android16-6.12
repo sync -c -j8
Enter fullscreen mode Exit fullscreen mode

7.3 Build with Kleaf

Kleaf is the Bazel-based kernel build system, introduced with Android 13. The tools come with the kernel checkout.

tools/bazel run //common:kernel_aarch64_dist -- --destdir=out/dist
Enter fullscreen mode Exit fullscreen mode

build/build.sh is the legacy method for older branches. It is not supported on Android 14 and later.

7.4 Change the configuration

Build the unmodified kernel first. Then put your changes in a defconfig fragment instead of editing generated files. The exact Bazel setup differs between branches, so follow the Kleaf documentation in your checkout (build/kernel/kleaf/docs/).

A harmless first change is a custom version suffix or one debugging option. Leave overclocking and voltage changes alone: mistakes can cause instability or overheating.

8. Test and install

8.1 Unlock the bootloader

Enable Developer options and OEM unlocking, then follow your manufacturer's procedure. On devices that support the standard command:

adb reboot bootloader
fastboot flashing unlock
Enter fullscreen mode Exit fullscreen mode

Unlocking factory-resets the device. An unlocked bootloader also weakens the phone's security.

8.2 Try a kernel before flashing it

Some devices can boot an image temporarily:

adb reboot bootloader
fastboot boot <your-boot-image.img>
Enter fullscreen mode Exit fullscreen mode

This does not permanently replace the kernel, and a normal reboot returns to the installed one. It is not a guarantee of safety: a bad kernel can still affect data. Not every bootloader supports the command. The image must match your device's boot format, and a raw kernel file may not be enough.

8.3 Install the ROM

Follow the installation page for your exact codename. Depending on the device, the steps may involve boot, init_boot, vendor_boot, a recovery image or adb sideload. Do not copy flash commands from another phone.

Do not relock the bootloader with custom software installed unless your device and signing setup explicitly support it. It can leave the phone unable to boot. Check anti-rollback rules before installing older firmware.

9. Debug failures

Problem What to check
repo sync fails Network, disk space, the first error; retry with fewer jobs (-j4)
Build runs out of memory Free RAM, swap, fewer parallel jobs
Build fails The first error, not the final summary
Missing vendor makefiles Blob extraction and device repositories
Boot loop Restore the last working image; compare your latest change
Wi-Fi, touch or camera broken Module loading, vendor files, kernel/ROM compatibility

Useful commands:

m 2>&1 | tee build.log          # save an AOSP build log
adb logcat -b all > logcat.txt  # when ADB is available
adb shell su -c dmesg           # needs root; syntax varies
adb root && adb shell dmesg     # userdebug builds that allow adb root
Enter fullscreen mode Exit fullscreen mode

Kernel crash logs may be available under /sys/fs/pstore/ after a reboot, if the device supports it.

10. Share builds responsibly

  • Publish kernel source that matches your binaries, as required by the GPL.
  • Check the licence of every component. Do not assume you may redistribute proprietary blobs or Google apps.
  • Use your own private release keys for builds you distribute, and never publish them. Public test keys are not suitable.
  • State the supported codename, known bugs, install steps and recovery steps.
  • Keep up with security patches.
  • Credit upstream developers.

Suggested learning path

  1. Build AOSP and run it on Cuttlefish.
  2. Make one visible framework or resource change.
  3. Build an unmodified ROM for a supported spare phone.
  4. Build the unmodified kernel.
  5. Make one small kernel change and test it.
  6. Learn signing, SELinux, updates and release maintenance.

Change one thing at a time, keep a way back, and read the first error.

References

Author: Padmakar Garg

Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture, mobile security, and high-performance app development.

Top comments (0)