1、下载nexus5 对应的内核源码

Android系统使用的是Linux内核,但与主线分枝的Linux内核又有一些差异,各个不同的厂商又有自己的修改。内核源码位于谷歌境外服务器,不FQ无法访问,这给同步源码造成很大的不便。幸好清华大学的开源服务器上也同步了AOSP内核的源码,使得我们无须FQ就可以同步源码。

映射关系如下:

名称GoogleGIT地址清华服务器地址commonhttps://android.googlesource.com/kernel/common.githttps://aosp.tuna.tsinghua.edu.cn/kernel/common.gitexynoshttps://android.googlesource.com/kernel/exynos.githttps://aosp.tuna.tsinghua.edu.cn/kernel/exynos.gitgoldfishhttps://android.googlesource.com/kernel/goldfish.githttps://aosp.tuna.tsinghua.edu.cn/kernel/goldfish.githikey-linarohttps://android.googlesource.com/kernel/hikey-linarohttps://aosp.tuna.tsinghua.edu.cn/kernel/hikey-linaro.gitlkhttps://aosp.tuna.tsinghua.edu.cn/kernel/lk.gitmsmhttps://android.googlesource.com/kernel/msm.githttps://aosp.tuna.tsinghua.edu.cn/kernel/msm.gitomaphttps://android.googlesource.com/kernel/omap.githttps://aosp.tuna.tsinghua.edu.cn/kernel/omap.gitsamsunghttps://android.googlesource.com/kernel/samsung.githttps://aosp.tuna.tsinghua.edu.cn/kernel/samsung.gittegrahttps://android.googlesource.com/kernel/tegra.githttps://aosp.tuna.tsinghua.edu.cn/kernel/tegra.gitx86_64https://android.googlesource.com/kernel/x86_64.githttps://aosp.tuna.tsinghua.edu.cn/kernel/x86_64.git

由于nexus5使用的是高通处理器,所以直接同步msm的链接

ninjame@ubuntu1604:~/aosp$mkdirkernelninjame@ubuntu1604:~/aosp$cdkernelninjame@ubuntu1604:~/aosp/kernel$gitclonehttps://aosp.tuna.tsinghua.edu.cn/kernel/msm.git

git branch 查看Android内核源码的代码版本分支

ninjame@ubuntu1604:~/aosp/kernel/msm$gitbranch-a*(头指针分离于7717f76)masterremotes/origin/HEAD->origin/masterremotes/origin/android-4.4remotes/origin/android-4.4.y……remotes/origin/android-msm-hammerhead-3.4-kitkat-mr1remotes/origin/android-msm-hammerhead-3.4-kitkat-mr2remotes/origin/android-msm-hammerhead-3.4-kk-fr1remotes/origin/android-msm-hammerhead-3.4-kk-fr2remotes/origin/android-msm-hammerhead-3.4-kk-r1remotes/origin/android-msm-hammerhead-3.4-l-previewremotes/origin/android-msm-hammerhead-3.4-lollipop-mr1remotes/origin/android-msm-hammerhead-3.4-lollipop-mr1.1remotes/origin/android-msm-hammerhead-3.4-lollipop-releaseremotes/origin/android-msm-hammerhead-3.4-m-previewremotes/origin/android-msm-hammerhead-3.4-marshmallowremotes/origin/android-msm-hammerhead-3.4-marshmallow-mr1remotes/origin/android-msm-hammerhead-3.4-marshmallow-mr2remotes/origin/android-msm-hammerhead-3.4-marshmallow-mr3……

和nexus5 相关的且是adnoid6.0 版本的内核分支共有4个,到底checkout哪个分支呢?对于Android的源码与Android内核源码版本的对应关系,很难说得清楚。经过摸索,找到一种比较讨巧的方法,直接进入手机设置,查看关于手机

内核版本是3.4.0-g7717f76

ninjame@ubuntu1604:~/aosp/kernel/msm$gitcheckout3.4.0-g7717f76

当发现真的把相应分支源码的源码给同步过来,竟然有种意外的惊喜,这是怎么回事呢?

