How to compile your own Android Kernel


earlier this week I compiled my own kernel for my Nexus 5 with Android 5.1.1. If you want to know how to do that read along (You need your Nexus to have the bootloader unlocked and need it to be rooted for this to work).

Why tho?

For a project of mine (I will be writing about that soon) I wanted to add the functionality to my Nexus to send keystroke commands (like a keyboard) to a connected PC via USB (get registered as a HID). First I stumbled upon this app USB Keyboard. After installing and starting the app it told me I needed to install a custom kernel. For someone with a little bit of experience with flashing roms and stuff this was not a big thing to ask (If you need help here google should do the trick). I tried all of the linked kernels to no avail. They just were not compatible with the latest stock rom (5.1.1). Finally I found the github of the patch witch enables this feature. Along with a nice guide to compile it yourself.

Step-by-Step Compile

DISCLAIMER: I will describe all the steps it took me to compile the kernel for my Nexus 5 with Android 5.1.1 if you run on any other hardware / software this most likely will not work for you but you can use this How-to as guidance.

First of all you gonna need to get the toolchain. The 4.7 one seems the only one that works correctly with the Nexus 5 at the moment.

#Make sure you are in a working directory
git clone

Next open your favorite text editor (I use nano) and make a file with following content:

#File creation
export CC=$(pwd)/arm-eabi-4.7/bin/arm-eabi-
export CROSS_COMPILE=$(pwd)/arm-eabi-4.7/bin/arm-eabi-
export ARCH=arm
export SUBARCH=arm
export PATH=$PATH:$(pwd)/andorid_boot_tools_bin

Save it, make it executable and source it to the current terminal

chmod +x
#For cross-compiling

Next we will clone the actual kernel source (this may take some time ~1GB of data)

git clone

After this we switch into that directory

cd msm/

Next we will switch to the correct branch. First list all the available branches:

git branch -a

You will get a list of all the branches available. Now we choose the correct one (for me “android-msm-hammerhead-3.4-lollipop-release”)

git checkout origin/android-msm-hammerhead-3.4-lollipop-release

Now you should be able to actually compile the kernel…but wait we wanted to include a patch didn’t we?

First you have to get the *.patch file. I am patching a 3.4 Kernel if you need another patch file check Pelyas github


Next we have to apply the patch:

patch -p1 < ../kernel-3.4.patch

Now we can actually compile the kernel. This may take some time again. Get a coffee.

make hammerhead_defconfig
# -j4 stands for Quadcore if I am not mistaken
make -j4

Now you have your kernel (zImage-dtb) and you are ready to go. So save this file to a place you remember! On nearly any embedded Linux device. But Android needs something more. You need to make a special boot partition which can be flashed to your device via fastboot. And that’s what we’ll do in the next step.

Step-by-Step make the boot.img

Now that you have your kernel ready you need to make a boot partition. Usually you would have to download a whole bunch of data and stuff. But Pete Batard wrote a tool to extract a boot.img and pack it with another kernel. First we have to get the original “boot.img”. For the Nexus 5 you can get the stock roms from their website. You have to unpack the zip file once and then unzip another file to get to the “boot.img”. Save that file somewhere you remember. After that we get the source for the unpack/repack tool (I highly recommend to do this in a new terminal window so you don’t accidentally cross-compile). Switch back to your working directory (the one above “msm/”), clone the git repository and compile:

git clone
cd bootimg-tools/libmincrypt/
gcc -c *.c -I../include
ar rcs libmincrypt.a *.o
cd ../mkbootimg
gcc mkbootimg.c -o mkbootimg -I../include ../libmincrypt/libmincrypt.a
gcc -o unmkbootimg unmkbootimg.c

After that we can unpack the boot.img.

unmkbootimg -i {path/to/your/}boot.img

Now we get two files a “kernel” and a “ramdisk.cpio.gz”. The ramdisk contains the partition and we need to repack this now but with our own kernel (“zImage-dtb”) instead.

mkbootimg --base 0 --pagesize 2048 --kernel_offset 0x00008000 --ramdisk_offset 0x02900000 --second_offset 0x00f00000 --tags_offset 0x02700000 --cmdline 'console=ttyHSL0,115200,n8 androidboot.hardware=hammerhead user_debug=31 maxcpus=2 msm_watchdog_v2.enable=1' --kernel {path/to/your/}zImage-dtb --ramdisk ramdisk.cpio.gz -o new_boot.img

Now we have our new flash able kernel with the file “new_boot.img” which can easily be flashed with fastboot:

fastboot boot new_boot.img

If you need further help with flashing try to hitting up goolge or leave a comment below with your question.


After you did all of the above you are finally able to use your Nexus 5 as a HID device. You can test this out very easily by downloading the App “USB Keyboard“. There is also a precompiled executable for Android which you can use to access this functionality via script e.g. Tasker. More on that in a later post of mine and on the github page of pelya.

Credit where credit belongs

I give my thanks to pelya for the awesome work on his/her HowTo and the work on the kernel patch! Also big thanks to Marcin for the post. Without those two guides I would have been lost. And last but not least thanks to Pete for his tool to make a flashable boot.img without having to download gigs of data.


Finally here is a link to the patched 3.4 kernel for the Nexus 5 (hammerhead) with Android 5.1.1 stock rom

Please leave feedback in the comments!