Compile our own Android Kernel in 5 Simple Steps
The Android kernel helps the applications to communicate with the hardware components of the device.
For example:
- Most of us are familiar with game mode. What it does is instructs the processor and the graphics processing unit to run at their maximum frequencies.
- Another example is power saver mode. It instructs the processor and the graphics processing unit to run at their minimum frequencies.
Need to compile our own kernel: Compiling our own kernel might prove very useful as:
- The user experience can be further optimized as per the need using our own kernel
- It can also help in Open Source Development.
Steps to compile our own kernel:
- Prerequisites: Below are the prerequisites required to compile our own Android Kernel:
- Ubuntu or any other Linux based OS
- Familiar with basic Linux Commands
- Familiar with Github
- Device source code
Note: This article is for a device with a 64bit Snapdragon SOC
- Install Dependencies Open the terminal and paste the following:
sudoapt-getinstallgit ccache automake flex lzop bison \gperf build-essential zip curlzlib1g-dev zlib1g-dev:i386 \g++-multilib python-networkxlibxml2-utilsbzip2libbz2-dev \libbz2-1.0 libghc-bzlib-devsquashfs-tools pngcrush \schedtool dpkg-dev liblz4-toolmakeoptipng maven libssl-dev \pwgen libswitch-perl policycoreutilsminicom libxml-sax-base-perl \libxml-simple-perlbclibc6-dev-i386 lib32ncurses5-dev \x11proto-core-dev libx11-devlib32z-dev libgl1-mesa-dev xsltproc unzipchevron_rightfilter_none - Download Required Files:
- Clone the device source on local disk:
mkdirmykernelgit clone {link to your device kernelsource}chevron_rightfilter_none - Download a compatible GCC toolchain. In this article, AOSP’s GCC is used.
cdmykernelgit clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9toolchainchevron_rightfilter_none - Download a compatible CLANG toolchain. In this article, AOSP’s CLANG is used.
- Move the downloaded file in the mykernel folder and then extract using the following command:
tarvxzf linux-x86-android-9.0.0_r48-clang-4691093.tar.gzchevron_rightfilter_none
- Clone the device source on local disk:
- Compiling The Kernel:
cdmykernelrm-rf outmkdiroutexportARCH=arm64exportSUBARCH=arm64exportDTC_EXT=dtcmakeO=out ARCH=arm64 {device defconfig}PATH="${PWD}/bin:${PWD}/toolchain/bin:${PATH}"\make-j$(nproc --all) O=out \ARCH=arm64 \CC=clang \CLANG_TRIPLE=aarch64-linux-gnu- \CROSS_COMPILE=aarch64-linux-android-|teekernel.logchevron_rightfilter_noneHere, replace the {device defconfig} with the name of your config file. You can find it in /arch/arm64/configs folder.
- Booting The Compiled Kernel:
- Browse to /out/arch/arm64/boot and find the Image-dtb file (compiled zImage) and copy the file.
- Download Android Image Kitchen and decompile your stock boot image. Once you decompile it you’ll find the stock zImage in the decompiled folder. Replace it with the one you copied earlier and recompile the boot image.
- Flash via fastboot using the following command:
fastboot flash boot mykernel.imgchevron_rightfilter_none
- Dealing with the encountered errors: A kernel.log file will be generated in mykernel folder. Find the line which says error and search for the solution. Also please don’t forget to attach the log file when posting in forums for help.
This will be the basic kernel, and more features can be added once it boots successfully.
Recommended Posts:
- How to build a simple Calculator app using Android Studio?
- How to build a simple music player app using Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | AdMob Banner Ads for Android Studio
- Android | How to Request permissions in Android Application?
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to add Radio Buttons in an Android Application?
- Android | Android Application File Structure
- How to Compile and Run C/C++/Java Programs in Linux
- The Linux Kernel
- Pattern compile(String) method in Java with Examples
- Pattern compile(String,int) method in Java with Examples
- Initializing and Cache Mechanism in Linux Kernel
- Linux Kernel Module Programming: Hello World Program
- Android | Running your first Android app
- Difference between Compile-time and Run-time Polymorphism in Java
- Allocating kernel memory (buddy system and slab system)
- Simple Calculator using TCP in Java
- Simple Calculator via UDP in Java
- Simple Calculator in Bash
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



