好了 google爸爸給我們帶來了很大的方便,話不多說,直接去
https://mp.weixin.qq.com/s/DFXsHBOg0Fw2SU1h6hK4Lw
我們非常高興的爲各位開發者們介紹一個 Android 開源項目 (AOSP) 的代碼搜索工具:
https://cs.android.com
。。。。。。。。。2019.12.18.。。。。。。。。分割線。。。。。。。。
吐槽網上各種亂七八糟的指導,試了半天也未能成功,還要裝ubuntu 各種噁心。下面我親測了一種方法ok的,適用於windows同學。
爲了進一步的深入Android源碼的學習,我們需要去下載android源碼到我們的AS裏面編譯。
當然如果你不想下載下來,那麼也可以在線看
https://www.androidos.net.cn/sourcecode
準備環境
安裝 git
安裝 Python
自備梯子
硬盤剩餘容量最好大於 100G
預計需要8個小時
- 安裝 git
官網:https://git-scm.com/downloads/
圖形化工具:https://tortoisegit.org/
什麼?不會安裝 git,那還看什麼源碼!自行百度!
- 安裝 Python
官網:https://www.python.org/downloads/
安裝參考:http://jingyan.baidu.com/article/c910274be14d64cd361d2dd8.html
- 自備梯子
沒有梯子?那就使用清華源:https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
下載源碼
- 打開 Git Bash,用 git 克隆源代碼倉庫
git clone https://android.googlesource.com/platform/manifest.git
//沒有梯子使用清華源
git clone https://aosp.tuna.tsinghua.edu.cn/platform/manifest.git
這時 D:/android_source_code/ 目錄下會出現一個 manifest 目錄,進入此目錄,裏面除了 git 的配置目錄外,clone 下來了一個 default.xml 文件。
- 切換到想要的源碼版本分支
去這裏 https://source.android.com/source/build-numbers.html#source-code-tags-and-builds
找到想要的版本分支,並複製。
(裏面可能沒有最新的版本,比如我下載的時候就沒有9.0版本,這時候可以通過下面的命令行去搜索最新的版本)
cd manifest
//沒有梯子,使用 git branch -a 查看所有分支,找到想要的分支
git branch -a
git checkout android-6.0.1_r79 //這裏以 6.0 最後一個版本下載
- 使用 Python 執行腳本進行源代碼下載
將下面的代碼複製,創建文件 python_download.py,並保存。
import xml.dom.minidom
import os
from subprocess import call
# 1. 修改爲源碼要保存的路徑
rootdir = "F:/androidSource/Android_9.0"
# 2. 設置 git 安裝的路徑
git = "D:/Program Files/Git/cmd/git.exe"
# 3. 修改爲第一步中 manifest 中 default.xml 保存的路徑
dom = xml.dom.minidom.parse("F:/androidSource/manifest/default.xml")
root = dom.documentElement
#prefix = git + " clone https://android.googlesource.com/"
# 4. 沒有梯子使用清華源下載
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)
d = node.getAttribute("path")
last = d.rfind("/")
if last != -1:
d = rootdir + "/" + d[:last]
if not os.path.exists(d):
os.makedirs(d)
os.chdir(d)
cmd = prefix + node.getAttribute("name") + suffix
call(cmd)
- 執行 Python 腳本開始下載
打開 Python 客戶端(win10的同學直接在屏幕左下角的開始按鈕出搜python)
打開上一步保存的 python_download.py 腳本文件
點擊 Run->Run Module 來運行腳本,或直接按F5運行。
到這裏就基本ojbk了,默默的等待下載吧,我從早上10點下到晚上7點下班都沒好,下班前也就沒關電腦了
溫馨提示,下載過程中一直會有一個黑色的cmd窗口不斷的彈出(下載進程),建議利用下班時間進行下載,以免影響工作
下載下來以後需要通過AS去打開,由於編譯過程比較複雜,這裏我們推薦
https://github.com/difcareer/AndroidSourceReader
給我們提供了各個版本導入as所需的編譯產物。