AOSP 介绍

AOSP 是 Android 开放源码项目,通俗点就是整个 Android 的源码。有了 AOSP 我们就可以在源码的层面去了解整个 Android 系统。甚至可以自己基于 AOSP 去定制自己系统,这也是众多 ROM 的直接或间接起源。

虽然我们不一定会去做 ROM ,但对于开发来说,了解 Android 系统的底层原理还是非常有必要的,这也是成为高级工程师的必经之路。源码里面有非常多的东西值得我们去学习,是一座宝藏,接下来我们就要把这个宝藏下载下来。

Repo 工具

目前市面上最流行的版本控制管理工具是 GitAOSP 也是用 Git 来管理的,不过 AOSP 中有几百个 Git 仓库管理起来非常不方便,光下载就要几百次,想想都可怕。

为了管理 AOSP 这么庞大的代码,谷歌开发了 Repo 的工具专门用来管理 AOSP 代码。 Ropo 本身是用 Python 写的,是对 Git 的一次封装,方便使用,我们就不要每个仓库都去操作一次。

要想使用 Repo 需要先在 Docker 中安装,由于众所周知的原因,我们使用国内清华大学的镜像来加速下载,或者可以使用中国科学技术大学AOSP

1
2
3
mkdir ~/bin
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo

创建一个挂载镜像磁盘

Mac 上的文件名默认是不区分大小写的,我们首先要创建一个支持大小写的镜像文件,用来存放下载的 AOSP 。这样做的好处是当容器被删除的时候,下载的内容不会跟着一起被删除。

打开 Disk Utility ,按下 ⌘ + N 创建一个,记得要选择大小写敏感的格式。如果你需要编译,最好要有 300G,在 Android 10 上。不需要编译给 150G 就够了。

构建容器并启动

Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
FROM ubuntu

ENV PATH="/root/bin:${PATH}"
ENV REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

RUN apt update && apt install -y git curl python openjdk-8-jdk git-core gnupg flex bison gperf build-essential zip 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 && \
    rm -rf /var/cahce/apt && \
    rm -rf /var/lib/apt/lists && \
    mkdir ~/bin && curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo && chmod a+x ~/bin/repo && mkdir ~/aosp

VOLUME ~/aosp

WORKDIR ~/aosp

COPY entrypoint.sh /root/entrypoint.sh

ENTRYPOINT ["/root/entrypoint.sh"]

ENTRYPOINT

entrypoint 是个 shell 脚本,用来在启动容器时执行命令用的,在这里我们只要初始化 repo 并执行 sync 就会开始下载。

1
2
3
4
5
6
#!/bin/bash
cd ~/aosp
echo "start sync"
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest && repo sync

exec "$@"

启动容器并开始下载

准备好 Dockerfileentrypoint 后,我们开始构建镜像 docker build -t aosp . 执行构建需要一定的时间,等待构建完成。构建完成后,我们可以通过下面指令启动容器 docker run -v /Volumes/Android10:/root/aosp --name aosp aosp sync 在下载的过程中,我经常会碰到容器意外退出。可以通过 docker start aosp 来重启容器。

成功

如果你看到如下所示,表示下载完成了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Checking out project platform/test/vts-testcase/hal-trace
Checking out project platform/test/vts-testcase/kernel
Checking out project platform/test/vts-testcase/performance
Syncing work tree:  98% (557/568)Checking out project platform/test/vts-testcase/security
Checking out project platform/test/vts-testcase/vndk
Checking out project toolchain/binutils
Checking out project platform/tools/apksig
Checking out project platform/tools/external/fat32lib
Checking out project platform/tools/external/gradle
Syncing work tree:  99% (563/568)Checking out project platform/tools/loganalysis
Checking out project platform/tools/repohooks
Checking out project platform/tools/test/connectivity
Checking out project platform/tools/tradefederation/contrib
Checking out project platform/tools/tradefederation
Syncing work tree: 100% (568/568), done.

碰到的错误

entrypoint.sh permission denied

1
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"/root/entrypoint.sh\": permission denied": unknown.

这个问题是在宿主机上没有给执行权限,在宿主机上执行下面指令 chmod a+x entrypoint.sh

error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function.

1
2
3
4
error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function.
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

这个错误是由于 git 的缓存设置的不够大

1
2
git config --global http.sslVerify false
git config --global http.postBuffer 1073741824

如果设置的太大了会包下面的错误

1
fatal: Out of memory, malloc failed (tried to allocate 20000000000 bytes)

index-pack died of signal 952

1
git config --global pack.windowMemory "32m"

Server does not provide clone.bundle

1
2
curl: (22) The requested URL returned error: 404
Server does not provide clone.bundle; ignoring.

如果你用的清华的源,会碰到这个问题,这是由于清华的源不支持 git 协议导致的,忽略就好,不影响下载。

总结

Docker 中下载 AOSP 对于没有 Linux 的小伙伴来说是一个不错的选择,不需要去安装虚拟机,只需要启动一个容器就可以了。

但是现实往往没那么容易,在使用的过程中会发现,容器莫名其妙就退出了,需要不断的重启容器来确保下载完成。

如果你需要编译的话还是建议用虚拟机来进行,用容器就非常不方便了。

最后还是建议使用虚拟机来下载 AOSP ,这样不用一直去观察容器的状态,虚拟机下载的体验会比使用 Docker 好很多,也更节省时间。

参考