本文首發(fā)于 公眾號 劉望舒
前言
這篇文章我們來學(xué)習(xí)如何下載AOSP 源碼,如果你還沒有Linux 環(huán)境,請查看Android AOSP基礎(chǔ)(一)VirtualBox 安裝 Ubuntu這篇文章,另外如果你不需要編譯源碼,或者不需要最新的源碼,可以直接從百度網(wǎng)盤:https://pan.baidu.com/s/1ngsZs 將源碼下載下來。
1.關(guān)于AOSP
AOSP(Android Open Source Project)是Google開放的Android 開源項目,中文官網(wǎng)為:https://source.android.google.cn/。
AOSP通俗來講就是一個Android系統(tǒng)源碼項目,通過它可以定制 Android 操作系統(tǒng),國內(nèi)手機(jī)廠商都是在此基礎(chǔ)上開發(fā)的定制系統(tǒng)。因為墻的緣故,如果無法連接谷歌服務(wù)器獲取AOSP源碼,可以從 清華大學(xué)鏡像站或者 中科大鏡像。本篇文章以清華大學(xué)鏡像站為例。
2. 下載 repo工具
Android源碼包含數(shù)百個git庫,光是下載這么多的git庫就是一項繁重的任務(wù),所以Google開發(fā)了repo,它是用于管理Android版本庫的一個工具,使用了Python對git進(jìn)行了一定的封裝,簡化了對多個Git版本庫的管理。
安裝 Git,在Ubuntu輸入如下命令:
sudo apt-get install git
可能會報如下的錯誤:

這個問題是有另外一個程序正在運行,導(dǎo)致資源被鎖不可用,輸入如下命令進(jìn)行解決:
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
接下來創(chuàng)建bin,并加入到PATH中。
mkdir ~/bin
PATH=~/bin:$PATH
安裝curl庫:
sudo apt-get install curl
下載repo并設(shè)置權(quán)限:
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo
最后安裝python,repo初始化時會用到:
sudo apt-get install python
重啟虛擬機(jī)后,開始下載源碼。
3. 下載源碼
建立工作目錄 :
mkdir aosp
cd aosp
repo的運行過程中會嘗試訪問官方的git源更新自己,如果想使用tuna的鏡像源進(jìn)行更新,可以將如下內(nèi)容復(fù)制到你的~/.bashrc里:
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
設(shè)置身份,添加自己的郵箱和姓名:
git config --global user.email "piratemorgen@gmail.com"
git config --global user.name "piratemorgen"
初始化倉庫:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
初始化并指定版本:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r8
同步源碼:
repo sync
源碼下載完成后,看到下面的信息可以說明下載成功。整個源碼的大小為36.5 g

感謝:
https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
https://blog.csdn.net/counsellor/article/details/86591081
http://wuxiaolong.me/2018/07/07/AOSP1/
https://github.com/tuna/issues/issues/362
分享大前端、Java、跨平臺等技術(shù),關(guān)注職業(yè)發(fā)展和行業(yè)動態(tài)。
