Android Kernel Compilation
===
Using Nexus 4 as example
Environment:
* Ubuntu 16.04 LTS
* kernel version 4.4.0-47-generic
* CPU

# Step
1. [download](https://source.android.com/source/building-kernels.html) andorid kernel source of a particular version.
`$ git clone https://android.googlesource.com/kernel/msm.git`
* the master branch is empty[^[1]^](https://groups.google.com/forum/?fromgroups=#!topic/android-kernel/Rq5993NokVs%5B1-25%5D), using `$ git branch -r ` to see the list of remote branch.
* `$ git checkout -b <local branch name> <remote branch name>`
* Here I use origin/android-msm-mako-3.4-lollipop-release
2. download cross compiler for example : arm-linux-androideabi-4.6
`$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6`
3. setup environment variables
`export ARCH=arm`
`export PATH=$PATH:${path to cross compiler}/arm-linux-androideabi-4.6/bin`
`export CROSS_COMPILE=arm-linux-androideabi-`
4. choose a config file
`$ make mako_defconfig `
In directoty `/arch/arm/configs/`, choose a default config
* make menuconfig to view available options
* append addtional build version number ...etc
5. build the kernel
`$ make -j4`
If succeeds, there will be a zImage file in `/arch/arm/boot`
* kernel/timeconf.pl line 373 may have error, comments the
```
@@ -370,9 +370,9 @@ if ($hz eq '--can') {
}
@val = @{$canned_values{$hz}};
- if (!defined(@val)) {
- @val = compute_values($hz);
- }
+ #if (!defined(@val)) {
+ # @val = compute_values($hz);
+ #}
output($hz, @val);
}
```
6. install the kernel to you phone [^[5]^](http://forum.xda-developers.com/showpost.php?p=37379748&postcount=40)
I use factory imgaes from [here](https://developers.google.com/android/images) and wrap new zImage into boot.img
* `$ sudo apt-get install abootimg`
* extract `boot.img` by `$ abootimg -x boot.img`
* update bootsize in `bootimg.cfg` with Hex format
If the bootsize is unknown, directly run the commands below. abootimg will promt out a message saying that the bootsize is incorrect and the correct bootsize is displayed

* pack new boot.img
`$ abootimg --create <your_boot.img> -f bootimg.cfg -k zImage -r initrd.img`
* use fastboot to flash new boot.img,
`$ fastboot boot <your_boot.img>`
7. See if you compile success
Check the phone status

# Comments
* Before building the kernel, you can use `$ make menuconfig ` to change the kernel name

or modify the Makefile in the root to add custom kernel version

# Reference:
[1] https://groups.google.com/forum/?fromgroups=#!topic/android-kernel/Rq5993NokVs%5B1-25%5D
[2][Tutorial: Android Kernel Configure, Compile & Install (Part 1)](https://www.youtube.com/watch?v=IV4jhXWc7AE&t=154s)
[3][Tutorial: Android Kernel Configure, Compile & Install (Part 2)](https://www.youtube.com/watch?v=gjN8x8Pfcl0)
[4] http://forum.xda-developers.com/nexus-4/general/howto-build-nexus-4-kernel-t2021202/[tunnel rush](https://tunnelrush3.com)
[5] http://forum.xda-developers.com/showpost.php?p=37379748&postcount=40