Android-Kernel-Compilation

Using Nexus 4 as example

Environment:

  • Ubuntu 16.04 LTS
  • kernel version 4.4.0-47-generic
  • CPU

Step

  1. download andorid kernel source of a particular version.
    $ git clone https://android.googlesource.com/kernel/msm.git
    • the master branch is empty^[1]^, 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
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      @@ -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]^
    I use factory imgaes from here 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)
[3]Tutorial: Android Kernel Configure, Compile & Install (Part 2)
[4] http://forum.xda-developers.com/nexus-4/general/howto-build-nexus-4-kernel-t2021202
[5] http://forum.xda-developers.com/showpost.php?p=37379748&postcount=40