1. 环境准备与虚拟机配置在Ubuntu 18.04虚拟机上编译Android源码需要特别注意几个关键点。首先VMware Workstation Pro 15.x是较适合的版本与Ubuntu 18.04的兼容性最好。创建虚拟机时建议分配至少200GB的虚拟磁盘实际编译后占用约150GB采用单个文件存储模式而非拆分多个文件这样可以避免后期磁盘空间不足的问题。内存分配有讲究主机32GB内存可分配20-24GB给虚拟机16GB主机则分配12-14GB。我曾尝试在8GB主机上分配6GB给虚拟机编译过程频繁因内存不足中断。处理器设置中务必开启虚拟化Intel VT-x/EPT选项这对后续运行Android模拟器至关重要。重要提示虚拟机磁盘格式必须选择SCSI而非SATA否则在长时间编译过程中可能遇到I/O性能瓶颈。实测SCSI接口的持续写入速度比SATA快30%左右。文件系统配置建议/ 分区50GB ext4swap分区内存的1.5倍如分配16GB内存则swap设24GB/home分区剩余所有空间这种分配方式避免了编译中途因根分区空间不足导致的失败。我曾在默认分区方案下编译到90%时因根分区耗尽而前功尽弃。2. 系统环境与依赖安装Ubuntu 18.04默认的Python版本是2.7和3.6这与Android源码编译要求完美匹配。首先执行基础更新sudo apt update sudo apt upgrade -y sudo apt install -y git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip关键依赖说明lib32z-dev处理32位兼容库缺少会导致zlib相关错误libxml2-utils解析XML配置文件必备ccache可加速后续编译建议配置缓存大小prebuilts/misc/linux-x86/ccache/ccache -M 50GPython环境配置有个坑点虽然AOSP官方文档说支持Python 3但实际编译过程中仍有部分脚本依赖Python 2。最佳实践是sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2这样可以通过sudo update-alternatives --config python灵活切换。编译时建议使用Python 2.7因为仍有部分repo工具依赖它。3. 源码下载与repo工具配置清华大学镜像站是国内下载AOSP源码的最佳选择。首先配置git身份信息git config --global user.name Your Name git config --global user.email youexample.com然后设置repo工具mkdir ~/bin PATH~/bin:$PATH curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo ~/bin/repo chmod ax ~/bin/repo使用初始化包可以大幅节省时间约节省20小时wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar tar xf aosp-latest.tar cd aosp repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-9.0.0_r46 repo sync -c --no-tags实测数据通过初始化包增量同步完整获取android-9.0.0_r46代码仅需3小时而传统方式需要24小时。同步过程中常见问题处理遇到fatal: 过早的文件结束符错误时执行repo sync -f -j1出现error: Exited sync due to fetch errors时先删除对应出错的git目录再重试4. 编译过程与排错指南编译前的关键配置source build/envsetup.sh lunch aosp_x86-eng # 选择模拟器镜像开始编译根据CPU核心数调整-j参数make -j$(nproc) 21 | tee build.log典型问题及解决方案问题1内存不足导致ninja失败症状ninja: build stopped: subcommand failed. [ 0% 3/91935] //external/llvm...c] ninja failed with: exit status 137解决方法增加swap空间sudo fallocate -l 16G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile减少并行编译任务数make -j4 # 改为4线程问题2Java版本冲突症状You are attempting to build with the incorrect version of java.解决方法sudo update-alternatives --config java sudo update-alternatives --config javac选择OpenJDK 8Ubuntu 18.04默认安装的是OpenJDK 11需要额外安装8sudo apt install openjdk-8-jdk问题3文件系统大小写敏感问题症状Case mismatch: .../file.h and .../FILE.H解决方法mv aosp aosp_temp mkdir aosp mv aosp_temp/* aosp/ rmdir aosp_temp确保所有源码都在纯小写路径中。编译成功后的输出位于out/target/product/generic_x86/包含system.img、ramdisk.img等重要镜像文件。5. 模拟器运行与调试技巧启动编译好的模拟器emulator -show-kernel -verbose高级参数说明-no-snapshot-load禁用快照加载避免缓存问题-gpu host使用主机GPU加速-partition-size 2048增大系统分区大小性能优化建议在VMware设置中启用3D加速虚拟机设置 显示器 加速3D图形调整Ubuntu显示设置sudo nano /etc/default/grub修改为GRUB_CMDLINE_LINUX_DEFAULTquiet splash nomodeset然后执行sudo update-grub常见模拟器问题处理黑屏无响应尝试emulator -no-audio -no-window报错Failed to open lib64/qt/plugins/platforms/libqxcb.sosudo apt install libxcb-xinerama06. 增量编译与模块编译技巧完成全量编译后日常开发中可以使用增量编译节省时间m # 相当于make mm # 编译当前目录模块 mmm path/to/module # 编译指定模块查看完整模块列表make modules快速编译framework并推送到模拟器make framework adb sync adb shell stop adb shell start我常用的开发循环修改代码如frameworks/base编译单个模块mmm frameworks/base/快速部署adb root adb remount adb push out/target/product/generic_x86/system/framework/framework.jar /system/framework/ adb shell chmod 644 /system/framework/framework.jar adb shell stop adb shell start这种工作流可以将修改验证周期从30分钟缩短到2分钟。7. 性能监控与编译优化使用ccache加速后续编译export USE_CCACHE1 export CCACHE_DIR/path/to/ccache prebuilts/misc/linux-x86/ccache/ccache -M 50G监控编译状态watch -n 1 ps -ef | grep make | grep -v grep关键性能指标内存使用free -h磁盘IOiostat -x 1CPU温度sensors当发现系统资源吃紧时可以降低编译并行度make -j4限制ccache内存ccache -M 20G暂停其他高负载进程编译时间参考i7-9750H/32GB RAM/SSD全量编译约3小时增量编译5-30分钟单个模块编译10秒-5分钟8. 虚拟机专属优化技巧针对VMware环境的特别优化启用虚拟化嵌套需主机BIOS支持vmx.allowNested TRUE vhv.enable TRUE添加到.vmx配置文件磁盘性能优化使用独立持久磁盘禁用内存页面修剪sudo sysctl vm.swappiness10网络配置建议使用NAT模式而非桥接禁用IPv6sudo sysctl -w net.ipv6.conf.all.disable_ipv61 sudo sysctl -w net.ipv6.conf.default.disable_ipv61定期清理虚拟机快照它们会显著降低磁盘性能我在i9-10900K/64GB RAM的主机上通过上述优化将全量编译时间从4.5小时缩短到2.8小时。最关键的是磁盘和内存配置——编译过程会产生数百万个小文件随机读写性能至关重要。