ninjame@ubuntu1604:~/aosp/kernel/msm$gitbranch-r--contains7717f769b2d0bf26db19598d8826a01b82ab6540origin/android-msm-hammerhead-3.4-marshmallow-mr1origin/android-msm-hammerhead-3.4-marshmallow-mr2origin/android-msm-hammerhead-3.4-marshmallow-mr3

7717f76对应着源码中的一个commit节点,可以看做是git库上最小的分支(分支都是从commit节点拉出来的,那么每一个节点都可以看做一个分支,当然某些分支中只包含它自己)

3.4.0-g7717f76 就可以解读为该内核版本大的分支为linux3.4.0,具体的源码对应于commit节点为7717f76 那一次提交代码对应的快照。

下载部分参考博客:

http://blog.csdn.net/sunao2002002/article/details/53057374

http://blog.csdn.net/qq1084283172/article/details/54880488

2、配置编译环境

由于是在 Linux / Mac OS X 上进行编译,所以交叉编译的环境变量是必须要设置的,一共涉及到三个。

首先设定 ARCH,–注意,由于 Nexus 5 是32位架构,所以取值为 arm,如果是64位,则要取值为 arm64

export ARCH=arm

然后设定 CROSS_COMPILE,同上,64位要取值 aach74-linux-android-

export CROSS_COMPILE=arm-eabi-

对应地,告诉编译系统到哪里寻找编译器

export PATH=$PATH:/home/ninjame/aosp/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin

3、编译

开始编译之前,不要着急开始,先使用 help 看有哪些预定义好的配置

make help

从上面可以找到 hammerhead_defconfig,这正是 Nexus 5 的默认配置

make hammerhead_defconfig

上述就是生成配置,如果需要调整,则可以继续执行

make menuconfig

最后执行编译吧

make

成功编译后,可以看到类似如下的输出:
OBJCOPY arch/arm/boot/zImage
Kernel: arch/arm/boot/zImage is ready
DTC arch/arm/boot/msm8974-hammerhead-rev-11.dtb
DTC arch/arm/boot/msm8974-hammerhead-rev-11j.dtb
DTC arch/arm/boot/msm8974-hammerhead-rev-10.dtb
DTC arch/arm/boot/msm8974-hammerhead-rev-c.dtb
DTC arch/arm/boot/msm8974-hammerhead-rev-b.dtb
DTC arch/arm/boot/msm8974-hammerhead-rev-bn.dtb
DTC arch/arm/boot/msm8974-hammerhead-rev-a.dtb
DTC arch/arm/boot/msm8974-hammerhead-rev-f.dtb
CAT arch/arm/boot/zImage-dtb
Kernel: arch/arm/boot/zImage-dtb is ready
make[1]: Nothing to be done for 'arch/arm/boot/dtbs'.
编译碰到的问题;

,line,m.group(/usr/bin/-s/usr/bin/python2./usr/bin/python,默认设置成python2.

其实,提示的错误信息已经明确告诉你了,你应该省略defined()。打开kernel/timeconst.pl

4、刷新内核

进入到了 Android 源代码目录,设置环境变量,告知已编译好的新内核地址

ninjame@ubuntu1604:~/aosp/kernel/msm$cdarch/arm/boot/ninjame@ubuntu1604:~/aosp/kernel/msm/arch/arm/boot$ls-lzImage-dtb-rw-rw-r--1ninjameninjame84411445月2300:27zImage-dtbninjame@ubuntu1604:~/aosp/kernel/msm/arch/arm/boot$pwd/home/ninjame/aosp/kernel/msm/arch/arm/boot

export TARGET_PREBUILT_KERNEL=/home/ninjame/aosp/kernel/msm/arch/arm/boot/zImage-dtb

重新编译bootp_w_picpath

make bootp_w_picpath

成功后,若有类似如下的输出:

Copy:out/target/product/hammerhead/kernelTargetbootp_w_picpath:out/target/product/hammerhead/boot.imgout/target/product/hammerhead/boot.imgmaxsize=23519232blocksize=135168total=9369600reserve=270336####makecompletedsuccessfully(03:29(mm:ss))####

重启手机进入bootloader模式

adb reboot bootloader

刷入内核

fastboot flash boot boot.img

重启手机

fastboot reboot

~晒图,换上自己的签名了,哈哈~ ,不过g4ad03fa 打脸了。。。