Windows下載Android所有源碼

下載msysgit,安裝node

官方下載:http://code.google.com/p/msysgit/downloads/listpython

 

打開Git Bash,執行命令linux

cd D:

git clone https://android.googlesource.com/platform/manifest.git

 

輸入命令,切換到manifest目錄android

cd manifest

git tag 列出android各個分支版本git

git tag

下載android-2.2系統源碼,輸入下面命令,若是要下載其餘版本源碼,checkout git tag列出的版本號便可vim

git checkout android-2.2_r1

checkout以後,manifest/default.xml文件中記錄的就是android2.2系統各個模塊的路徑windows

 

咱們來分析一下default.xml文件,api

以bionic爲例,path屬性表示bionic源碼的相對路徑,假設android源碼在d:/android-source,下載bionic以後,應該存放在d:/android-source/bionic目錄網絡

name屬性是bionic源代碼在庫上的路徑,完整的路徑就是:http://android.googlesource.com/platform/bionic.git,有了源碼下載路徑,執行git clone就能夠將bionic源碼下載到本地dom

 

<project path="bionic" name="platform/bionic" />

 

Android源碼中project不少,一個一個下載比較麻煩,本人寫了一個python腳本,雙擊download-src.py執行此腳本,就能夠將android完整源碼下載到本地。

PS:執行此腳本的前提是已經執行了git checkout,選擇好了要下載的Android源碼版本,若是你的manifest文件不是D:/manifest/default.xml,請自行修改腳本。

 

download-src.py源碼:

import xml.dom.minidom
import os
from subprocess import call

#downloaded source path rootdir = "D:/android-source"

#git program path git = "D:/Program Files/Git/bin/git.exe"

dom = xml.dom.minidom.parse("D:/manifest/default.xml")
root = dom.documentElement

prefix = git + " clone https://android.googlesource.com/"
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)

 

使用清華鏡像的方式

 

Android源碼由Git進行管理,因爲源碼由衆多的模塊組成,每一個模塊又可能依賴許多第三方庫,因此若是想要經過git連接一個個clone下來的話工做量將會很是大,因此Google用Python編寫了一個repo工具用來批量下載Android源碼(可是這個工具須要linux環境,若是想在windows下使用咱們就必須在windows環境下搭建一個模擬的linux環境,這個模擬的linux環境咱們能夠用Cygwin來搭建)。
首先Google用一個Git倉庫用來記錄當前Android版本下各個子項目的Git倉庫分別處於哪個分支,這個倉庫一般叫作:manifest倉庫。這個倉庫裏面有一個XML文件,其實就是一個文件清單,列出了本代碼倉庫依賴哪些代碼,該去哪下載,分支是什麼。repo工具首先會clone這個倉庫,而後根據這個XML文件(default.xml)列出的路徑去批量下載源碼。在windows下通常能夠用如下兩種方法來下載Android源碼。

注意:因爲google的連接被牆了,因此咱們可使用清華大學提供的鏡像連接進行下載
https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/

1、自制Python腳本批量下載

須要工具以下:
下載git,安裝 官方下載:https://git-scm.com/downloads/
下載python,安裝 官方網址:http://www.python.org
打開Git Bash,執行命令,我是放在c盤的,路徑可自定義
git clone https://aosp.tuna.tsinghua.edu.cn/platform/manifest.git
輸入命令,切換到manifest目錄
cd manifest
Git tag 列出android各個分支版本
下載android-7.0系統源碼,輸入下面命令,若是要下載其餘版本源碼,checkout git tag列出的版本號便可
git checkout android-7.0.0_r6
checkout以後,manifest/default.xml文件中記錄的就是android7.0系統各個模塊的路徑,
下面就輪到python出場了,這裏用的是網上的一段python代碼,實現源碼的批量下載
執行此腳本的前提是已經執行了git checkout,選擇好了要下載的Android源碼版本,若是你的manifest文件不是C:\Android\Android Src\manifest\default.xml,還要把裏面的git.exe的路經修改爲你的安裝路徑,請自行修改腳本。
download-src.py源碼:

import xml.dom.minidom  
import os  
from subprocess import call  

#downloaded source path  
rootdir = "C:/Android/Android Src"  

#git program path  
git = "F:/Users/Richard/AppData/Local/GitHub/PortableGit_c2ba306e536fdf878271f7fe636a147ff37326ad/bin/git.exe"
dom = xml.dom.minidom.parse("C:/Android/Android Src/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)  
    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)

執行這個腳本以後將會自動下載源碼

2、安裝cygwin

1.下載 Cygwin ,而後就是一路的下一步(注意:若是第一次安裝,你須要選install from internet
而後就是選安裝位置,還有臨時文件的位置和鏈接網絡的設置,這些都默認就能夠.關鍵的一步是選擇要安裝的庫和程序,如下這幾個是要安裝的:
Net -> curl
Devel -> git,git-completion,git-gui,gitk
Libs -> libreadline6,libiconv2
Editors -> vim
Python -> python
若是很差找,你能夠在上面的Search上搜索.
2.下載Repo
啓動Cygwin,而後cd /bin,切到bin目錄執行
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
下載repo到bin目錄.而後執行
chmod a+x repo賦予它可執行的權限.
初始化倉庫:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
若是須要某個特定的 Android 版本:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-4.0.1_r1 同步源碼樹(之後只需執行這條命令來同步):