接上篇 (Ubuntu12.04编译Android4.0.1源码全过程),编译好后在目录/media/娱乐/androidSource/out/target/product/generic 下有三个镜像文件:ramdisk.img, userdata.img, system.img,模拟器运行除了这三个镜像文件外,还需要/media/娱乐/androidSource/prebuilt/android-arm/kernel 目录下的kernel-qemu文件。为了运行模拟器,sudo gedit /etc/profile设置环境变量:
export PATH=$PATH:/media/娱乐/androidSource/out/host/linux-x86/bin
export ANDROID_PRODUCT_OUT=/media/娱乐/androidSource/out/target/product/generic
然后source /etc/profile. 启动模拟器的命令emulator就在/media/娱乐/androidSource/out/host/linux-x86/bin目录下,当emulator没有任何参数时会使用ANDROID_PRODUCT_OUT目录下的三个镜像及上面的kernel-qemu文件。终端运行emulator后就会启动模拟器,出现:emulator: WARNING: system partition size adjusted to match image file (163 MB > 66 MB) 这样的警告。也可以用如下命令指定内核和那三个镜像:emulator -kernel ./prebuilt/android-arm/kernel/kernel-qemu -sysdir ./out/target/product/generic -system system.img -data userdata.img -ramdisk ramdisk.img
上面的内核是google编译好的,为了自己编译内核需要自己下载内核源码。下载步骤:
1,创建目录mkdir /home/yan/kernel, 在这个目录下,终端输入:git clone http://android.googlesource.com/kernel/goldfish.git
会看到以下提示:
Cloning into 'goldfish'... remote: Sending approximately 499.97 MiB ... remote: Counting objects: 41051, done remote: Finding sources: 100% (144/144) remote: Getting sizes: 100% (72/72) remote: Compressing objects: 100% (314839/314839) remote: Total 2442786 (delta 2048918), reused 2442751 (delta 2048913) Receiving objects: 100% (2442786/2442786), 499.75 MiB | 408 KiB/s, done. Resolving deltas: 100% (2049084/2049084), done.
yan@ubuntu:~/kernel/goldfish$ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/android-goldfish-2.6.29 remotes/origin/android-goldfish-3.4 remotes/origin/linux-goldfish-3.0-wip remotes/origin/master
yan@ubuntu:~/kernel/goldfish$ git checkout remotes/origin/android-goldfish-2.6.29
快下载完时,看到如下信息:
Checking out files: 100% (26821/26821), done.
Note: checking out 'remotes/origin/android-goldfish-2.6.29'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at e3d684d... enable FUSE for sdcard emulation
这就表示内核下载完毕。文件夹goldfish除去上面的隐藏文件夹.git, 大小为360M,压缩文件下载链接:http://ajh59wyr6i.l9.yunpan.cn/lk/QteFfbQWrhmh6 下载后可以直接按下面步骤进行编译。
为了编译内核,需要如下步骤:
1,设置交叉编译器路径:
export PATH=$PATH:/media/娱乐/androidSource/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin
2,修改goldfish目录下的Makefile文件的以下两行为:
3,输入:make goldfish_armv7_defconfig,这里注意如果输入make goldfish_defconfig编译好的内核在启动时,将启动不了,一直黑屏。最初我弄的也是黑屏启动不了,参考后面参考2才找到答案。
看到如下信息:
yan@ubuntu:~/kernel/goldfish$ make goldfish_armv7_defconfig HOSTCC scripts/basic/fixdep scripts/basic/fixdep.c: In function ‘traps’: scripts/basic/fixdep.c:377: warning: dereferencing type-punned pointer will break strict-aliasing rules scripts/basic/fixdep.c:379: warning: dereferencing type-punned pointer will break strict-aliasing rules HOSTCC scripts/basic/docproc HOSTCC scripts/basic/hash HOSTCC scripts/kconfig/conf.o scripts/kconfig/conf.c: In function ‘conf_askvalue’: scripts/kconfig/conf.c:105: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result scripts/kconfig/conf.c: In function ‘conf_choice’: scripts/kconfig/conf.c:307: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result HOSTCC scripts/kconfig/kxgettext.o SHIPPED scripts/kconfig/zconf.tab.c SHIPPED scripts/kconfig/lex.zconf.c SHIPPED scripts/kconfig/zconf.hash.c HOSTCC scripts/kconfig/zconf.tab.o HOSTLD scripts/kconfig/conf arch/arm/configs/goldfish_defconfig:289:warning: override: FB_EARLYSUSPEND changes choice state # # configuration written to .config #
输入make或make -j4就可以编译了,带参数-j4,表示cpu为双核,速度快了很多,大概不到3分钟内核就编译完了。编译完成时可以看到:
GEN .version CHK include/linux/compile.h UPD include/linux/compile.h CC init/version.o LD init/built-in.o LD .tmp_vmlinux1 KSYM .tmp_kallsyms1.S AS .tmp_kallsyms1.o LD .tmp_vmlinux2 KSYM .tmp_kallsyms2.S AS .tmp_kallsyms2.o LD vmlinux SYSMAP System.map SYSMAP .tmp_System.map OBJCOPY arch/arm/boot/Image Kernel: arch/arm/boot/Image is ready AS arch/arm/boot/compressed/head.o GZIP arch/arm/boot/compressed/piggy.gz AS arch/arm/boot/compressed/piggy.o CC arch/arm/boot/compressed/misc.o LD arch/arm/boot/compressed/vmlinux OBJCOPY arch/arm/boot/zImage Kernel: arch/arm/boot/zImage is ready
生成的内核在/home/yan/kernel/goldfish/arch/arm/boot文件夹下。
4,用这个编译好的内核和编译源码产生的3个镜像安装至模拟器。
终端中输入:
yan@ubuntu:~/kernel/goldfish$ emulator -kernel /home/yan/kernel/goldfish/arch/arm/boot/zImage &
注意这个&号是让程序在后台运行,如果这样输入,会显示以下警告:
emulator: WARNING: system partition size adjusted to match image file (163 MB > 66 MB)
为此,启动命令更改为:
emulator -kernel /home/yan/kernel/goldfish/arch/arm/boot/zImage -partition-size 500 &
如果后面带&,模拟器启动后终端会自动释放,然后还可以接着输入命令:adb shell,然后cd /proc,然后cat version查看机器所用的内核版本。如果后面不带&,启动模拟器的终端就不会释放,用户也无法在此终端输入后续命令。这时可以新开一个终端,输入上面同样的命令查看内核版本信息。
其实可以在emulator命令时带个-shell参数,相当于在启动模拟器的终端输入adb shell命令了。因此,我推荐的启动命令是:
emulator -kernel /home/yan/kernel/goldfish/arch/arm/boot/zImage -partition-size 500 -shell
如果用系统自带的默认内核,cat version查看的结果是:
# cat version
Linux version 2.6.29-g327fbfb ([email protected]) (gcc version 4.4.3 (GCC) ) #10 Thu Oct 6 00:37:14 CEST 2011
如果用刚编译好的内核,查看的结果是:
# cat version
Linux version 2.6.29 (yan@ubuntu) (gcc version 4.4.3 (GCC) ) #2 Fri Jul 12 19:09:31 CST 2013
虽然都是2.6.29内核,但从后面的日期和linux版本不难看出,新编译的内核成功加载至模拟器了。
参考:
1,老罗:在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
2,http://blog.csdn.net/mr_president/article/details/7689084
欢迎android爱好者加群248217350 备注:yanzi
---------------------------------------- 本文系原创,转载请注明作者:yanzi1225627