How to Compile a Linux Kernel for Android

Header

A while ago I was working on building a custom kernel for my Android phone. Once you get the source the compilation process is not as straightforward as I hoped. Here are the steps required to get from the kernel source to a flashable image for your phone.

Get a copy of the build toolchain and Linux kernel for your device

First download a copy of the prebuild toolchain from git.

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

Then locate a copy of a kernel for your device. I used the CyanogenMod kernel for the tuna device which is the code-name for the hardware of the Galaxy Nexus. Alternatively you can get a kernel from the Android source website.

Download the kernel with git:

git clone https://github.com/CyanogenMod/android_kernel_samsung_tuna.git  

Build the kernel configuration file

Change into the root directory of the kernel you just downloaded and run the following code:

export ARCH=arm  
export SUBARCH=arm  
export CROSS_COMPILE=/home/user/git/arm-eabi-4.4.3/bin/arm-eabi-  
make DEVICE_defconfig  

Replace the CROSSCOMPILE path with the path to where you cloned the toolkit repository and replace DEVICEdefconfig with the configuration name for the device you are using; I used tunadefconfig. If you are using CyanogenMod then use cyanogenmodDEVICE_defconfig. This will create a .config file with all the setting to compile the kernel with. You can edit the file with a text editor or run the following if you want to use a terminal GUI.

make menuconfig  

Compiling the kernel

Now the kernel is ready to be compiled. To start the compilation process run:

make -j 4  

-j 4 tells the compiler how many of your possessor’s cores to use for the compilation process. Since I have a quad core possessor I used 4, but you should change it to match your system. If you are unsure of what to use, use make with no options. This may take some time depending on the speed of your system, it took 20 minutes for me.

Once it is done if there are any kernel modules you want compiled for this kernel run:

make modules  

If everything went well and you got no errors then your kernel will be located at arch/arm/boot/zImage

Installing the Kernel

In order to turn the zImage file into a flashable image for an Android device you can use koush’s AnyKernel utility.

Clone the AnyKernel repository and replace kernel/zImage with the one you just created. Also remove everything from the system directory and replace it with any kernel modules you created. NOTE: Some devices may require a modified version of AnyKernel to work. The Galaxy Nexus should use the AnyKernel form the Galaxy Nexus Project. If you are not using any special kernel modules then you can skip line 5. Otherwise the output of “make modules” should tell you where to copy the modules from.

git clone https://github.com/Galaxy-Nexus-Project/AnyKernel.git  
cd AnyKernel  
cp ../android_kernel_samsung_tuna/arch/arm/boot/zImage kernel/zImage  
rm system/lib/modules/*  
cp PATH_TO_COMPILED_MODULES system/lib/modules/  
zip -r9 newKernel.zip *  

The newKernel.zip you just created should now install your new kernel and modules on your device. Note: if your recovery requires zips to be signed then you will need to sign it before it will work.