Skip to main content

Build a Custom Kernel for Android on Debian

These instructions are written for Debian Bullseye, but should be straightforward to adapt to other Linux distributions.

Prepare to Build the Kernel Image

To create a viable custom kernel for Android, first install the dependencies:

sudo apt-get install --no-install-recommends docker.io git patch tar

The next steps depend on the version of Android you're after.

Android 7 and 8

  1. Clone the goldfish kernel repository and checkout the android-goldfish-3.18 branch.
git clone https://android.googlesource.com/kernel/goldfish
cd goldfish/
git checkout android-goldfish-3.18 -f
export GOLDFISH_CHECKOUT=$(realpath .)
  1. Download this patch and save it to $GOLDFISH_CHECKOUT:

android-goldfish-3.18.patch

  1. Apply the patch.
patch -p1 < android-goldfish-3.18.patch
  1. Save this file to `$GOLDFISH_CHECKOUT/arch/arm64/configs/:

corellium_defconfig-3.18 (Note: this file is specific to Android 7 and 8.)

  1. Rename the defconfig file.
mv $GOLDFISH_CHECKOUT/arch/arm64/configs/corellium_defconfig-3.18 $GOLDFISH_CHECKOUT/arch/arm64/configs/corellium_defconfig
  1. Preparation is complete. Proceed to the section, Build the Kernel.

Android 9 and 10

  1. Clone the goldfish kernel repository and checkout the android-goldfish-4.4-dev branch.
git clone https://android.googlesource.com/kernel/goldfish
cd goldfish/
git checkout android-goldfish-4.4-dev -f
export GOLDFISH_CHECKOUT=$(realpath .)
  1. Download this tarball and save it to $GOLDFISH_CHECKOUT: goldfish.tar.gz

  2. Once that's saved, replace the Goldfish platform drivers.

rm --force --recursive drivers/platform/goldfish
mkdir drivers/platform/goldfish
tar --extract --directory drivers/platform/goldfish --file goldfish.tar.gz --gunzip
  1. Save this file to $GOLDFISH_CHECKOUT/arch/arm64/configs/:

corellium_defconfig

(Note: this file is specific to Android 9 and 10.)

Preparation is complete. Proceed to the section, Build the Kernel.

Android 11

  1. Clone the common kernel repository and checkout the android11-5.4.161_r00 branch.
git clone https://android.googlesource.com/kernel/common
cd common/
git checkout android11-5.4.161_r00 -f
export COMMON_CHECKOUT=$(realpath .)
  1. Download this patch and save it to $COMMON_CHECKOUT:

android-common-5.4.patch

  1. Apply the patch.
patch -p1 < android-common-5.4.patch
  1. Download the corellium-drivers.tar.gz tarball and save it to $COMMON_CHECKOUT:

drivers-corellium.tar.gz

  1. Extract the tarball to the correct directory.
mkdir drivers/corellium/
tar --extract --directory drivers/corellium --file drivers-corellium.tar.gz --gunzip
  1. Save this file to $COMMON_CHECKOUT/arch/arm64/configs/:

corellium_defconfig

(Note: this file is specific to Android 11.)

  1. Preparation is complete. Proceed to the section, Build the Kernel.

Android 12

  1. Clone the common kernel repository and checkout the android12-5.10.81_r00 branch.
git clone https://android.googlesource.com/kernel/common
cd common/
git checkout android12-5.10.81_r00 -f
export COMMON_CHECKOUT=$(realpath .)
  1. Download this patch and save it to $COMMON_CHECKOUT:

android-common-5.10.patch

  1. Apply the patch.
patch -p1 < android-common-5.10.patch
  1. Download the corellium-drivers.tar.gz tarball and save it to $COMMON_CHECKOUT:

drivers-corellium.tar.gz

  1. Extract the tarball to the correct directory.
mkdir drivers/corellium/
tar --extract --directory drivers/corellium --file drivers-corellium.tar.gz --gunzip
  1. Save this file to `$COMMON_CHECKOUT/arch/arm64/configs/:

corellium_defconfig

(Note: this file is specific to Android 12.)

  1. Preparation is complete. Proceed to the section, Build the Kernel

Build the Kernel

For portability, build the kernel inside a Docker container.

Android 7 through 10

To build the kernel image for Android 7, 8, 9, or 10, use the following:

mkdir goldfish-build-environment/
cd goldfish-build-environment/
cat > Dockerfile << END
FROM ubuntu:16.04
RUN apt-get update && apt-get upgrade --assume-yes && apt-get install --assume-yes --no-install-recommends bc bison flex gcc gcc-aarch64-linux-gnu libc6-dev make
END
sudo docker build --tag=goldfish-build-environment .
sudo docker run --interactive --tty --mount=type=bind,source=$GOLDFISH_CHECKOUT,target=/goldfish goldfish-build-environment bash -c 'cd /goldfish/ && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make corellium_defconfig && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make'

Android 11 and 12

To build the kernel image for Android 11 or 12, use the following:

mkdir common-build-environment/
cd common-build-environment/
cat > Dockerfile << END
FROM ubuntu:18.04
RUN apt-get update && apt-get install --assume-yes --no-install-recommends autoconf automake bc bison build-essential ca-certificates cpio curl device-tree-compiler flex g++ gcc gcc-aarch64-linux-gnu gettext git-core gnupg gperf groff lib32ncurses5-dev lib32stdc++6 lib32z-dev libc6-dev libc6-dev-i386 libffi-dev libgl1-mesa-dev libncursesw5-dev libpopt-dev libssl-dev libtool libx11-dev libxml2-utils make ninja-build openjdk-8-jdk-headless openjdk-8-jre-headless openssh-client p7zip-full pkg-config python python3 python3-pip python3-venv rsync sudo unzip uuid-dev wget x11proto-core-dev xsltproc xz-utils zip zlib1g-dev
END
sudo docker build --tag=common-kernel-build-environment .
sudo docker run --interactive --tty --mount=type=bind,source=$COMMON_CHECKOUT,target=/common common-kernel-build-environment bash -c 'cd /common/ && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make corellium_defconfig && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make'

Tweak and Rebuild the Kernel Image

Once the build is finished, a usable kernel image is found in your checkout directory under arch/arm64/boot/Image.

You can now tweak the kernel as you like.

Rebuild the Android 7, 8, 9, or 10 kernel image with your changes by running:

sudo docker run --interactive --tty --mount=type=bind,source=$GOLDFISH_CHECKOUT,target=/goldfish goldfish-build-environment bash -c 'cd /goldfish/ && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make'

Rebuild the Android 11 or 12 kernel image with your changes by running:

sudo docker run --interactive --tty --mount=type=bind,source=$COMMON_CHECKOUT,target=/common common-kernel-build-environment bash -c 'cd /common/ && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make corellium_defconfig && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make'