参考: https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
目前下载安卓系统完整源码只能使用repo工具, 且只能在Linux/Mac下执行. Windows下的理论上也可以下载, 但无法编译, 参考: https://blog.csdn.net/freekiteyu/article/details/70939672
前提准备:
安装好git和python, 准备磁盘空间100G+(如果是完整源码大概是600G+, 安卓11单个分支需要110G空间), 时间一般至少需要3个小时+.
首先下载并安装repo工具 (https://mirrors.tuna.tsinghua.edu.cn/help/git-repo/)
安装 Repo
Repo 是一款工具,可让您在 Android 环境中更轻松地使用 Git。要详细了解 Repo,请参阅开发部分。
要安装 Repo,请执行以下操作:
-
确保主目录下有一个 bin/ 目录,并且该目录包含在路径中:
mkdir ~/bin
PATH=~/bin:$PATH
-
下载 Repo 工具,并确保它可执行:
curl
https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
> ~/bin/repo
chmod a+x ~/bin/repo
更新
repo的运行过程中会尝试访问官方的git源更新自己,如果想使用tuna的镜像源进行更新,可以将如下内容复制到你的~/.bashrc
里 (如果是最新的MacOS或者用的zsh请写入 .zshrc里)
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
并重启终端模拟器。
如果您需要完整的代码(所有版本的), 请参考
使用每月更新的初始化包
由于首次同步需要下载约 95GB 数据,过程中任何网络故障都可能造成同步失败,我们强烈建议您使用初始化包进行初始化。
下载 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,下载完成后记得根据 checksum.txt 的内容校验一下。
由于所有代码都是从隐藏的
.repo
目录中 checkout 出来的,所以我们只保留了.repo
目录,下载后解压 再repo sync
一遍即可得到完整的目录。使用方法如下:
wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包 tar xf aosp-latest.tar cd AOSP # 解压得到的 AOSP 工程目录 # 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录 repo sync # 正常同步一遍即可得到完整目录 # 或 repo sync -l 仅checkout代码
此后,每次只需运行
repo sync
即可保持同步。 我们强烈建议您保持每天同步,并尽量选择凌晨等低峰时间
这里我们使用正常的步骤, 请先查阅 安卓源码版本清单(转载待查), 然后选择一个分支, 例如android-11.0.0_r45
传统初始化方法
建立工作目录:
mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY
初始化仓库
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest
如果提示无法连接到 gerrit.googlesource.com,请参照git-repo的帮助页面的更新一节。
如果需要某个特定的 Android 版本(列表):(建议方案)
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-11.0.0_r45
同步源码树(以后只需执行这条命令来同步), 此过程超级慢且任何错误都会中断, 例如网络故障或者磁盘空间不足:
repo sync
中间同步失败了怎么办?
由于前期磁盘不足, 最后虽然恢复了空间, 但是也导致repo sync的时候反复提示失败:
error: Unable to fully sync the tree.
error: Checking out local projects failed.
Failing repos:
external/swiftshader
Try re-running with "-j1 --fail-fast" to exit at the first error.
Aborting
error: prebuilts/clang/host/darwin-x86/: platform/prebuilts/clang/host/darwin-x86 checkout 78c8a5102c5185a16a3589b6e31d083712bf99fb
error: Cannot checkout platform/prebuilts/clang/host/darwin-x86
Checking out: 100% (786/786), done in 29.824s
error: Unable to fully sync the tree.
error: Checking out local projects failed.
Failing repos:
hardware/libhardware
frameworks/ml
external/swiftshader
frameworks/av
prebuilts/remoteexecution-client
prebuilts/clang/host/darwin-x86
解决办法:
删除失败的目录, 然后重新调用 repo sync:
rm -r hardware/libhardware
rm -r frameworks/ml
rm -r frameworks/av
rm -r prebuilts/remoteexecution-client prebuilts/clang/host/darwin-x86
强制更新单个项目, 可以用:
repo sync hardware/libhardware --force-sync