准备工作: 创建区分大小写的 APFS 宗卷
国内使用 Android Source 官方的下载方式有点慢, 所以通常建议使用国内的 AOSP 镜像, 一般是 中科大的镜像 https://lug.ustc.edu.cn/wiki/mirrors/help/aosp 或者 清华大学的镜像 https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ , 本文使用的是清华大学的镜像.
先下载 repo 工具:
- 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 工具下载, 速度快, 支持断点续传.
- # 下载初始化包
- axel -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 版本见 列表 https://source.android.com/source/build-numbers.html#source-code-tags-and-builds
- # 建立工作目录
- 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 https://github.com/dxjia/common-tools/tree/master/read_aosp_through_intellij .
- # 复制到指定目录
- mv idegen.jar aosp/out/host/darwin-x86/framework/
- # 在默认终端中运行 idegen.sh, 因为 sh 只支持 bash
- development/tools/idegen/idegen.sh
然后在 aosp 目录可以看到 android.ipr 和 android.iml 文件, 接下来编辑 android.iml 文件, 把不需要导入的项目注释掉, 节省导入时间.
最后在 Android Studio 中打开 android.ipr 即可.
来源: http://www.tuicool.com/articles/F7bQr2B