一、下载Android源码
Android源码通过repo工具获取,编译Android源码需要设置相关的环境。(Android Source网站)
$repo init -u https://android.googlesource.com/platform/manifest -b master
如需签出 master 之外的其他分支,需要-b
指定分支。分支参考网站:源代码标记和 build
比如我们想获取android-12.1.0_r27
的源码,可以通过下面的命令。
$repo init -u https://android.googlesource.com/platform/manifest -b android-12.1.0_r27
$repo sync -j8
如果连接谷歌源有问题(谷歌一般都需要翻墙),也可以从国内镜像源下载源代码:
二、编译
模拟器镜像是通过qemu运行的。源码中定义的product为sdk_phone_xx(xx为arm,arm64,x86,x86_64等)。我们需要通过下面的命令设置环境,选择要编译的目标product并启动编译过程。
$source ./build/envsetup.sh
$lunch sdk_phone_x86
$make -j32
如果想构建 x86_64 位Android虚拟机运行镜像,则需针对 64 位目标运行 lunch
。编译完成后会生成镜像文件。通过命令emulator
可以直接启动模拟器。
$lunch sdk_phone_x86_64
$make -j32
[ 99% 36747/36947] Target empty super fs image: out/target/product/emulator_x86_64/super_empty.img
2022-11-01 15:43:06 - build_super_image.py - INFO : Building super image from info dict...
2022-11-01 15:43:06 - build_super_image.py - INFO : Done writing image out/target/product/emulator_x86_64/super_empty.img
[ 99% 36901/36947] Target cache fs image: out/target/product/emulator_x86_64/cache.img
WARNING: out/target/product/emulator_x86_64/cache.img approaching size limit (16777216 now; limit 16777216)
[ 99% 36902/36947] Target userdata fs image: out/target/product/emulator_x86_64/userdata.img
WARNING: out/target/product/emulator_x86_64/userdata.img approaching size limit (576716800 now; limit 576716800)
ls99% 36939/36947] Create product-qemu.img
[ 99% 36944/36947] Target super fs image for debug: out/target/product/emulator_x86_64/super.img
2022-11-01 15:45:08 - build_super_image.py - INFO : Building super image from info dict...
2022-11-01 15:45:11 - build_super_image.py - INFO : Done writing image out/target/product/emulator_x86_64/super.img
[100% 36947/36947] Create system-qemu.img now
removing out/target/product/emulator_x86_64/system-qemu.img.qcow2
out/host/linux-x86/bin/sgdisk --clear out/target/product/emulator_x86_64/system-qemu.img
#### build completed successfully (25:38 (mm:ss)) ####
三、共享 AVD 系统映像以供他人配合使用 Android Studio
要想通过emulator命令启动我们编译好的系统镜像,需要将镜像打包并部署。
通过下面的步骤打包AVD 系统映像。
安卓12开始
$make -j8 emu_img_zip
// 目标文件在out/tarproduct/emulator_x86_64/sdk-repo-linux-system-images-eng.[username].zip
安卓12以下
$make -j8 sdk sdk_repo
// 目标文件在out/host/linux-x86/sdk/sdk_phone_x86/sdk-repo-linux-system-images-eng.[username].zip
// 目标文件repo-sys-img.xml 用来部署镜像源
运行完成后会生成压缩包文件sdk-repo-linux-system-images-eng.[username].zip
,其中包含模拟器启动系统锁需要的所有系统镜像。解压该文件,其中有个文件夹,如x86_64,arm64.
可以启动http server(如:python -m http.server 8888),使用repo-sys-img.xml部署镜像源,此处不详细介绍。
四、运行编译生成的镜像
方法1
通过Android Studio创建虚拟设备.
Android Studio-> tools-> avd manager->create virtual device lets say we created Pixel_2_API_29
我创建一个名字为aaa
的虚拟机设备,通过下面的命令启动
$emulator -list-avds
aaa
$emulator -avd aaa -sysdir x86_64/
或
$emulator @aaa -sysdir x86_64/
第一次启动通过-wipe-data选项启动模拟器或在 AVD 管理器中擦除相关数据。
$emulator -avd aaa -sysdir x86_64/ -wipe-data
或者
$emulator @aaa -sysdir x86_64/ -wipe-data
运行后效果如下,接下来就可以基于模拟器调试Android的源码了。
方法2:
将镜像解压到androidsdk/system-images目录。从其他镜像目录(如android-sdk\system-images\android-33\default\x86_64)移动一个package.xml过去。并修改一下。
这样就可以使用镜像直接创建AVD了。
五、内核
AVD系统目录下文件如下:
$ls
NOTICE.txt VerifiedBootParams.textproto advancedFeatures.ini build.prop data encryptionkey.img kernel-ranchu ramdisk.img source.properties system.img userdata.img vendor.img
文件 | 说明 | 用于指定其他文件的选项 |
---|---|---|
kernel-qemu 或 kernel-ranchu | AVD 的二进制内核映像。kernel-ranchu 是最新版本的 QEMU 2 模拟器。 | -kernel |
system.img | 系统映像的只读初始版本;具体而言,包含与 API 级别和变体对应的系统库和数据的分区。 | -system |
ramdisk.img | 启动分区映像。这是 system.img(在装载系统映像之前最初由内核加载)的一个子集。它通常只包含一些二进制文件和初始化脚本。 | -ramdisk |
userdata.img | 数据分区的初始版本,在模拟系统中显示为 data/,包含 AVD 的所有可写入数据。当您创建新 AVD 或使用 ‑wipe-data 选项时,模拟器会使用此文件。如需了解详情,请参阅以下部分中的 userdata-qemu.img 文件说明。 | -initdata -init-data |
编译可用的Android模拟器ranchu内核 - 腾讯云开发者社区-腾讯云
https://blog.csdn.net/gs344937933/article/details/117491559
六、构建AVD内核
mkdir goldfish-kernel-54
cd goldfish-kernel-54
repo init -u https://android.googlesource.com/kernel/manifest -b
common-android11-5.4
repo sync
BUILD_CONFIG=common/build.config.gki.x86_64 build/build.sh
BUILD_CONFIG=common-modules/virtual-device/build.config.goldfish.x86_64
build/build.sh
ls -l ./out/android11-5.4/dist/
https://source.android.google.cn/docs/devices/automotive/start/avd?hl=zh-cn
六、定位问题
无法启动虚拟机
-verbose
选项会输出模拟器启动时的详细日志。
使用下面命令查看模拟器详细信息
$ emulator -avd bbb -sysdir sdk-repo-linux-system-images-eng.XX/x86_64/ -verbose
Failed to create Vulkan instance.
七、lunch选择product
如果想在AndroidStudio上运 avd加载自己编译的镜像
请参考:android studio avd加载自己编译的镜像_android avd 运行android镜像-CSDN博客