In this guide, we are going to explain to you how to build an Android kernel on Windows 10 – yes, we will still be using a Linux build environment, however, it will be a Linux subsystem inside Windows 10. So if you are a Windows 10 user interested in developing for Android, follow our guide carefully. This guide will help you to compile a custom kernel for any device owning a kernel source provided by OEM.
What is Kernel?
A kernel is a system software of an operating system that carries out I/O tasks and produces an interface between hardware and software. Android kernel uses a modified Linux kernel which is documented under GNU GPL.
Build Kernel For Specific Device:
A kernel source is provided by OEM itself which can be found at their Git-hub Repository. Note that a Kernel compiled for a particular device cannot be flashed on other devices.
- A PC with Windows 10 x64 or Linux Installed
- Familiar with basic Linux Commands
Setting up the Linux Environment
- On Windows 10, go to Settings > Update and Security > For Developers > enable Developers Mode.
- Now go to Control Panel > Programs > Turn Windows Features On or Off > enable Windows Subsystem for Linux.
- Reboot your PC.
- Now go to the Windows app store, and download Ubuntu.
- Launch Ubuntu on the Windows 10 desktop, and it will request a username and password.
- In Ubuntu, launch the native terminal and type the following command:
- This will proceed to update all repos for apps and dependencies.
- Next in the terminal type:
sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2
- In order to check if all dependencies were correctly installed, type ‘gcc’ in the terminal (no quotes).
- If “gcc” is already installed, you should see “gcc : fatal error : no input file”
- Now you can type ‘make’ in the terminal. If “make” is already installed, you should see “make: *** no target specified and no makefile found. stop.”
- Next type ‘
git’, and if “git” is already installed, you should see a bunch of basic git commands.
- Now we need some toolchains (there are several types, including GCC, Linaro, and a few custom ones). Some devices may require different toolchains, for example, not all device kernels will boot or compile with GCC.
Accessing your Ubuntu folders from Windows 10
Your path to Ubuntu should be:
But you should not edit files directly from Windows, as this will break the permissions on them – you would then need to reset the permissions from within the Linux terminal otherwise you will never be able to use that file again.
Setting Linux sub-system on windows finally ends here. From now a Linux user can also follow the steps. Let’s start our guide on how to build an android kernel on Windows or Linux.
For ARM Devices
We will be using GCC 4.9 for this.
- Open the Linux terminal and type: mkdir kernel
- Now type: cd mykernel
- (it doesn’t have to be ‘mykernel’, this is for simplicity’s sake, you can name it whatever you want.)
- Now type:
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.9
For ARM 64 Devices
You need a 64-bit kernel compiler for ARM 64 devices
- You can get by typing this:
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9
Download a compatible CLANG toolchain. From here AOSP Clang. Move the downloaded file in the mykernel folder and then extract using the following command:
tar zxvf linux-x86-android-9.0.0_r48-clang-4691093.tar.gz
Download Kernel Source:
You are going to need to download your kernel source. Open the Linux terminal and make sure that you are in the same folder you previously created. If not then use this “
cd your-folder-name” just replace the name with ur folder’s name.
Now type in terminal: “git clone “link of your device kernel source on github” -b “name of the branch”
For Example: “
git clone https://github.com/atxoxx/android_ke…amsung_msm8974 -b stable”
We are now done with download and installations part.
Now you have to find your defconfig. It will be named the same as your device-codename_defconfig. Like for Android One: sprout_defconfig or for Redmi Note 5 Pro which codename is whyred it will be named as whyred_defconfig.
The kernel folders are typically as follows:
- /arch/arm/configs: This contains various defconfig files for the devices.
- /output/arch/arm/boot/: This is where the zimage will be stored. The final kernel file.
Compiling the Kernel
To make it easier, you can navigate to the location in the file explorer. It should be /home/user ID/kernel (or whatever you named the kernel folder).
You should see two folders inside, for the toolchain and the kernel source. Go inside the kernel source folder. By using “cd folder-name”
How to build Android kernel on Windows/Linux
Follow the below steps for compiling your own custom Android kernel. Read all the steps carefully otherwise you may end up with an error. Cause compiling a kernel is a little complicated if for newbies.
Compiling The Kernel:
Open terminal and use the following commands for specific device ARM/ARM64.
cd mykernel(name of your kernel folder)
rm -rf out
make O=out ARCH=arm device-code_defconfig(replace device-code with yours)
make -j$(nproc --all) O=out ARCH=arm CC-clang CLANG_TRIPLE=arm-linux-gnueabi- CROSS_COMPILE=arm-linux-androideabi-
cd mykernel(name of your kernel folder)
rm -rf out
make O=out ARCH=arm64 device-code_defconfig(replace device-code with your device)
make -j$(nproc --all) O=out ARCH=arm64 CC-clang CLANG_TRIPLE=aarch64-linux-gnu- CROSS_COMPILE=aarch64-linux-android-
Now your kernel compilation will start. It may take a while but typically not very long, compiling a kernel is not like compiling an entire Android ROM. This is really CPU dependent – for example, an AMD Phenom X4 3.4GHz with 8GB of RAM should take around 10 minutes to compile from start to finish.
When it finishes, it will notify you with a message like “zimage is ready”.
Booting The Compiled Kernel:
- Browse to /out/arch/arm64/boot or /out/arch/arm/boot and find the (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 your-filename.img
Well, that’s it! Our guide on how to build Android kernel on Windows or Linux finally ends here. You just compiled your first kernel for your android device. This will be the basic kernel, more features can be added once your device successfully boots the kernel. In case if you face any problems on kernel compilation kindly use the comment box below. Thank you!