> 文章列表 > AOSP编译速通指南

AOSP编译速通指南

AOSP编译速通指南

最近有编译AOSP系统的需求,于是学习着如何编译了AOSP源码,时效性为文章发布日
使用的机器环境如下:

  • CPU:i7-1170K
  • vRAM:32G
  • vOS:Ubuntu 20.04
  • vDisk:500G

强烈建议在编译AOSP的时候提供充足的磁盘和内存空间,不然很有可能编译失败
安装所需的软件包

sudo apt-get install 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
sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev
sudo apt-get install git-core gnupg flex bison gperf build-essential
sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib
sudo apt-get install libc6-dev-i386
sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
sudo apt-get install lib32z-dev ccache
sudo apt-get install libssl-dev

下载REPO工具

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

配置git用户名和邮箱:

git config --global user.name [Your Name]
git config --global user.email [you@example.com]

使用每月更新的初始化包:
由于首次同步需要下载约 60GB 数据,过程中任何网络故障都可能造成同步失败,强烈建议使用初始化包进行初始化,初始化包的下载地址:

https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar

你可以在Windows上下载好,然后再拖到虚拟机里面进行编译,也可以使用如下的方法:

curl -OC - https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包
tar xf aosp-latest.tar
cd AOSP   # 解压得到的 AOSP 工程目录
# 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录

由于所有代码都是从隐藏的.repo目录中checkout出来的,所以我们只保留了.repo目录,下载后解压再 repo sync一遍即可得到完整的目录
切换AOSP分支
设置好你要同步的分支,这里以AOSP 12为例子:

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-12.0.0_r8

然后开始使用sync同步,sync的时候并发数不宜太高,否则会出现 503 错误,即-j后面的数字不能太大,建议选择4

repo sync -c -j4

开始编译
通过脚本初始化环境:

source build/envsetup.sh

设置构建目标,根据提示选择构建目标,为了方便的启动模拟器我建议使用sdk_phone_x86_64:

lunch sdk_phone_x86_64

开始编译:

make -j8

启动模拟器
当编译完成后,这样启动模拟器:

source build/envsetup.sh
lunch sdk_phone_x86_64
emulator

常见问题

0x1 执行repo init命令时报错:/usr/bin/env: “python”

如果你的Ubuntu系统是20及以上版本,就可能出现该错误

sudo ln -s $(which python3) /usr/bin/python

参考资料

  • https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
  • https://blog.csdn.net/yubo_725/article/details/117470199
  • https://source.android.com/docs/setup/start/initializing?hl=zh-cn
  • https://www.cnblogs.com/sandeepin/p/ubuntu20-aosp-latest.html