Build Android Kernel From Source Using Ubuntu OS

Check out this article for step-by-step instructions to build an Android kernel. We are going to setup Android development environment on Ubuntu operating system and then use the latest kernel source from the device manufacturer website to build an Android kernel. This tutorial has instructions only for Android kernel compilation, not the entire ROM.

Build Android Kernel

If you are into Android development or even getting started, you will eventually come across the Android kernel compilation part. Even if someone is developing an Android application that requires custom kernel functionality, he/she should know how to incorporate that custom functionality into the kernel source and then build an Android kernel for testing purpose.

Requirements

Hardware Requirements

There are certain hardware requirements which should be met or exceeded by your system.

  • To build Gingerbread (2.3.x) or newer version, you need a 64-bit system.
  • To build Android ROM
    • If the downloaded source “does not include .repo” then around 100GB of free hard disk space is required.
      If the downloaded source “includes .repo” then around 250GB of free hard disk space is required.
  • To build Android kernel
    • 20GB of free hard disk space is enough for kernel development and compilation.
  • System should have at-least 2GB of physical RAM. Greater the RAM, faster will be the compilation.
  • SWAP partition can be used to increase the RAM for faster compilation.

Build System

In order to compile an Android kernel from source, we need a Linux operating system. There are more than a dozen of Debian based Linux operating systems. Whatever the Linux distribution you may select, make sure that you download the 64-bit operating system, not the 32-bit. For Android development, Ubuntu is preferred by most of the developers and is also recommended by Google.

I recommend Debian based Ubuntu 14.04 Linux distribution. Click on the attached Ubuntu 14.04 LTS (Trusty Tahr) link to download the latest Ubuntu 14.04 release. You can also select Ubuntu 16.04 LTS (Xenial Xerus) Linux distribution. Make sure that you download 64-bit AMD64 Desktop Image.

If your system fulfills the above hardware and build requirements, then you can proceed with the installation of Ubuntu on your Computer/Laptop. You can install Ubuntu on your HDD alongside Windows (dual boot) or in a virtual machine using VirtualBox in Windows.

Note: If you run Ubuntu in a virtual machine then you need at-least 16GB of RAM.

Now that you have installed Ubuntu OS, you will need to setup your Linux operating system for Android development and compilation.

Setup Ubuntu for Android Development

Steps involved in setting up Android development environment are almost same for all Linux operating systems. In this section, we will be setting up Ubuntu operating system for Android development.

Ubuntu OS and Java JDK requirements depends on the Android version. Check them below.

  • Android 1.5 (Cupcake) – Android 2.2.x (Froyo) : Java JDK 5 and Ubuntu 10.04 are minimum requirements.
  • Android 2.3.x (Gingerbread) – Android 4.4.x (KitKat) : Java JDK 6 and Ubuntu 12.04 are minimum requirements.
  • Android 5.x (Lollipop) : OpenJDK 7 and Ubuntu 12.04 are minimum requirements.
  • Android 6.0 (Marshmallow) : OpenJDK 7 and Ubuntu 14.04 are minimum requirements.
  • Android 7.0 (Nougat) – Android 8.0 (Oreo): OpenJDK 8 and Ubuntu 14.04 are minimum requirements.

Installing Java Development Kit

Visit this official Google link for JDK installation instructions: https://source.android.com/setup/build/initializing

Click on the above attached links for installation instructions of Java JDK 6 and Java JDK 5.

With this, we have finished setting up build environment for Android development and compilation.

Build Android Kernel

The process to build an Android kernel can be divided into three parts.

  1. Downloading Android Kernel Source Code
  2. Downloading Toolchain
  3. Compiling Android Kernel

After setting up Ubuntu, you have to download the source code of your device kernel. You can directly download the source from your device manufacturer website. They have to release the kernel source for public as the kernel source is licensed under GPL. If your device manufacturer has not made the kernel source public, contact them.

ManufacturerKernel Source
LGhttp://opensource.lge.com/index
HTChttps://www.htcdev.com/devcenter/downloads
Samsunghttp://opensource.samsung.com/reception.do
Motorolahttps://sourceforge.net/motorola/wiki/Projects/
Sonyhttps://developer.sonymobile.com/downloads/xperia-open-source-archives/
Googlehttps://android.googlesource.com/?format=HTML
Dellhttp://opensource.dell.com/releases/

Downloading from the Manufacturer Website

Click on the above appropriate attached link, enter your device model name and then search for its kernel source. Once you find the required download link, click on it and download the kernel source to your computer. Mostly its in the form of archive. For example, this link will give you download links for GT-S7262.

After downloading the kernel source, extract the archive to your home directory and rename the extracted folder to kernel for our convenience. You can right click on the folder and select ‘Rename’ option.

