如何从源码编译Android Emulator及相关组件?
Android Emulator (AOSP main Branch) Full Build & Custom Logging Guide
Prerequisites (Debian)
Install required dependencies for AOSP and emulator compilation:
sudo apt update && sudo apt install -y openjdk-17-jdk git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
Sync Complete AOSP Codebase
Initialize and sync the main branch, which includes all emulator-related repositories:
repo init -u https://android.googlesource.com/platform/manifest -b main repo sync -j$(nproc)
Key paths for emulator components:
- Emulator core:
external/qemu/- Device-side client components (OpenGL forwarding, HAL services):
frameworks/native/opengl/,hardware/google/emulator/
Component 1: Build Custom Goldfish Kernel
- Navigate to the Goldfish kernel directory:
cd kernel/goldfish
- Configure the kernel for x86_64 emulator:
ARCH=x86_64 make goldfish_x86_64_defconfig
- Modify kernel code to add custom logs (e.g., add
printkstatements in driver files), then compile:
ARCH=x86_64 make -j$(nproc)
- The compiled kernel image is at
arch/x86_64/boot/bzImage; specify it with the-kernelflag when launching the emulator.
Component 2 & 3: Build Device-side Images with Custom Logs
These components are part of the Android system image. Use the following steps:
- Return to the AOSP root, select the correct build target (replaces the outdated
sdk_phone_x86_64-userdebug):
lunch aosp_x86_64-userdebug
For ARM64 emulators, use
aosp_arm64-userdebuginstead.
- Add custom logs to device-side code:
- OpenGL forwarding: Edit files under
frameworks/native/opengl/libs/EGL/orframeworks/native/opengl/libs/GLESv2/, addALOGD/ALOGVstatements - Modem HAL services: Edit code in
hardware/google/emulator/modem/to add logs
- OpenGL forwarding: Edit files under
- Compile system images:
make -j$(nproc) systemimg vendorimg ramdiskimg
Compiled images are stored in out/target/product/generic_x86_64/.
Component 4: Build Custom Emulator Binary
Replace the prebuilt emulator binary with your own build:
- From AOSP root, compile the emulator:
make emulator -j$(nproc)
- Add custom logs to emulator code: Edit files in
external/qemu/(e.g., client request handling, rendering, input forwarding logic), then re-runmake emulator - The compiled binary is located at
out/host/linux-x86_64/bin/emulator
Launch Custom Emulator
Combine all components with this example command:
cd out/host/linux-x86_64/bin ./emulator -avd test_avd -kernel ../../../../kernel/goldfish/arch/x86_64/boot/bzImage -system ../../../../out/target/product/generic_x86_64/system.img -vendor ../../../../out/target/product/generic_x86_64/vendor.img -ramdisk ../../../../out/target/product/generic_x86_64/ramdisk.img -verbose
Use
-verboseto enable detailed logging for debugging your custom log statements.
Troubleshooting
- Missing emulator-compatible lunch targets: Ensure you synced the full main branch (partial syncs may omit required configs)
- Emulator build dependency errors: Verify all Debian prerequisites are installed, especially OpenGL-related libraries
内容的提问来源于stack exchange,提问作者Kyoungwon Kim
相关产品推荐
相关产品推荐

