发布于 ,更新于 

Ubuntu 22.04 下编译 Android 13 系统源码

首先,采用 Ubuntu 22.04 的环境编译 Android 系统源代码需要额外安装一些软件包。

下面是 Google 官方【文档】要求安装的软件包:

Ubuntu 18.04 以上的版本安装以下软件包:

1
sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig

Ubuntu 14.04 的版本安装以下软件包:

1
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev libxml2-utils xsltproc unzip

而 Ubuntu 22.04 下编译还可能遇到找不到 libncurses.so.5 等奇怪问题,则可能需要额外再安装一下包:

1
2
3
4
5
6
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libncurses5:i386
sudo apt-get install libncurses5-dev libncurses5
#默认情况在linux编译系统 会开启odex的, 解决方法:编译前关闭dex2oat
export WITH_DEXPREOPT=false

事实上,还要安装 Python 3,这是必不可少的。

1
2
sudo apt-get install python3
sudo apt-get install python-is-python3

不同版本的 Android 系统在编译的时候,对具体的 Java JDK 的版本有特殊的要求:

Android 版本 JDK 版本要求
Android 9.0 以上 源码自带 JDK
Android 7.0 ~ Android 8.0 OpenJDK 8
Android 5.0 ~ Android 6.0 OpenJDK 7
Android 2.3 ~ Android 4.4 Oracle JDK 6
Android 1.5 ~ Android 2.2 Oracle JDK 5

由于 Android 13 系统源码自带了自己需要的 JDK 版本,所以在 Ubuntu 22.04 下不需要安装指定的 JDK 了。

未进行 git 配置的先对 git 客户端工具进行配置:

1
2
git config --global user.name 'gradliang'
git config --global user.email 'gradliang@xxxxxx.com'

下载 repo 工具:

1
2
3
4
mkdir ~/bin
cd ~/bin
curl -o repo https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
chmod 755 repo

使用 vi 在 ~/.bashrc 文件的最后添加一行:

1
export PATH=~/bin:$PATH

exit 退出,然后再重新进入 shell, repo 命令就可以直接使用了。

用下面命令将 repo 的源直接指定为清华大学的源,避免需要跑到海外去下载,耗费大量时间,还被挡住访问不了:

1
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

初始化 repo 仓库:( -b 后面指定分支)

1
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-13.0.0_r41

然后下载整个系统的源码:( -c 只同步下载选定分支,-j 是指定开多个线程)

1
repo sync -c -j8

运行 source 命令加载编译环境,并选择配置 aosp_arm64-eng :

1
2
source build/envsetup.sh
lunch aosp_arm64-eng

开始编译: ( -j 指定多个线程编译,根据自身机器配置情况)

1
make -j8