Downloading from the code hosting sites

Alternatively, if someone has already uploaded your device kernel source to code hosting sites like Github or Code Aurora then you can sync the kernel source from these sites to your local machine instead. You should check these sites before searching in your manufacturer website.

Search for the kernel source of your device in those websites.

Github link for GT-S7262: https://github.com/ItsAnilSingh/android_kernel_samsung_logan2g

After finding out the kernel source link, open the kernel source in a new tab or a new window. If the site allows the source code to be downloaded in the form of archive, click on the download button. Remember to select the branch of the source properly before clicking on the download button.

Github provides the source code to be downloaded in the form of archive. You just need to click on the kernel source link, select the appropriate branch, click on the ‘clone or download’ button and then click on the ‘download zip’ option.

After downloading the kernel source, extract the archive to your home directory and rename the extracted folder to kernel for our convenience. You can right click on the folder and select ‘Rename’ option.

But this will only download the source code, you will not be able to sync the changes made to the remote code to your local code using Git commands. You will have to download the entire code again or make a patch out of it and apply it manually to your local code. Not good isn’t it? Hence you need to follow the below steps in order to sync code properly.

Download the kernel source using the appropriate ‘git clone’ command. For example,

git clone -b master https://github.com/ItsAnilSingh/android_kernel_samsung_logan2g.git kernel

The above command downloads or clones the master branch of the logan2g kernel source and stores it in kernel folder. Remember to replace ‘master’ with the ‘target branch’ and ‘kernel source url’ in the above code before you use it.

Now, whenever someone makes changes to the remote code, you just need to go to the kernel folder and pull changes from it to your local machine using git pull command.

Downloading Toolchain

After downloading Android kernel source, its time to download Toolchain. Without toolchain, its impossible to compile kernel as far as I know.

Before that you will need to find out the architecture of your Android device. Install Droid Hardware Info application on your Android device and open it. Click on the System tab. Check CPU Architecture and Instruction Sets values.

  • ARM – ARMv7 or armeabi
  • ARM64 – AArch64 or arm64
  • x86 – x86 or x86abi

Click on the following link and select the toolchain appropriate to your Android device architecture.

https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/

After selecting the toolchain, you will get a command to clone it on your local computer. Open command prompt in home directory and enter the command from the webpage to download the toolchain.

For example, GT-S7262’s architecture is ARM. So I will select arm/arm-eabi-4.6.

git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6

Build Android Kernel

After downloading Android kernel source and Toolchain, follow these steps in order to build Android kernel.

  1. Open command prompt and enter the following command to enter into the kernel folder. Assuming kernel folder to be in your home directory.
    cd ~/kernel/
  2. Replace ‘arm’ in the following command with your device architecture and then enter the resultant command.
    export ARCH=arm
  3. Replace ‘anil’ with your linux username, ‘arm-eabi-4.6’ with toolchain folder and “arm-eabi-” with toolchain name without version number in the following command and then enter the resultant command.
    export CROSS_COMPILE=/home/anil/arm-eabi-4.6/bin/arm-eabi-
  4. Now you need to find out your device code name. Remember you downloaded Droid Hardware Info app previously? Open it again. In Device tab, check Model value. Note down the value present inside parenthesis. For example, Star Pro has GT-S7262(logan2g). I will copy the value ‘logan’.
  5. Open your kernel source folder and then open arch directory. Now open folder corresponding to your device architecture. For example, GT-S7262 architecture is arm, so I will open arm folder. Open config folder. Search for the device code that you got in step 4. For example, I will search for logan. Copy the name of the file that you get from the search result. I got logan-vlx_defconfig as search result.
  6. Replace ‘logan-vlx_defconfig’ in the following command with the file name that you got in step 5 and enter the resultant commands one after the another.
    make logan-vlx_defconfig
    make zImage
  7. That’s it. Wait and watch the kernel getting compiled.
  8. After successful compilation, you will get your kernel inside arch/<your-device-architecture>/boot/ folder of your kernel source.
    zImage = compressed kernel image, Image = uncompressed kernel image

Note: You will need a ramdisk along with the above kernel image in order to make a boot image. zImage or Image is a kernel image not entire boot image. You cannot flash it without ramdisk.

If you have any doubts, or need help, or stuck at some point then leave a comment below. I will reply to your query as soon as possible.

Subscribe to our newsletter for latest post updates directly to your email inbox and YouTube channel for tech video tutorials.

9 Replies to “Build Android Kernel From Source Using Ubuntu OS

  1. Good day! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.If you know of any please share. Many thanks!

    1. If you are using WordPress CMS then you can use Yoast SEO plugin. If your website is custom designed, then you need to hire a web developer to achieve this task.

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen − 10 =