AOSP 源码整编单编

《AOSP 源码下载》完成后,就可以开编了。

整编

整编,顾名思义就是编译整个 Android 源码,最终 out 目录会生成几个重要的镜像文件,其中有 system.img、userdata.img、ramdisk.img 等,这些是可以刷机的。

初始化编译环境

项目目录,打开终端,输入命令:

source build/envsetup.sh

选择编译目标

命令:

lunch

输出:

You're building on LinuxLunch menu... pick a combo:     1. aosp_arm-eng     2. aosp_arm64-eng     3. aosp_blueline-userdebug     4. aosp_car_arm-userdebug     5. aosp_car_arm64-userdebug     6. aosp_car_x86-userdebug     7. aosp_car_x86_64-userdebug     8. aosp_cf_x86_64_auto-userdebug     9. aosp_cf_x86_64_phone-userdebug     10. aosp_cf_x86_64_tablet-userdebug     11. aosp_cf_x86_64_tablet_3g-userdebug     12. aosp_cf_x86_64_tv-userdebug     13. aosp_cf_x86_64_wear-userdebug     14. aosp_cf_x86_auto-userdebug     15. aosp_cf_x86_phone-userdebug     16. aosp_cf_x86_tablet-userdebug     17. aosp_cf_x86_tablet_3g-userdebug     18. aosp_cf_x86_tv-userdebug     19. aosp_cf_x86_wear-userdebug     20. aosp_crosshatch-userdebug     21. aosp_marlin-userdebug     22. aosp_sailfish-userdebug     23. aosp_taimen-userdebug     24. aosp_walleye-userdebug     25. aosp_walleye_test-userdebug     26. aosp_x86-eng     27. aosp_x86_64-eng     28. beagle_x15-userdebug     29. hikey-userdebug     30. hikey64_only-userdebug     31. hikey960-userdebug     32. hikey960_tv-userdebug     33. hikey_tv-userdebug     34. m_e_arm-userdebug     35. mini_emulator_arm64-userdebug     36. mini_emulator_x86-userdebug     37. mini_emulator_x86_64-userdebug     38. poplar-eng     39. poplar-user     40. poplar-userdebug     41. uml-userdebugWhich would you like? [aosp_arm-eng] 26

编译目标格式说明

编译目标的格式:BUILD-BUILDTYPE,比如上面的 poplar-eng 的 BUILD 是 poplar,BUILDTYPE 是 eng.

什么是 BUILD

BUILD 指的是特定功能的组合的特定名称,即表示编译出的镜像可以运行在什么环境。其中 aosp(Android Open Source Project)代表 Android 开源项目;arm 表示系统是运行在 arm 架构的处理器上,arm64 则是指 64 位 arm 架构处理器,x86 则表示 x86 架构的处理器,更多设备代码和编译目标参考官方文档。

什么是 BUILDTYPE

BUILDTYPE 则指的是编译类型,通常有三种:

  • user:用来正式发布到市场的版本,权限受限,如没有 root 权限,不能 dedug 等。

  • userdebug:在user版本的基础上开放了 root 权限和 debug 权限。

  • eng:代表 engineer,开发工程师的版本,拥有最大的权限(root等),具有额外调试工具的开发配置。

如果没有谷歌手机设备,可以选择 arm 或者 x86,我选择了 aosp_x86-eng,编译完后运行模拟器看看,因此这里选择序号 26。

开始编译

通过 make 指令进行代码编译:

make -j8

其中 -jN 参数表示处理并行任务,通常使用的任务数 N 介于编译时所用计算机上硬件线程数的 1-2 倍之间。

查看计算机上的核心数:

cat /proc/cpuinfo | grep processorprocessor    : 0processor    : 1processor    : 2processor    : 3

可看到创建的虚拟机 CPU 核心共有 4 个,那么要实现最快的编译速度,可以使用介于 make -j4 到 make -j8 之间的命令。

不出意外,当看到:#### build completed successfully (03:55:24 (hh:mm:ss)) ####就整编成功了。

运行模拟器

整编完,网上很多都是说直接运行命令:emulator,但是我报以下错误了:

emulator: WARNING: Couldn't find crash service executable /media/ubuntu/disk/Project/AOSP9/prebuilts/android-emulator/linux-x86_64/emulator64-crash-serviceemulator: WARNING: system partition size adjusted to match image file (1080 MB > 800 MB)queryCoreProfileSupport: swap interval not foundfailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablegetGLES2ExtensionString: Could not make GLES 2.x context current!

第一个警告不管,第二个警告可以 emulator 加参数 -partition-size 2048解决,也可以不管,最后一个折腾了很久,可以加参数-gpu off,完整运行模拟器的命令:emulator -gpu off -partition-size 2048,这时应该又会报:

emulator: WARNING: Couldn't find crash service executable /media/ubuntu/disk/Project/AOSP9/prebuilts/android-emulator/linux-x86_64/emulator64-crash-serviceemulator: WARNING: system partition size adjusted to match image file (1338 MB > 800 MB)emulator: ERROR: Running multiple emulators with the same AVD is an experimental feature.Please use -read-only flag to enable this feature.

只能重启一下才能解决。

如果编译完成后启动模拟器时卡在黑屏,可以尝试编译其他的!

