Mac 中下载阅读 Android 源码

1. Mac 中下载 Android 源码

准备工作:创建区分大小写的 APFS 宗卷

国内使用 Android Source 官方的下载方式有点慢,所以通常建议使用国内的 AOSP 镜像,一般是中科大的镜像或者清华大学的镜像,本文使用的是清华大学的镜像。

先下载 repo 工具:

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

然后有两种下载方式,分别适合不同场景。

1.1 下载最新版本的源码

如果需要下载最新版本的源码,建议使用镜像中的每月初始化包。先下载初始化包,其中包含了.repo目录,然后解压再repo sync 一次就完成了。

推荐使用 axel 工具下载,速度快,支持断点续传。

1
2
3
4
5
6
7
8
9
10
11
# 下载初始化包
axel -a -n 3 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
# 解压得到 aosp 目录
tar xvf aosp-latest.tar
cd aosp
# 同步一次
repo sync
# 或 repo sync -l 仅 checkout .repo 中的代码

1.2 下载具体某版本的源码

如果需要下载具体某版本的源码,使用官方的方式,不过下载源替换成清华大学镜像的地址。具体 Android 版本见列表

1
2
3
4
5
6
7
8
9
# 建立工作目录
mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY
# 初始化仓库
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-4.0.1_r1 --depth=1
# 只同步当前分支代码
repo sync -c

这种方式下载下来的代码会小很多,推荐这种方式。

2. 导入 Android 源码到 Android Studio 中阅读

Mac 下无法使用 Source Insight,所以我是使用 Android Studio 来阅读的,Android 源码中已经提供了导入到 Eclipse 和 Android Studio 的配置,需要使用development/tools/idegen.sh工具。

因为我只想阅读 Android 源码,不需要编译,所以使用其他人编译好的idegen.jar

1
2
3
4
5
# 复制到指定目录
mv idegen.jar aosp/out/host/darwin-x86/framework/
# 在默认终端中运行 idegen.sh,因为 sh 只支持 bash
development/tools/idegen/idegen.sh

然后在 aosp 目录可以看到android.iprandroid.iml文件,接下来编辑android.iml文件,把不需要导入的项目注释掉,节省导入时间。

最后在 Android Studio 中打开android.ipr即可。

END
Johnny Shieh wechat
我的公众号,不只有技术,还有咖啡和彩蛋!