How to compile Android kernel for Samsung A8

December 2021 was the EOL date of my mobile phone Samsung A8 but My phone is still working and I don`t feel the necessity of buying a new one. Also, I like to learn new things and I think upgrading a phone by myself from the source code is an interesting challenge and I want to give a try.

In this post I will explain how I compiled a Linux kernel for Samsung A8. Most of steps will work as well with other phone models.

Getting the tools for compiling Linux kernel

The minimum hardware requeriments for compiling the kernel are low and any modern machine should be able to compile it.

In my case I created a Virtual Machine in Azure of size Standard_F4s_v2 with Ubuntu 20.04 and executed this command to install the necessary tools to build the kernel:

dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install -y build-essential imagemagick xorriso locales openjdk-8-jdk python git m4 unzip bison zip gperf libxml2-utils zlib1g:i386 libstdc++6:i386 bc curl lzop lzip lunzip jq wget squashfs-tools ccache ncurses-dev aptitude gcc-multilib

Downloading the kernel source code

The original Linux kernel won`t work on mobile devices directly. This is because phone manufacturers make changes in the source code to make the code compatible with their devices. Also, they add tweaks for things like improving the battery life, and some proprietary driver binaries needed to work with third-party hardware like cameras and sensors.

At this point you may choose to compile the source code provided in the website of the phone manufacturer or to use unofficial custom kernel.

I decided to use a custom kernel from VDavid003/universal7885_treeui_kernel because it had improvements over Samsung kernel source code and its changes were better documented than the official kernel.

You can download the universal7885_treeui_kernel source code with this command:

git clone https://github.com/VDavid003/universal7885_treeui_kernel.git

You will also need a cross-compiler, which is a tool that translates the source code into instructions that the mobile phone processor can understand.

Take in care that the Linux Kernel is designed to be compiled by an specific cross-compiler version. For example: I tried to compile it with the latest version of gcc-linaro compiler and it caused a bootloop in My mobile phone. The right version of the cross-compiler is in the documentation of the kernel version you want to compile.

The crosscompiler for universal7885_treeui_kernel is gcc-linaro-4.9.4. These are the commands to download and decompress it:

wget https://releases.linaro.org/components/toolchain/binaries/latest-4/aarch64-linux-gnu/gcc-linaro-4.9.4-2017.01-x86_64_aarch64-linux-gnu.tar.xz
tar xf gcc-linaro-4.9.4-2017.01-x86_64_aarch64-linux-gnu.tar.xz

Configure the kernel

This step is different depending on the kernel source code you use. In my case the universal7885_treeui_kernel can be compiled for different phone models and I had to configure manually the build for the Galaxy A8.

In the universal7885_treeui_kernel folder I made these changes to build_kernel.sh script: I set the value of CROSS_COMPILE with the path where I decompress the cross-compiler and I set the line make exynos7885-jackpotlte_defconfig to build the image using the specific configuration for the Samsung A8.

This is my build_kernel.sh file after the changes:

#!/bin/bash

export ARCH=arm64
export CROSS_COMPILE=/source/gcc-linaro-4.9.4-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
export ANDROID_MAJOR_VERSION=q

make exynos7885-jackpotlte_defconfig
make -j$(nproc --all)

Compile the sources

Just execute ./build_kernel.sh and wait. In my virtual machine compilation took less than 30 minutes to build the kernel image.

When finish open the folder universal7885_treeui_kernel/arch/arm64/boot and you will see the files dtb.img and Image these files are the ones that will be use to create the flashable .zip file for installing the kernel.

Create installer for the kernel

An image should not be flashed directly with TWRP because the mobile phone won`t be able to boot. You must create an installer for your kernel.

I used jcadduono/lazyflasher to create the kernel installer. Just follow these steps:

  1. Download lazyflasher with the command git clone https://github.com/jcadduono/lazyflasher.git
  2. Enter in lazyflasher folder and copy the files Image and dtb.img inside the folder
  3. Execute make. It will create a zip file. Use that file to flash your kernel with TWRP.