我们完成AOSP源码下载之后,就可以准备源码进行编译了。但编译之前,首先要对编译环境进行初始化工作。
在这个过程中,主要是指定编译的类型和目标设备的型号。
Android的编译类型主要有eng、userdebug和user三种,而支持的目标设备型号则是不确定的,它们由当前的源码配置情况所决定。为了确定源码支持的所有目标设备型号,Android编译系统在初始化的过程中,需要在特定的目录中加载特定的配置文件。
如果之前执行过编译工作,或者编译出错了,可以执行清除命令:
$ make clobber
打开一个终端(bash),cd到源码根目录,并且将build/envsetup.sh加载到该终端中:
$ source build/envsetup.sh 或 $ . ./build/envsetup.sh
including device/generic/car/vendorsetup.sh including device/generic/mini-emulator-arm64/vendorsetup.sh including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh including device/generic/mini-emulator-mips/vendorsetup.sh including device/generic/mini-emulator-mips64/vendorsetup.sh including device/generic/mini-emulator-x86/vendorsetup.sh including device/generic/mini-emulator-x86_64/vendorsetup.sh including device/generic/uml/vendorsetup.sh including device/google/crosshatch/vendorsetup.sh including device/google/cuttlefish/vendorsetup.sh including device/google/marlin/vendorsetup.sh including device/google/muskie/vendorsetup.sh including device/google/taimen/vendorsetup.sh including device/linaro/hikey/vendorsetup.sh including sdk/bash_completion/adb.bash
我们来看命令的输出,文件build/envsetup.sh在加载的过程中又会加载一些其他文件。
-
在device目录中加载那些名称为vendorsetup.sh的文件。
-
在sdk/bash_completion目录下的adb.bash文件也会加载到当前终端来。
它是用来实现adb命令的bash completion功能的。也就是说,加载了该文件之后,我们在运行adb相关的命令的时候,通过按tab键就可以帮助我们自动完成命令的输入。
执行命令lunch,选择编译的目标:
$ lunch You're building on Darwin Lunch menu... pick a combo: 1. aosp_arm-eng 2. aosp_arm64-eng 3. aosp_mips-eng 4. aosp_mips64-eng 5. aosp_x86-eng 6. aosp_x86_64-eng 7. aosp_car_arm-userdebug 8. aosp_car_arm64-userdebug 9. aosp_car_x86-userdebug 10. aosp_car_x86_64-userdebug 11. mini_emulator_arm64-userdebug 12. m_e_arm-userdebug 13. m_e_mips-userdebug 14. m_e_mips64-eng 15. mini_emulator_x86-userdebug 16. mini_emulator_x86_64-userdebug 17. uml-userdebug 18. aosp_crosshatch-userdebug 19. aosp_blueline-userdebug 20. aosp_cf_x86_auto-userdebug 21. aosp_cf_x86_phone-userdebug 22. aosp_cf_x86_tablet-userdebug 23. aosp_cf_x86_tablet_3g-userdebug 24. aosp_cf_x86_tv-userdebug 25. aosp_cf_x86_wear-userdebug 26. aosp_cf_x86_64_auto-userdebug 27. aosp_cf_x86_64_phone-userdebug 28. aosp_cf_x86_64_tablet-userdebug 29. aosp_cf_x86_64_tablet_3g-userdebug 30. aosp_cf_x86_64_tv-userdebug 31. aosp_cf_x86_64_wear-userdebug 32. cf_x86_auto-userdebug 33. cf_x86_phone-userdebug 34. cf_x86_tablet-userdebug 35. cf_x86_tablet_3g-userdebug 36. cf_x86_tv-userdebug 37. cf_x86_wear-userdebug 38. cf_x86_64_phone-userdebug 39. cf_x86_64_tablet-userdebug 40. cf_x86_64_tablet_3g-userdebug 41. cf_x86_64_tv-userdebug 42. cf_x86_64_wear-userdebug 43. aosp_marlin-userdebug 44. aosp_marlin_svelte-userdebug 45. aosp_sailfish-userdebug 46. aosp_walleye-userdebug 47. aosp_walleye_test-userdebug 48. aosp_taimen-userdebug 49. hikey-userdebug 50. hikey64_only-userdebug 51. hikey960-userdebug
lunch命令输出了一个Lunch菜单,该菜单列出了当前Android源码支持的所有设备型号及其编译类型。
编译的类型:
- user: limited access; suited for production(有限的访问权限,一般用于发布版)。
- eng:具有开发配置,并且有额外的调试工具(注:工程师模式engineer)。
- userdebug: 这个和user类似,但是可以获取root权限,并且能够调试。
当我们选定了一个Lunch菜单项序号(1-51)之后,按回车键,就可以完成Android编译环境的初始化过程。
注:lunch命令也可以直接这么用 $ lunch aosp_arm-eng
Android编译环境初始化完成之后,获得了以下三样东西:
使用make命令开始整个系统的编译:
make -j8
这里的-j参数后面的数字是编译需要的线程数,建议电脑的CPU数量的1~2倍来设置。
然后就是漫长的等待过程了……
我们也可以用m/mm/mmm/make命令编译源代码。
当然,这要求每一个模块都有一个Android.mk文件。Android.mk实际上是一个Makefile脚本,用来描述模块编译信息。Android编译系统通过整合Android.mk文件完成编译过程。
m、mm和mmm命令也分别是由定义在build/envsetup.sh文件中的函数m、mm和mmm提供的,而这三个函数又都是通过make命令来对源代码进行编译的。
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory, but not their dependencies.
- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
To limit the modules being built use the syntax: mmm dir/:target1,target2. - mma: Builds all of the modules in the current directory, and their dependencies.
- mmma: Builds all of the modules in the supplied directories, and their dependencies.
事实上,命令m就是对make命令的简单封装,并且是用来对整个Android源代码进行编译,而命令mm和mmm都是通过make命令来对Android源码中的指定模块进行编译。
m的实现:
function m() { local T=$(gettop) if [ "$T" ]; then _wrap_build $T/build/soong/soong_ui.bash --make-mode $@ else echo "Couldn't locate the top of the tree. Try setting TOP." return 1 fi }
当在Android源码中定义的各个模块都编译好之后,我们还需要将编译得到的文件打包成相应的镜像文件,例如system.img、boot.img和recorvery.img等,这样我们才可以将这些镜像烧录到目标设备去运行。
系统编译完成之后,我们可以通过使用模拟器来运行,或者使用真机进行刷机。
$ source build/envsetup.sh $ lunch //(选择刚才设置的目标版本,例如如果之前我们选择1,那就是aosp_arm-eng) $ emulator //模拟器启动
$ adb reboot bootloader
$ fastboot flashall -w //这个 -w 是为了wipes the /data partition擦除/data分区
**PS:更多精彩内容,请查看 --> 《AOSP 专栏》
**PS:更多精彩内容,请查看 --> 《AOSP 专栏》
**PS:更多精彩内容,请查看 --> 《AOSP 专栏》
- 海报