Ubuntu 22.04编译AOSP Android 13完整指南

发布时间:2026/7/19 5:54:31
Ubuntu 22.04编译AOSP Android 13完整指南 1. 环境准备与系统配置在Ubuntu 22.04上编译AOSP Android 13需要先完成基础环境搭建。我推荐使用物理机而非虚拟机进行编译因为Android源码编译对硬件资源要求较高。以下是经过实测的配置方案1.1 硬件需求建议CPU至少8核推荐16核以上内存32GB起步64GB更佳存储500GB SSD源码编译产物约需300GB网络稳定连接需要下载约50GB源码注意低于此配置可能导致编译时间过长超过10小时或内存不足错误1.2 系统初始化设置首先更新系统并安装基础工具包sudo apt update sudo apt upgrade -y sudo apt install -y git curl python3 python-is-python3设置交换分区32GB物理内存以下必须配置sudo fallocate -l 8G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile echo /swapfile none swap sw 0 0 | sudo tee -a /etc/fstab1.3 依赖包安装Android官方推荐的依赖包列表需要调整适配Ubuntu 22.04sudo apt install -y git-core gnupg flex bison build-essential zip curl zlib1g-dev \ libc6-dev-i386 libncurses5 x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev \ libxml2-utils xsltproc unzip fontconfig python2.7特别处理Python2.7兼容性问题sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 22. 源码获取与同步2.1 配置Repo工具创建bin目录并配置环境变量mkdir -p ~/bin curl https://storage.googleapis.com/git-repo-downloads/repo ~/bin/repo chmod ax ~/bin/repo echo export PATH~/bin:$PATH ~/.bashrc source ~/.bashrc2.2 初始化代码仓库建议使用清华镜像源加速下载mkdir ~/aosp cd ~/aosp repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r41配置git身份信息git config --global user.name Your Name git config --global user.email youexample.com2.3 同步源码树使用多线程加速同步repo sync -j$(nproc) --current-branch --no-tags常见同步问题处理网络中断后恢复同步repo sync --fail-fast遇到冲突时repo forall -c git reset --hard3. 编译环境配置3.1 JDK版本管理Android 13需要JDK 11sudo apt install -y openjdk-11-jdk sudo update-alternatives --config java # 选择JDK 11验证Java版本java -version # 应显示11.x.x3.2 环境变量设置编辑~/.bashrc添加export USE_CCACHE1 export CCACHE_EXEC/usr/bin/ccache export CCACHE_DIR~/.ccache ccache -M 50G # 设置缓存大小 source ~/.bashrc3.3 设备驱动准备对于Pixel设备以Pixel 6为例从Google开发者网站下载对应驱动解压到AOSP根目录执行sh脚本提取二进制文件4. 完整编译流程4.1 初始化编译环境source build/envsetup.sh lunch aosp_arm64-eng # 根据目标设备选择常见lunch选项aosp_x86_64-engx86模拟器aosp_oriole-userdebugPixel 6aosp_raven-userdebugPixel 6 Pro4.2 开始编译使用多线程编译建议不超过CPU核心数的1.5倍m -j$(($(nproc)*3/2)) 21 | tee build.log监控编译状态watch -n 10 tail -n 20 build.log4.3 编译产物处理编译完成后产物位置system.imgout/target/product/[device]/system.imgramdisk.imgout/target/product/[device]/ramdisk.imguserdata.imgout/target/product/[device]/userdata.img打包完整镜像make snod # 快速重建system.img5. 常见问题排查5.1 内存不足错误症状g: fatal error: Killed signal terminated program cc1plus解决方案增加swap空间减少编译线程m -j4使用ccache减少重复编译5.2 Python版本冲突症状File build/make/core/main.mk, line: Python 2.7 is required解决方案sudo update-alternatives --config python # 选择Python 2.75.3 网络问题导致同步失败建议配置git代理git config --global http.proxy http://proxy.example.com:8080或者改用SSH协议repo init -u git://mirrors.tuna.tsinghua.edu.cn/aosp/platform/manifest6. 高级技巧与优化6.1 增量编译加速仅编译特定模块m module-name快速重建system.imgmake snod使用ccache统计ccache -s6.2 自定义编译选项修改build/core/main.mk# 添加编译参数 COMMON_GLOBAL_CFLAGS -DDEBUG16.3 多设备支持同时编译多个设备lunch aosp_arm64-eng m -j8 lunch aosp_x86_64-eng m -j87. 刷机与测试7.1 刷入Pixel设备解锁bootloaderfastboot flashing unlock刷入镜像fastboot flashall -w7.2 模拟器启动emulator -show-kernel -verbose调试参数-debug启用调试信息-gpu host使用主机GPU加速-no-snapshot禁用快照8. 后续开发建议使用Android Studio导入源码make idegen development/tools/idegen/idegen.sh常用调试命令adb logcat -b all # 查看完整日志 adb shell dumpsys # 系统服务信息代码搜索技巧cgrep 关键字 # 全代码库搜索 jgrep 关键字 # Java文件搜索