说明:以下所有编译命令均在源码根目录执行
一、加载编译环境
source build/envsetup.sh
二、编译
2.1 编译完整Android
Android 全编译的完整操作命令有两种:
- 第一种:
source build/envsetup.sh
lunch
<input number>【在编号列表中选择合适的产品编号】
make -j 32
1.1 第二种:
source build/envsetup.sh
lunch <product_name_type> 【以pixel5为例,它的产品名称与类型是 aosp_redfin-userdebug】
make -j 32
2.2 编译 image
make systemimage
make systemextimage
2.3 编译模块
具体请通过帮助命令查询:
- m help
- hmm
2.4 使用 Ninja 编译模块
alias ninja=./prebuilts/build-tools/linux-x86/bin/ninja
ninja -f out/combined-<XXX>.ninja <module_name>
for example (Bluetooth.apk on pixel5):
alias ninja=./prebuilts/build-tools/linux-x86/bin/ninja
ninja -f out/combined-aosp_redfin.ninja Bluetooth
三、刷机(烧录)
3.0 前提条件
以 pixel5 为例:
- 将 pixel5 通过线连接到电脑;
- 打开手机的开发者模式;
- 启动 USB 调试模式。
3.1 解锁设备
- 打开开发者模式
- 进入开发者模式首页,打开“OEM解锁”开关
- 命令行执行 adb reboot bootloader
- 设备重启至屏幕显示内容之后,执行 fastboot flashing unlock
- 通过音量加、减按键,选择 unlock 选项;再通过电源按键确定选择,手机自行重启
- 执行 adb disable-verity
- 执行 adb reboot ,设备重启
- 执行 adb root
- 执行 adb remount
执行 1-5 后,设备即可执行全刷;
再执行 6-9 后,设备即可执行模块的更新(后文的刷模块)。
3.2 全刷
- 生成刷机文件 images.zip
zip images.zip out/target/product/redfin/*.img out/target/product/redfin/android-info.txt
- 刷机命令
需要在image所在的路径执行
fastboot flash bootloader bootloader.img
fastboot reboot-bootloader
fastboot flash radio radio.img
fastboot reboot-bootloader
fastboot -w update images.zip
3.3 刷 image
3.3.1 刷 image 的模式
3.3.1.1 bootloader 模式
进入方式:
xxx reboot bootloader
例如:
adb reboot bootloader
fastboot reboot bootloader
3.3.1.2 fastbootd 模式
进入方式:
xxx reboot fastboot
例如:
adb reboot fastboot
fastboot reboot fastboot
3.3.2 刷 image 举例
由于存在两种刷机模式,因此首先需要明确该 img 需要在哪个模式下进行烧录,然后执行烧录命令。
- 例如烧录 bootloader.img (bootloader):
adb reboot bootloader
fastboot flash bootloader bootloader.img
- 例如烧录 system.img (fastbootd)
adb reboot fastboot
fastboot flash system system.img
3.4 刷 模块(so、jar、apk、apex 等)
3.4.1 刷模块 举例
- 刷 设置apk(以 pixel5 redfin 为例)
adb push out\target\product\redfin\system_ext\priv-app\Settings\Settings.apk /system_ext/priv-app/Settings/
- 刷 tethering apex
adb push out\target\product\redfin\system\apex\com.android.tethering.apex /system/apex/
wifi 模块的 jar文件位置(class文件打包,还未打成 dex 包,用于调试)
out/soong/.intermediates/packages/modules/Wifi/framework/framework-wifi/android_common_apex30/javac/framework-wifi.jar
- 刷 framework.jar
system/framework/framework.jar
system/framework/boot-*.vdex
system/framework/arm/*
system/framework/arm64/*
3.4.2 刷模块的脚本 举例
- init.bat(进入源码根目录的脚本)
Y:
cd pixel5
- remount.bat(挂载设备的脚本)
adb root
adb remount
- push_settings_apk.bat(push 目标产物的脚本)
call remount.bat
call init.bat
adb push out\target\product\redfin\system_ext\priv-app\Settings\Settings.apk /system_ext/priv-app/Settings/
四、刷机 Log
4.1 全刷(注意:中途进入 fastbootd 模式)
执行的命令
adb reboot bootloader
fastboot flash bootloader bootloader.img
fastboot reboot-bootloader
fastboot flash radio radio.img
fastboot reboot-bootloader
fastboot -w update images.zip
由于中途进入了fastbootd模式,因此需要注意:
哪些 image 在bootloader模式下刷;
哪些 image 在fastbootd模式下刷。
注意:执行该命令之前,处在 bootloader 模式
PS E:\device\pixel\localimg> fastboot -w update images.zip
\--------------------------------------------
Bootloader Version...: r3-0.3-6776358
Baseband Version.....: g7250-00016-200826-B-6790987
Serial Number........: 0C111FDD4001NF
--------------------------------------------
extracting android-info.txt (0 MB) to RAM...
Checking 'product' OKAY [ 0.066s]
Setting current slot to 'a' OKAY [ 0.079s]
extracting boot.img (96 MB) to disk... took 0.258s
archive does not contain 'boot.sig'
Sending 'boot_a' (98304 KB) OKAY [ 2.434s]
Writing 'boot_a' OKAY [ 0.505s]
extracting dtbo.img (16 MB) to disk... took 0.031s
archive does not contain 'dtbo.sig'
Sending 'dtbo_a' (16384 KB) OKAY [ 0.492s]
Writing 'dtbo_a' OKAY [ 0.175s]
archive does not contain 'dt.img'
archive does not contain 'recovery.img'
extracting vbmeta.img (0 MB) to disk... took 0.001s
archive does not contain 'vbmeta.sig'
Sending 'vbmeta_a' (8 KB) OKAY [ 0.137s]
Writing 'vbmeta_a' OKAY [ 0.070s]
extracting vbmeta_system.img (0 MB) to disk... took 0.000s
archive does not contain 'vbmeta_system.sig'
Sending 'vbmeta_system_a' (4 KB) OKAY [ 0.137s]
Writing 'vbmeta_system_a' OKAY [ 0.076s]
extracting vendor_boot.img (96 MB) to disk... took 0.192s
archive does not contain 'vendor_boot.sig'
Sending 'vendor_boot_a' (98304 KB) OKAY [ 2.441s]
Writing 'vendor_boot_a' OKAY [ 0.366s]
extracting super_empty.img (0 MB) to disk... took 0.000s
Rebooting into fastboot OKAY [ 0.062s]
< waiting for any device >
Sending 'super' (4 KB) OKAY [ 0.001s]
Updating super partition OKAY [ 0.008s]
Resizing 'product_a' OKAY [ 0.005s]
Resizing 'system_a' OKAY [ 0.006s]
Resizing 'system_ext_a' OKAY [ 0.005s]
Resizing 'system_b' OKAY [ 0.006s]
Resizing 'vendor_a' OKAY [ 0.005s]
Resizing 'vendor_b' OKAY [ 0.004s]
archive does not contain 'boot_other.img'
archive does not contain 'odm.img'
archive does not contain 'odm_dlkm.img'
extracting product.img (228 MB) to disk... took 1.100s
archive does not contain 'product.sig'
Resizing 'product_a' OKAY [ 0.005s]
Sending 'product_a' (233900 KB) OKAY [ 5.923s]
Writing 'product_a' OKAY [ 2.001s]
extracting system.img (837 MB) to disk... took 3.565s
archive does not contain 'system.sig'
Resizing 'system_a' OKAY [ 0.007s]
Sending sparse 'system_a' 1/4 (262140 KB) OKAY [ 6.754s]
Writing 'system_a' OKAY [ 0.911s]
Sending sparse 'system_a' 2/4 (262140 KB) OKAY [ 6.727s]
Writing 'system_a' OKAY [ 1.012s]
Sending sparse 'system_a' 3/4 (262140 KB) OKAY [ 6.686s]
Writing 'system_a' OKAY [ 0.992s]
Sending sparse 'system_a' 4/4 (70804 KB) OKAY [ 1.805s]
Writing 'system_a' OKAY [ 0.296s]
extracting system_ext.img (117 MB) to disk... took 0.450s
archive does not contain 'system_ext.sig'
Resizing 'system_ext_a' OKAY [ 0.006s]
Sending 'system_ext_a' (120496 KB) OKAY [ 3.043s]
Writing 'system_ext_a' OKAY [ 0.478s]
extracting system_other.img (23 MB) to disk... took 0.111s
archive does not contain 'system.sig'
Resizing 'system_b' OKAY [ 0.006s]
Sending 'system_b' (23836 KB) OKAY [ 0.609s]
Writing 'system_b' OKAY [ 0.147s]
extracting vendor.img (673 MB) to disk... took 2.744s
archive does not contain 'vendor.sig'
Resizing 'vendor_a' OKAY [ 0.006s]
Sending sparse 'vendor_a' 1/3 (262140 KB) OKAY [ 6.794s]
Writing 'vendor_a' OKAY [ 0.924s]
Sending sparse 'vendor_a' 2/3 (262140 KB) OKAY [ 6.702s]
Writing 'vendor_a' OKAY [ 0.999s]
Sending sparse 'vendor_a' 3/3 (165656 KB) OKAY [ 4.250s]
Writing 'vendor_a' OKAY [ 0.631s]
archive does not contain 'vendor_dlkm.img'
archive does not contain 'vendor_other.img'
Erasing 'userdata' OKAY [ 7.315s]
Erase successful, but not automatically formatting.
File system type raw not supported.
Erasing 'metadata' OKAY [ 0.013s]
Erase successful, but not automatically formatting.
File system type raw not supported.
Rebooting OKAY [ 0.001s]
Finished. Total time: 100.540s
五、分区与镜像
Android分区介绍 by Goole
引导加载程序(bootloader)示例流程
镜像文件名 | 用途 |
---|---|
recovery.img | |
boot.img | 其中包含 kernel 和 ramdisk ,Android 13 将 ramdisk 剥离 。 |
init_boot.img | Android13,仅包含从 boot.img 中剥离的 ramdisk。 |
dtb.img | Device Tree Blob,设备树文件 |
dtbo.img | Device Tree Blob Overlay,设备树叠加文件 |
metadata.img | |
vbmeta.img | Verified Boot Android Verified Boot (AVB)是 Verified Boot 的一种具体实现。 |
动态分区适用于用户空间分区,从用户空间分区独立出来的分区如果不属于用户空间分区,那么也不属于动态分区 | |
odm.img | |
product.img | |
system.img | 包含 init 可执行文件(second stage init),还有很多其他内容未描述 |
system_ext.img | |
vendor.img | |
super.img |
动态分区镜像的集合,但具体包含了哪些镜像,需根据配置查询。 以 db845c 为例: device/linaro/dragonboard/BoardConfigCommon.mk # Super partition TARGET_USE_DYNAMIC_PARTITIONS := true BOARD_BUILD_SUPER_IMAGE_BY_DEFAULT := true BOARD_SUPER_PARTITION_GROUPS := db_dynamic_partitions BOARD_DB_DYNAMIC_PARTITIONS_PARTITION_LIST := system vendor system_ext product 该开发板的 super.img 包含了四个镜像: system、system_ext、product、vendor |
radio.img | |
userdata.img | |
最终刷写时可能不需要,这里单列用于介绍其内容 | |
ramdisk.img | 包含 adb shell 进入之后看到的(部分)一级目录结构 (dev、metadata、mnt、proc、second_stage_resources、sys、system) 和 init 可执行文件(first stage init,其他的 image包含了 ramdisk image 才会有 该init,否则没有) |