Most of the time I use my laptop that has a dual core celeron processor, which isn’t good in kernel compilation. So, I use Google cloud to compile kernels. Google cloud provides free 50 hours/week, which is more than enough to compile multiple kernels. The only thing you need is a google account. You can use Google cloud in your phone, without PC/Laptop. One more thing, you can follow the below tutorial to build in your PC as well, just skip first step. (-;

1. Open Google Cloud Console

  • Go to cloud.google.com.
  • Login with your google account.
  • Click on “Console” at top-right corner.
  • After clicking on “Console” you’ll see a terminal icon on top-right corner. Click on that icon.
  • Now, a terminal will appear on the webpage. Here we’ll do our kernel compilation.

2. Setup Build Environment

Once you get the console, install necessary packages, toolchains and kernel source. You can download the toolchains from my repo or download according to your device.

# Install necessary packages/libs
sudo apt update
sudo apt upgrade -y
sudo apt install -y gcc-aarch64-linux-gnu build-essential libncurses-dev \
libssl-dev flex bc git curl wget unzip python3 python-is-python3

3. Download Toolchains and Kernel Source

# Example: LineageOS kernel for OnePlus3
# Create tmp directory and download source and toolchains
mkdir tmp && cd tmp
wget https://github.com/Shubhamvis98/toolchains/archive/refs/heads/master.zip -O tc.zip && unzip -q tc.zip && rm tc.zip
wget https://github.com/LineageOS/android_kernel_oneplus_msm8996/archive/refs/heads/lineage-18.1.zip -O k.zip && unzip -q k.zip && rm k.zip
# The above commands will download, unzip and delete the zip files.

# After above steps you'll get two directories, toolchains and kernel source directory:
android_kernel_oneplus_msm8996-lineage-18.1 toolchains-master

4. Setup Exports and Aliases

The below code block is all you need if you want to compile with CLANG or GCC. I’ve created two aliases for make command. You can adjust the parameters according to your need.

If you want to compile with CLANG, use makecl and to build with GCC, use make command.

TC=~/tmp/toolchains-master
GCC=$TC/aarch64-linux-android-4.9/bin
GCC32=$TC/arm-linux-androideabi-4.9/bin
CLANG=$TC/clang-r428724/bin

export PATH=$CLANG:$GCC:$GCC32:$PATH
export CROSS_COMPILE=aarch64-linux-android-
export CROSS_COMPILE_ARM32=arm-linux-androideabi-
export CLANG_TRIPLE=aarch64-linux-gnu-

DEFCONFIG=lineageos_oneplus3_defconfig
export ARCH=arm64

alias makecl="make CC=clang O=output"
alias make="make O=output"

5. Let's Build...

# cd to kernel source
cd ~/tmp/android_kernel_oneplus_msm8996-lineage-18.1

# To build with GCC
make $DEFCONFIG
make menuconfig
make

# To build with CLANG
makecl $DEFCONFIG
makecl menuconfig
makecl

6. Get the kernel and dtbs

After compilation, you can download the kernel and necessary dtbs to your local machine/phone to make boot.img.

mkdir ~/tmp/out
cp output/arch/arm*/boot/Image* ~/tmp/out
# If your device has separate dtb from kernel then you need the dtb files as well to flash in dtb partition
mkdir ~/tmp/out/dtbs
find output -name '*.dtb*' -exec cp {} ~/tmp/out/dtbs \;

# Compress kernel and dtbs and download to local
cd ~/tmp
tar -cpzf build.tgz out # this will create a build.tgz archive

Now download the kernel to your local by clicking on “the three dots on top right corner -> Download”

7. Create boot.img

  • Clone or download Android Image Kitchen from my repo.
  • Get your stock boot.img and run ./unpackimg boot.img to unpack the stock boot image.
  • After that you’ll see the aik will create a directory named split with unpacked kernel and dtb.
  • Extract the downloaded build.tgz archive using tar -xpf build.tgz and replace the kernel in split directory.
  • Now repack the boot.img using ./repackimg. This will create a new image in aik directory.

Now you can boot/flash your new boot image using fastboot or from recovery.

NOTE:

  • Try booting before flashing the boot image.
    • fastboot boot new-boot.img
  • if everything works as expected, then you can flash it.
    • fastboot flash new-boot.img
  • Always backup your original/stock boot image.
  • Try to avoid using multiple cores and continuous kernel compilation in google cloud otherwise google will ban cloud access to your account. Don’t worry, your gmail, drive and other apps will work.