Compile our own Android Kernel in 5 Simple Steps - GeeksforGeeks

23 Feb, 2021
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.
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 OSFamiliar with basic Linux CommandsFamiliar with GithubDevice source codeNote: This article is for a device with a 64bit Snapdragon SOCInstall Dependencies Open the terminal and paste the following:sudo apt-get install     git ccache automake flex lzop bison \     gperf build-essential zip curl         zlib1g-dev zlib1g-dev:i386 \     g++-multilib python-networkx          libxml2-utils bzip2 libbz2-dev \     libbz2-1.0 libghc-bzlib-dev          squashfs-tools pngcrush \     schedtool dpkg-dev liblz4-tool          make optipng maven libssl-dev \     pwgen libswitch-perl policycoreutils          minicom libxml-sax-base-perl \     libxml-simple-perl bc         libc6-dev-i386 lib32ncurses5-dev \     x11proto-core-dev libx11-dev          lib32z-dev libgl1-mesa-dev xsltproc unzipDownload Required Files:Clone the device source on local disk:mkdir mykernel  git clone {link to your device kernel source}Download a compatible GCC toolchain. In this article, AOSP’s GCC is used.cd mykernel   git clone https://android.googlesource.com/platform/             prebuilts/gcc/linux-x86/aarch64/             aarch64-linux-android-4.9   toolchainDownload 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:tar vxzf linux-x86-android-9.0.0_r48-clang-4691093.tar.gzCompiling The Kernel:cd mykernelrm -rf outmkdir outexport ARCH=arm64export SUBARCH=arm64export DTC_EXT=dtc  make O=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-                      | tee kernel.logHere, 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.imgDealing 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.
Prerequisites: Below are the prerequisites required to compile our own Android Kernel:Ubuntu or any other Linux based OSFamiliar with basic Linux CommandsFamiliar with GithubDevice source codeNote: This article is for a device with a 64bit Snapdragon SOC
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:sudo apt-get install     git ccache automake flex lzop bison \     gperf build-essential zip curl         zlib1g-dev zlib1g-dev:i386 \     g++-multilib python-networkx          libxml2-utils bzip2 libbz2-dev \     libbz2-1.0 libghc-bzlib-dev          squashfs-tools pngcrush \     schedtool dpkg-dev liblz4-tool          make optipng maven libssl-dev \     pwgen libswitch-perl policycoreutils          minicom libxml-sax-base-perl \     libxml-simple-perl bc         libc6-dev-i386 lib32ncurses5-dev \     x11proto-core-dev libx11-dev          lib32z-dev libgl1-mesa-dev xsltproc unzip
sudo apt-get install     git ccache automake flex lzop bison \     gperf build-essential zip curl         zlib1g-dev zlib1g-dev:i386 \     g++-multilib python-networkx          libxml2-utils bzip2 libbz2-dev \     libbz2-1.0 libghc-bzlib-dev          squashfs-tools pngcrush \     schedtool dpkg-dev liblz4-tool          make optipng maven libssl-dev \     pwgen libswitch-perl policycoreutils          minicom libxml-sax-base-perl \     libxml-simple-perl bc         libc6-dev-i386 lib32ncurses5-dev \     x11proto-core-dev libx11-dev          lib32z-dev libgl1-mesa-dev xsltproc unzip
Download Required Files:Clone the device source on local disk:mkdir mykernel  git clone {link to your device kernel source}Download a compatible GCC toolchain. In this article, AOSP’s GCC is used.cd mykernel   git clone https://android.googlesource.com/platform/             prebuilts/gcc/linux-x86/aarch64/             aarch64-linux-android-4.9   toolchainDownload 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:tar vxzf linux-x86-android-9.0.0_r48-clang-4691093.tar.gz
Clone the device source on local disk:mkdir mykernel  git clone {link to your device kernel source}
mkdir mykernel  git clone {link to your device kernel source}
Download a compatible GCC toolchain. In this article, AOSP’s GCC is used.cd mykernel   git clone https://android.googlesource.com/platform/             prebuilts/gcc/linux-x86/aarch64/             aarch64-linux-android-4.9   toolchain
cd mykernel   git clone https://android.googlesource.com/platform/             prebuilts/gcc/linux-x86/aarch64/             aarch64-linux-android-4.9   toolchain
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:tar vxzf linux-x86-android-9.0.0_r48-clang-4691093.tar.gz
tar vxzf linux-x86-android-9.0.0_r48-clang-4691093.tar.gz
Compiling The Kernel:cd mykernelrm -rf outmkdir outexport ARCH=arm64export SUBARCH=arm64export DTC_EXT=dtc  make O=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-                      | tee kernel.logHere, replace the {device defconfig} with the name of your config file. You can find it in /arch/arm64/configs folder.
cd mykernelrm -rf outmkdir outexport ARCH=arm64export SUBARCH=arm64export DTC_EXT=dtc  make O=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-                      | tee kernel.log
Here, 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.img
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.img
fastboot flash boot mykernel.img
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.
Android-Misc
Technical Scripter 2019
Android
Java
Linux-Unix
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
How to Read Data from SQLite Database in Android?
How to Change the Background Color After Clicking the Button in Android?
Android Listview in Java with Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java