上一篇文章提到,启动模拟器使用的Android内核镜像是AOSP提前编译好的。而AOSP中又不包含Linux Kernel的源码。为了能全方位立体化的搞懂Android那就自己下载并编译Linux Kernel for Android吧。
1.下载
Linux Kernel for Android的源代码使用Git管理
首先我们新建一个文件夹用来装即将下载的代码
mkdir kernel
cd kernel/
然后执行git clone命令进行下载
git clone https://android.googlesource.com/kernel/goldfish.git
内核的工程也就几百M大小,远不及AOSP那样庞大,所以一会儿就下载完了。
下载完成后,kernel目录下会出现一个goldfish文件件
2.切换分支
先来看一下情况
git branch -a
得到如下输出
remotes/origin/android-3.10
remotes/origin/android-3.18
remotes/origin/android-3.4
remotes/origin/android-goldfish-2.6.29
remotes/origin/android-goldfish-3.10
remotes/origin/android-goldfish-3.10-k-dev
remotes/origin/android-goldfish-3.10-l-mr1-dev
remotes/origin/android-goldfish-3.10-m-dev
remotes/origin/android-goldfish-3.10-n-dev
remotes/origin/android-goldfish-3.18
remotes/origin/android-goldfish-3.18-dev
remotes/origin/android-goldfish-3.4
remotes/origin/android-goldfish-3.4-l-mr1-dev
remotes/origin/android-goldfish-4.4-dev
remotes/origin/heads/for/android-goldfish-3.18-dev
remotes/origin/linux-goldfish-3.0-wip
remotes/origin/master
由于编译的内核需要运行在模拟器上,所以应该选择goldfish版本,而我选择了android-goldfish-3.4。
git checkout remotes/origin/android-goldfish-3.4
3.编译
添加交叉编译工具目录到PATH
export PATH=$PATH:/home/windcake/Documents/aosp/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin
然后执行如下命令指定要编译的体系结构和交叉编译工具前缀
export ARCH=arm
export SUBARCH=arm
export CROSS_COMPILE=arm-eabi-
执行配置编译选项
make goldfish_armv7_defconfig
开始编译
make -j4
编译完成后会看到如下输出
OBJCOPY arch/arm/boot/zImage
Kernel: arch/arm/boot/zImage is ready
是的,zImage已经准备好了。
4.运行模拟器
-kernel告诉模拟器使用指定内核来启动后面的参数是zImage的路径
emulator -kernel /kernel/goldfish/arch/arm/boot/zImage
5.查看版本信息
adb shell
cd proc/
cat version
会看到如下输出
Linux version 3.4.67-g880d9af-dirty (windcake@windcake-ubuntu)
(gcc version 4.8 (GCC) ) #5 PREEMPT Wed Feb 21 18:09:14 CST 2018