emulator 还有很多参数,可以用 emulator -help 查看,参数如下:

-sysdir  为模拟器在目录中搜索系统硬盘镜像-system  为模拟器从文件中读取初始化系统镜像-datadir  设置用户数据写入的目录-kernel  为模拟器设置使用指定的模拟器内核-ramdisk  设置内存RAM 镜像文件(默认为/ramdisk.img)-image  废弃,使用-system  替代-init-data  设置初始化数据镜像(默认为/userdata.img)-initdata  和"-init-data "使用方法一致-data  设置数据镜像(默认为/userdata-qemu.img)-partition-size  system/data 分区容量大小(MB)-cache  设置模拟器缓存分区镜像(默认为零时文件)-no-cache 禁用缓存分区-nocache 与"-no-cache"使用方法相同-sdcard  指定模拟器SDCard 镜像文件(默认为/sdcard.img)-wipe-data 清除并重置用户数据镜像(从initdata 拷贝)-avd  指定模拟器使用Android 虚拟设备-skindir  设置模拟器皮肤在目录中搜索皮肤(默认为/skins 目录)-skin  选择使用给定的皮肤-no-skin 不适用任何模拟器皮肤-noskin 使用方法与"-no-skin"相同-memory  物理RAM 内存大小(MB)-netspeed  设置最大网络下载、上传速度-netdelay  网络时延模拟-netfast 禁用网络形态-tarce  代码配置可用-show-kernel 显示内核信息-shell 在当前终端中使用根Shell 命令-no-jni Dalvik 运行时禁用JNI 检测-nojni 使用方法与"-no-jni"相同-logcat  输出给定tag 的Logcat 信息-no-audio 禁用音频支持-noaudio 与"-no-audio"用法相同-audio  使用指定的音频backend-audio-in  使用指定的输入音频backend-audoi-out  使用指定的输出音频backend-raw-keys 禁用Unicode 键盘翻转图-radio 重定向无线模式接口到个性化设备-port  设置控制台使用的TCP 端口-ports , 设置控制台使用的TCP 端口和ADB 调试桥使用的TCP 端口-onion  在屏幕上层使用覆盖PNG 图片-onion-alpha <%age> 指定上层皮肤半透明度-onion-rotation 0|1|2|3 指定上层皮肤旋转-scale  调节模拟器窗口尺寸(三种:1.0-3.0、dpi、auto)-dpi-device  设置设备的resolution (dpi 单位) (默认165)-http-proxy  通过一个HTTP 或HTTPS 代理来创建TCP 连接-timezone  使用给定的时区,而不是主机默认的-dns-server  在模拟系统上使用给定的DNS 服务-cpu-delay  调节CUP 模拟-no-boot-anim 禁用动画来快速启动-no-window 禁用图形化窗口显示-version 显示模拟器版本号-report-console  向远程socket 报告控制台端口-gps  重定向GPS 导航到个性化设备-keyset  指定按键设置文件名-shell-serial  根shell 的个性化设备-old-system 支持旧版本(pre 1.4)系统镜像-tcpdump  把网络数据包捕获到文件中-bootchart  bootcharting 可用-qemu args.... 向qemu 传递参数-qemu -h 显示qemu 帮助-verbose 和"-debug-init"相同-debug  可用、禁用调试信息-debug- 使指定的调试信息可用-debug-no- 禁用指定的调试信息-help 打印出该帮助文档-help-

补充

模拟器运行需要四个文件,分别是:

  • Linux Kernel

  • system.img

  • userdata.img

  • ramdisk.img

上面我 lunch 命令时选择的是 aosp_x86-eng,因此 linux 默认使用的 AOSP/prebuilds/qemu-kernel/x86/kernel-qemu 下的 kernel-qemu,而其他文件则是使用的 AOSP/out/target/product/generic_x86 目录下的 system.img、userdata.img、ramdisk.img。

单编

单编就是编译某个模块,比如 Setting,会在 out 目录对应的产品有 Settings.apk,例如:out/target/product/generic_x86/system/priv-app/Settings/Settings.apk,这是可以直接安装的。

1、source build/envsetup.sh

2、lunch

3、选择单编版本序号

4、编译

4.1、mmm

mmm packages/apps/Settings/

编译指定目录下的模块,但不编译它所依赖的其它模块。

4.2、mm

先进入目录:

cd packages/apps/Settings/

再编译

 mm

编译当前目录下的模块,它和 mmm 一样,不编译依赖模块。

4.3、mma

编译当前目录下的模块及其依赖项。

4.4、mmma [module_path]

编译指定路径下所有模块,且包含依赖

4.5、make

make: 不带任何参数则是编译整个系统

make MediaProvider

单个模块编译,会把该模块及其依赖的其他模块一起编译,会搜索整个源代码来定位 MediaProvider 模块所使用的 Android.mk 文件,还要判断该模块依赖的其他模块是否有修改。

make snod

如果我们修改代码,想看效果,怎么办?

1、可以在编译完成后,借助 adb install -r apk路径直接将生成的 apk 文件安装到设备上。

2、可以编译好完成后,再使用make snod,重新生成 system.img,再运行模拟器也可。

你可能感兴趣的:(AOSP 源码整编单编)