
問:什么是AOSP?
答:"Android Open-Source Project"的縮寫。中文意為"Android 開放源代碼項目",簡單描述就是Android手機系統(tǒng)的源代碼。那些亂七八糟的UI定制、功能開發(fā),高通、MTK、展訊等平臺都是基于這個AOSP的二次開發(fā)、三次開發(fā)。另外,很多講解Android源代碼的書,或多或少都需要結(jié)合AOSP來閱讀。
按照Google的官方教程下載編譯整個Android源碼也是一個勞民傷財?shù)木薮蠊こ?。首先得有l(wèi)inux環(huán)境,內(nèi)存要大,硬盤也要大,cpu不能差,這里不多介紹了。網(wǎng)絡(luò)上教程資源很多,可以搜索查看。
本文要講得是在不需要編譯、不需要獲取所有源碼,只是想看看Android的部分源碼的前提下,介紹一種簡便獲取AOSP的方法。
本文核心:用python腳本,從manifest文件中,一個一個獲取到最新的git倉庫里的代碼。
本文是根據(jù)Windows 環(huán)境下載 Android 源碼整理而來。
1. 熟悉鏡像服務(wù)器
下面介紹了替換Google服務(wù)器的一種鏡像下載方式。
2. 安裝Python、git環(huán)境
- 建議安裝Python3
- 配置Git代理
當獲取時出現(xiàn)下面類似Maximum (20) redirects followed的錯誤,請嘗試配置代理。(在公司需要代理才能訪問外網(wǎng),訪問內(nèi)網(wǎng)時需要去除代理)
Cloning into 'base'...
fatal: unable to access 'http://mirrors.tuna.tsinghua.edu.cn/aosp/platform/frameworks/base.git/': Maximum (20) redirects followed
設(shè)置代理:
git config --global http.proxy http://proxy.abc.com:80
git config --global https.proxy https://proxy.abc.com:80
取消代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
3. 開始表演
1. 下載manifest清單,并找到自己想要的版本
打開git命令行,輸入:
git clone https://aosp.tuna.tsinghua.edu.cn/platform/manifest.git
$ git clone https://aosp.tuna.tsinghua.edu.cn/platform/manifest.git
Cloning into 'manifest'...
remote: Counting objects: 6742, done.
remote: Compressing objects: 100% (4462/4462), done.
remote: Total 6742 (delta 2433), reused 3160 (delta 1013)
Receiving objects: 100% (6742/6742), 2.65 MiB | 2.14 MiB/s, done.
Resolving deltas: 100% (2433/2433), done.
Checking connectivity... done.
里面除了 git 的配置目錄外,clone 下來了一個 default.xml 文件。
可以git branch -a先查看一下版本信息
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/adt_23.0.3
remotes/origin/afw-test-harness-1.5
remotes/origin/afw-test-harness-2.1
remotes/origin/afw-test-harness-marshmallow-dev
remotes/origin/afw-test-harness-nougat-dev
remotes/origin/android-1.6_r1
找到自己想要的那個版本,checkout出來,下面我checkout的是android-5.1.1_r9,一下子從master分支,切到了android-5.1.1_r9分支。
money@qiandao MINGW32 /f/Gerrit/manifest (master)
$ git checkout remotes/origin/android-5.1.1_r9
Note: checking out 'remotes/origin/android-5.1.1_r9'.
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>
HEAD is now at 7e381b0... Manifest for Android 5.1.1 release 9
money@qiandao MINGW32 /f/Gerrit/manifest ((android-5.1.1_r9))
2. 配置Python腳本
需要調(diào)整的內(nèi)容:
- 下載源碼的目錄:
rootdir = "F:/Android/source/android-5.1.1_r9" - Git安裝路徑:默認情況下是
C:/Program Files (x86)/Git/bin/git.exe,如果安裝到別處,自己調(diào)整,注意斜杠正反。 -
manifest文件夾下的defult.xml文件路徑:F:/Gerrit/manifest/default.xml - 下面的代碼我只下載了
framework的,其他的可以根據(jù)根目錄自己定制,如果需要下載全部,可以刪除那個if條件。
import xml.dom.minidom
import os
from subprocess import call
#downloaded source path
rootdir = "F:/Android/source/android-5.1.1_r9"
#git program path
git = "C:/Program Files (x86)/Git/bin/git.exe"
dom = xml.dom.minidom.parse("F:/Gerrit/manifest/default.xml")
root = dom.documentElement
prefix = git + " clone https://aosp.tuna.tsinghua.edu.cn/"
suffix = ".git"
if not os.path.exists(rootdir):
os.mkdir(rootdir)
for node in root.getElementsByTagName("project"):
os.chdir(rootdir)
mPath = node.getAttribute("path")
d = mPath
last = d.rfind("/")
if last != -1:
d = rootdir + "/" + d[:last]
if not os.path.exists(d):
os.makedirs(d)
os.chdir(d)
# print(d)
if(mPath.startswith("framework")):
print(mPath)
cmd = prefix + node.getAttribute("name") + suffix
call(cmd)
運行之后:
F:\myGitHub\myPython3>python f:/Gerrit/manifest/getAOSP.py
F:/Android/source/android-5.1.1_r9/frameworks
Cloning into 'av'...
remote: Counting objects: 191197, done.
remote: Compressing objects: 100% (59552/59552), done.
remote: Total 191197 (delta 137158), reused 182217 (delta 129436)
Receiving objects: 100% (191197/191197), 42.13 MiB | 2.42 MiB/s, done.
Resolving deltas: 100% (137158/137158), done.
Checking connectivity... done.
Checking out files: 100% (2900/2900), done.
F:/Android/source/android-5.1.1_r9/frameworks
Cloning into 'base'...
remote: Counting objects: 2559919, done.
remote: Compressing objects: 100% (562974/562974), done.
remote: Total 2559919 (delta 1652150), reused 2555075 (delta 1647444)
Receiving objects: 100% (2559919/2559919), 2.39 GiB | 1.99 MiB/s, done.
Resolving deltas: 100% (1652150/1652150), done.
Checking connectivity... done.
Checking out files: 100% (27359/27359), done.
后面就可以閱讀源碼了。。。用Source insight?或者其他更好的?我也不清楚了,我也是剛學。
不過我聽我的小伙伴說,用linux下編譯出來的某個東西,可以導入到Android Studio或Eclipse里查看或者調(diào)試framework層的源碼。
后面有時間會整理推薦一些大牛的博客。