在Android開發過程中,經常需要查看Android的源代碼。而在下載的SDK中,有各個Platform版本的Framework層的源代碼下載,而這些源代碼是不完整的,沒有C/C++的代碼。我們需要下載完整的Android的源代碼。
Google提供的源代碼的官網是:https://source.android.com/(需要梯子)
裏面有很詳細的下載源代碼的步驟:https://source.android.com/source/downloading.html
在2011年8月的時候,我使用Ubuntu下載成功了Android的源代碼。時隔4年多的時間,我已然忘了當時是怎麼做的了,依稀記得是教育網給力。而且使用了Sundy老師介紹的一款工具來看,叫啥名也忘了。現在重新來過,通過這篇文章Mark一下如何拿到Android源代碼,爲後面的開發查看提供更可靠的信息源。
我們使用清華的鏡像來下載Android的源代碼。https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/?utm_source=tuicool&utm_medium=referral
步驟如下:
下載 repo 工具:
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
由於googleapis.com被和諧了,訪問不了,於是第一步repo工具不成功。方案是
mkdir ~/bin
PATH=~/bin:$PATH
git clone git://aosp.tuna.tsinghua.edu.cn/android/git-repo.git
//得到的git-repo中的repo文件拷貝到bin目錄
cp git-repo/repo ~/bin/
//在repo文件中修改
REPO_URL = ‘git://aosp.tuna.tsinghua.edu.cn/android/git-repo’
建立工作目錄
mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY
初始化repo
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
//或 repo init -u git://aosp.tuna.tsinghua.edu.cn/aosp/platform/manifest
//如果提示無法連接到 gerrit.googlesource.com,可以編輯 ~/bin/repo,把 REPO_URL 一行替換成下面的:
REPO_URL = ‘https://gerrit-google.tuna.tsinghua.edu.cn/git-repo’
下載某個特定的 Android 版本
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-6.0.1_r16
最新的分支是android-6.0.1_r16。所有的版本列表見:http://source.android.com/source/build-numbers.html#source-code-tags-and-builds
同步源碼樹(以後只需執行這條命令來同步):
repo sync
使用每月更新的初始化包
由於首次同步需要下載 37GB 數據,過程中任何網絡故障都可能造成同步失敗,因此你可以選擇使用每月更新的初始化包。
首先到 http://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/ 選擇合適自己的構建包,aosp-latest.tar.xz 經過了 xz 壓縮, 相對 aosp-latest.tar 小一些,但是解壓的時間也要考慮進來,27G的壓縮包呢!(如果CPU核多可以使用pxz解壓)
使用方法如下:
wget http://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下載初始化包
tar xf aosp-latest.tar
cd AOSP # 解壓得到的 AOSP 工程目錄
repo sync # 正常同步
使用Eclipse查看源碼
在~/WORKING_DIRECTORY/development/ide目錄下,可以看到Eclipse、emacs、intellij、xcode的文件夾,可能是源代碼支持這幾種工具查看。我們將Eclipse下的.classpath文件拷貝到~/WORKING_DIRECTORY根目錄,然後在Eclipse中新建Java項目,選擇location時選擇~/WORKING_DIRECTORY目錄即可導入Android的所有源碼。由於數量比較大內容比較多,可能會等待幾分鐘的時間。最後情況如下: