文章目录
前言
由于国内长城防火墙的存在,没有VPN的话无法按照Google的官方下载方法下载源码(~现在因为疫情的原因就算有VPN也不敢用~),但是我们可以从清华大学的AOSP镜像下载各个版本的AOSP源码用于分析。
一、AOSP是什么?
AOSP是Android Open-Source Project的缩写,它跟Android的关系参见:AOSP (Android Open-Source Project) 跟 Android 是何关系?
二、从清华大学AOSP镜像下载
# 下载全部
repo init -u https://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-s-preview-1
只能参考这个清华的这个帮助文档,如果真按上面操作估计几天都未定下载的下来,所以下面我介绍一种纯粹使用git就能完成下载的,而且可以单独下载某个仓库,或者批量下载,或者全部下载所有仓库
1. 首先下载manifest仓库
什么是manifest仓库?就是使用repo init初始化后.repo目录里面的manifests git仓,这个仓库里面有各个分支的仓库清单,我们下载这个主要是为了checkout出所需的分支对应的仓库清单,然后使用清单去批量下载。
$ git clone https://mirrors.ustc.edu.cn/aosp/platform/manifest.git
2. 查看manifest仓库包含哪些分支
$ cd manifest
$ git branch -a|grep android-
remotes/origin/android-10.0.0_r1
remotes/origin/android-10.0.0_r10
remotes/origin/android-10.0.0_r11
remotes/origin/android-10.0.0_r12
remotes/origin/android-10.0.0_r13
remotes/origin/android-11.0.0_r1
remotes/origin/android-11.0.0_r10
remotes/origin/android-11.0.0_r11
remotes/origin/android-11.0.0_r12
remotes/origin/android-11.0.0_r13
remotes/origin/android-11.0.0_r14
remotes/origin/android-11.0.0_r15
remotes/origin/android-11.0.0_r16
remotes/origin/android-11.0.0_r17
remotes/origin/android-11.0.0_r18
remotes/origin/android-11.0.0_r19
remotes/origin/android-11.0.0_r2
remotes/origin/android-11.0.0_r20
remotes/origin/android-11.0.0_r21
remotes/origin/android-11.0.0_r25
remotes/origin/android-11.0.0_r3
3. checkout所需的分支清单文件
截止2020/12/11当前最新的分支是android-11.0.0_r25,下面以下载这个分支的代码为例进行介绍。
$ git checkout -b android-11.0.0_r25 remotes/origin/android-11.0.0_r25
Branch 'android-11.0.0_r25' set up to track remote branch 'android-11.0.0_r25' from 'origin'.
Switched to a new branch 'android-11.0.0_r25'
$ git branch
* android-11.0.0_r25
master
$ ls
GLOBAL-PREUPLOAD.cfg default.xml # default.xml就是我们所需的仓库清单文件
$ vi default.xml
<manifest>
3
4 <remote name="aosp"
5 fetch=".."
6 review="https://android-review.googlesource.com/" />
7 <default revision="refs/tags/android-11.0.0_r25"
8 remote="aosp"
9 sync-j="4" />
.......
可以看到这个清单文件里面仓库的分支是android-11.0.0_r25
4. 下载单仓
以wpa_supplicant8仓库为例,打开default.xml后,搜索到对应的project:
<project path="external/wpa_supplicant_8" name="platform/external/wpa_supplicant_8" groups="pdk" />
$ # git clone https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/project-name.git -b branch
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/external/wpa_supplicant_8.git -b android-11.0.0_r25
Cloning into 'wpa_supplicant_8'...
remote: Counting objects: 20859, done.
remote: Compressing objects: 100% (8278/8278), done.
remote: Total 20859 (delta 15200), reused 16770 (delta 12508)
Receiving objects: 100% (20859/20859), 11.95 MiB | 1.70 MiB/s, done.
Resolving deltas: 100% (15200/15200), done.
Note: checking out '217c18962a713d6b6d3ba4c069cf37763c526ea8'.
5. 批量下载
这里直接下载整个default.xml里面所有的仓库,使用一个python2.7脚本来解析xml里面每个project的name和path,然后将name对应的仓库下载到patch指定的目录即可。
#coding=utf-8
import xml.dom.minidom
import os
import sys,getopt
if __name__ == '__main__':
if len(sys.argv) < 2:
print "too few args, need 1 more at least"
print "try '%s -h' for more information" % sys.argv[0]
sys.exit()
opts, args = getopt.getopt(sys.argv[1:], "hd:m:b:")
top_dir = ''
manifest_file = ''
repo_branch = 'master'
for op, val in opts:
if op == '-d':
top_dir = val
elif op == '-m':
manifest_file = val
elif op == '-b':
repo_branch = val
elif op == '-h':
print "%s -d download_top_dir -m manifest.xml [-b branch]" % sys.argv[0]
sys.exit()
else:
print "try '%s -h' for more information" % sys.argv[0]
sys.exit()
if top_dir is None:
print "err: download top dir can't be null"
sys.exit();
if manifest_file is None:
print "err: manifest file can't be null"
sys.exit()
repo_prefix = "https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/"
repo_suffix = ".git"
top_dir = os.path.abspath(top_dir)
if not os.path.exists(top_dir):
os.makedirs(top_dir)
print "Downloading code to", top_dir
manifest_dom = xml.dom.minidom.parse(manifest_file)
for node in manifest_dom.getElementsByTagName('project'):
repo_dir = node.getAttribute('path')
repo_name = node.getAttribute('name')
os.chdir(top_dir)
if not os.path.exists(repo_dir):
os.makedirs(repo_dir)
cmd = ' '.join(['git', 'clone', repo_prefix + repo_name + repo_suffix, '-b', repo_branch, repo_dir])
print cmd
os.system(cmd)
将上面的内容保存为dowloader.py,然后使用这个脚本来下载整个default.xml,如果只需要下载几个project,可以将其他都删除.
下载命令:
$ python downloader.py -d aosp -m manifest/default.xml -b android-11.0.0_r25
Downloading code to /Users/xxx/aosp
git clone https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/build.git -b android-11.0.0_r25 build/make
Cloning into 'build/make'...
remote: Counting objects: 262869, done.
remote: Compressing objects: 100% (5379/5379), done.
remote: Total 262869 (delta 3327), reused 3494 (delta 1121)
......
如果只下载几个仓库很快就下载完了。
Note: checking out '34f62af4d6425087667b7eb3998fcf58d61a2fa6'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
Checking out files: 100% (7108/7108), done.
很多人可能奇怪为什么很多仓库checkout完之前会打印下面的detached HEAD,这是因为-b所对应的其实是tag并不是真正的每个仓库都有这个分支,tag对应的hash如第一行所示。
总结
以上就是今天要讲的内容,本文详细介绍了只使用git从清华大学AOSP镜像网站下载源码的方法,这个方法希望能帮到大家,觉得对你有帮助的话可以点个赞或关注一下,后面我会更新一系列AOSP Wi-Fi框架的源码流程分析。