因爲公司的制度的使然,開發環境只能侷限於Windows下,因此不得不在Windows下掙扎徘徊。這破逼公司真是手機業界的一朵奇葩,想起來便讓人久久不能釋懷。python
官方推薦Android源碼的下載方式是Linux系統上是經過Repo來完成的。Win環境下Repo的使用又依賴Git、Python。源碼的下載又涉及到偉大的長城防火牆。因此這篇文章的核心有三個:安裝Repo依賴環境、讓Repo在Win上面跑起來、使用國內源避開牆。android
若是是使用Win10上面用Linux子系統下載,能夠參考微軟官方的教程(不過依然仍是會出問題)。若是直接在Win環境下面,能夠參考本教程。git
如下是Win下面下載Aosp源代碼的步奏。github
安裝後shell
將git添加到環境變量中 C:\Program Files\Git\cmd;
將MinGW添加到環境變量中 C:\Program Files\Git\bin;
版本選擇2.7+,不然會報異常ypeError: startswith first arg must be bytes or a tuple of bytes, not strsegmentfault
安裝後api
將Python添加到環境變量中 C:\Program Files\Python27;
所謂的源就是連接。因爲官方的源基本處於鏈接不上的狀態,在此咱們選擇了清華鏡像站的源。下面涉及到源的地方所有默認使用清華鏡像站的源瀏覽器
Android官方: https://storage.googleapis.co...
清華大學鏡像站: https://mirrors.tuna.tsinghua...
官方提供Repo在Windows跑起來上會報錯,咱們不建議你們使用官方的Repo。可是咱們仍是先教你們幾種下載Repo的方式,一心只關心源碼的同窗能夠跳過這節。安全
mkdir ~/bin PATH=~/bin:$PATH curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo chmod a+x ~/bin/repo
這種方式咱們不推薦,由於下載下的只有一個repo文件,repo init確定會失敗,並且還會把 .repo文件夾 刪除掉。徹底是浪費時間。
固然,這種方式在Linux環境中仍是有好處的,將Repo添加到了環境變量中,之後敲命令很方便的。bash
這種方式能夠下載完整的Repo文件
git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
在Windows上最簡單的下載方式,把源拷貝到瀏覽器地址欄中就行。
https://mirrors.tuna.tsinghua...
這種方式和git clone方式獲得的文件是同樣的
若是你下載了原生的Repo,你就會發現,在Win上面運行會報錯,還須要修改源。若是你熟悉Python,能夠本身調試一下。若是不熟悉,可使用咱們推薦的這個 https://github.com/esrlabs/gi...。
這是一個德國公司修改過的客戶端,適配了Windows 點此下載。以後咱們稍微調整下就能夠了。
下載後能夠解壓到源碼所在的文件夾。好比個人放在了 「E:\LocalProject\Oreo\git-repo-stable」
在源碼所在文件夾
右鍵
Git Bash Here
輸入如下命令
git-repo-stable\\repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.1.0_r7
android-8.1.0_r7是版本號,更多的版本號能夠查看列表
repo init 以後,源碼文件夾下會多一個.repo的文件夾。打開E:\Preo\.repo\repo\project.py。修改279行def __linkIt(self, relSrc, absDest)方法。若是不修改會報「WindowsError: [Error 5]」的錯誤。具體分析可見 https://segmentfault.com/a/11...
def __linkIt(self, relSrc, absDest): # link file if it does not exist or is out of date # if not os.path.islink(absDest) or (os.readlink(absDest) != relSrc): if not portable.os_path_islink(absDest) or (portable.os_path_realpath(absDest) != relSrc): try: # remove existing file first, since it might be read-only if os.path.lexists(absDest): # os.remove(absDest) # 增長這段 if os.path.isfile(absDest): os.remove(absDest) else: os.removedirs(absDest) # 增長這段 else: dest_dir = os.path.dirname(absDest) if not os.path.isdir(dest_dir): os.makedirs(dest_dir) # os.symlink(relSrc, absDest) portable.os_symlink(relSrc, absDest) except IOError: _error('Cannot link file %s to %s', relSrc, absDest)
而後再打開E:\Preo\.repo\manifest.xml,將如下三個項目註釋掉,否則會報「cannot initialize work tree」,具體分析可見 https://segmentfault.com/a/11...
<!--<project path="libcore" name="platform/libcore" groups="pdk" />--> <!--<project path="external/libunwind" name="platform/external/libunwind" groups="pdk" />--> <!--<project path="libcore" name="platform/libcore" groups="pdk" />-->
而後敲入最後一句命令就大功告成了!
git-repo-stable\\repo sync
repo sync後面還能夠加這些參數
repo sync -cdf --force-sync 同步代碼時爲了安全,能夠在同步時覆蓋掉已經存在的代碼 repo sync -c 只下載當前分支,能夠加快代碼下載速度,並且節省硬盤空間 repo sync -f 下載到某個項目失敗了,咱們能夠用-f屬性來讓它繼續下載,失敗的項目咱們回頭再說。 -c, --current-branch fetch only current branch from server -d, --detach detach projects back to manifest revision -f, --force-broken continue sync even if a project fails to sync --force-sync overwrite an existing git directory if it needs to point to a different object directory. WARNING: this may cause loss of data
接下來漫長的等待,大概睡一覺就下載完了。睡不着的小夥伴能夠看看下面的兩個教程,加強記憶。
Android提供的教程: https://source.android.google...
清華大學鏡像站提供的教程: https://mirrors.tuna.tsinghua...
好吧,有些同窗實在不想這麼折騰,想簡單快捷,也不是沒有辦法的。
android-6.0.1_r72
連接: https://pan.baidu.com/s/1skEVR1F 密碼: ik6t
android-7.1.1_r1
連接: https://pan.baidu.com/s/1dF6hc3n 密碼: pfsq
android-8.0.0_r1
連接: https://pan.baidu.com/s/1bqKNx3x 密碼: bthv
點此查看更多代碼
再來一個Android歷史以來的代碼,而且會一直更新,目前是45G。
下載 https://mirrors.tuna.tsinghua...,下載完成後記得根據 checksum.txt 的內容校驗一下。
因爲全部代碼都是從隱藏的 .repo 目錄中 checkout 出來的,因此咱們只保留了 .repo 目錄,下載後解壓 再 repo sync 一遍便可獲得完整的目錄。使用方法以下:
wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下載初始化包 tar xf aosp-latest.tar cd AOSP # 解壓獲得的 AOSP 工程目錄 # 這時 ls 的話什麼也看不到,由於只有一個隱藏的 .repo 目錄 repo sync # 正常同步一遍便可獲得完整目錄 # 或 repo sync -l 僅checkout代碼
此後,每次只需運行 repo sync 便可保持同步。 咱們強烈建議您保持天天同步,並儘可能選擇凌晨等低峯時間
按照上面的教程咱們就獲得了想要的代碼,發個截圖給你們參考一下
到這裏應該編譯源代碼了,可是咱們初始化環境就會發現報錯「Only bash is supported」
$ source build/envsetup.sh ps: unknown option -- o Try `ps --help' for more information. WARNING: Only bash is supported, use of other shell would lead to erroneous results
一直覺得Git Bash就是Bash,到這裏才發現其實不同。因此下載了這麼一堆,咱們其實並無法編譯,只能看看。這下真的只是在外邊蹭蹭不進去了。因此想和Android深刻交流仍是使用Linux吧。
1.出現 curl: (22) The requested URL returned error: 404 Not Found Server does not provide clone.bundle; ignoring. 怎麼辦?
無視便可。
2.有的同窗在執行repo init命令的時候可能會出現這樣的提示
error.GitError: manifests var: *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository.
使用git要設置帳號
git config --global user.email "you@example.com" git config --global user.name "Your Name"
3.有些網站的源爲何是git://android.git.kernel.org/platform/manifest.git
android.git.kernel.org是google之前用來下載android的網址,如今不用了。聽說是服務器被黑了。
repo sync配合manifest下載舊版本代碼 https://blog.csdn.net/ly89070...
Git Repo 鏡像使用幫助 https://mirrors.tuna.tsinghua...
Android 鏡像使用幫助 https://mirrors.tuna.tsinghua...
官方下載源代碼教程 https://source.android.google...
官方Repo開發教程 https://source.android.google...
官方Repo 命令參考資料 https://source.android.google...
官方代號、標記和細分版本號 https://source.android.google...
repo - The multiple repository tool (also works on MS Windows!) https://code.google.com/p/git...
window7下配置下載android源碼環境,安裝Repo https://blog.csdn.net/nicolel...
repo下載Android源碼(國內鏡像)https://blog.csdn.net/shenlan...
Android源碼下載(包括最新8.0版本) https://blog.csdn.net/hty1053...
深究repo的所謂「續傳」功能 https://blog.csdn.net/alien75...
Android源代碼倉庫及其管理工具Repo分析 https://blog.csdn.net/luoshen...