首页 > 代码库 > 从github下载最新Android源码

从github下载最新Android源码

今年5月底开始,谷歌彻底被墙,所有谷歌的网站都不能访问了,这次包括了android.org,googlesource.com,code.google.com。Android官方的资源不能访问,想下载Android代码当然是困难重重了。

本文就为大家解决这一问题——介绍如何使从github.com/andorid下载Andorid最新源码。俗话说“授之以鱼不如授之以渔”,本文不只是给出一个已经打包好的源码的链接,而是重现一下我是如何下载的,希望对大家遇到的类似的问题能有所帮助。


github.com/android简介

打开github.com/android可以看到:

中间黑体列出的的是各个项目的链接。github.com/android上托管的Android源码是用git单独管理每个项目的,而没有像googlesource.com那样使用repo(谷歌开发的基于git的命令行工具)管理所有项目。所以,如果你只想下载一个项目的源码,可以单独下载(比如开发App想跟一下SDK的一些源码可以下载platform_frameworks_base项目)。


怎么下载一个项目的代码?

用过git的同学当然知道:

git clone [仓库URL] 
就可以了,github项目页面的右边栏给出了仓库URL,如下图(clone URL下方的文本框):


当然,如果你只要配置“App源码调试”环境,可以点“Download ZIP”按钮,下载zip格式的源码包,然后在Eclipse调试的时候选择下载好的源码包。


如何批量下载github上的Android源码

这里在是本文的重点——批量下载github上的android源码。

根据单独下载一个项目的过程,下载所有项目的源码可以有两种方法:

  1. 得到所有项目的git仓库URL
  2. 得到所有项目的ZIP包下载链接

接下来的问题是如何得到这样的URL?

当然是从网页上(也就是页面的HTML文件上)!

现在问题就是如何得到这些网页?

我瞬间想到的办法就是——用CURL


ps: CURL是一个命令行上传下载工具,支持多种协议包括HTTP, HTTPS, FTP等。

没有curl的同学可以用如下命令下载:

sudo apt-get install curl


有了CURL用来抓页面,就不愁得不到仓库URL或ZIP包URL了:

首先要抓取github.com/android的页面,再“顺藤摸瓜”地抓取各个项目的页面,再从各项目的页面上抓取仓库URL或ZIP包URL。


抓取页面

在github.com/android页面的下方可以看github.com/andoid一共分5页,点击后面几个页面,可以发现这几个页面的URL形式一致为:

https://github.com/android?page=N
其中N表示“第几页”,比如第二页,就是https://github.com/android?page=2

由此,可以用bash脚本轻易写出抓取这5个页面的代码(可以直接在终端里执行下面的命令):

for ((i=1; i<=5; i++)); do
	curl https://github.com/android?page=$i > android.github.com.$i.html;
done

有捷径

再看项目的首页URL和仓库URL可以发现:

仓库URL = 首页URL + .git

比如,platform_frameworks_base的首页URL是:

https://github.com/android/platform_frameworks_base
仓库URL是:

https://github.com/android/platform_frameworks_base.git
当然,如果你有github.com的帐号,并且当前环境的pubkey已加入到了profile下,也可以使用ssh协议下载(而不是https),ssh下载的URL是:

git://github.com/android/platform_frameworks_base.git
由此可知,获取仓库URL可以不用进一步抓起项目首页,更简单(下面只介绍获取所有仓库URL)。

解析页面

现在的问题就是——如何从这些页面上得到仓库的URL了。

可以通过浏览器的“审查元素”菜单快速找到页面上一个链接对应的HTML代码块,如下图:


可以看到platform_frameworks_base项目的超链接(<a href=http://www.mamicode.com/"xxx">)被一个

标签包围,粗略浏览一下刚才下载的几个子页面,可以看出每个项目首页的链接都是如此(在

的下一行)。

根据刚才找到的两个规律:

  1. 项目首页URL总是出现在<h3 class="repo-list-name">的下一行
  2. 仓库URL = 首页URL + .git

可以写出解析各个项目首页(仓库URL)的python代码(getGitRepos.py):

#!/usr/bin/python
import sys

flag = False
link_prefix = 'https://github.com' # 如果使用ssh协议下载,可将https改为git

allLines = sys.stdin.readlines()
for curLine in allLines:
	if curLine.find('repo-list-name') >= 0:
		flag = True
	if flag:
		pos = curLine.find('href=http://www.mamicode.com/"')>之所以用python解析,只是因为python写起来简洁,也可以用其他语言。

这段python程序从标准输入读入文本并解析,实际使用时可用重定向操作符实现,实现用之前抓取的github.com/android的几个子页面替代标准输入。

程序输出的git clone命令的path参数会忽略仓库名称前面的platform_前缀,这使得最终下载下来的源码结构和repo sync下来的类似。


可以先用一个页面测试一下,看看解析出来的URL对不对(上面的python代码就是这样改正确的),终端输入下面命令:

./getGitRepos.py < android.github.com.1.html
该命令的输出为:
git clone https://github.com/android/platform_dalvik.git dalvik
git clone https://github.com/android/platform_bionic.git bionic
git clone https://github.com/android/platform_system_core.git system/core
git clone https://github.com/android/kernel_common.git kernel/common
git clone https://github.com/android/platform_external_qemu.git external/qemu
git clone https://github.com/android/platform_build.git build
git clone https://github.com/android/platform_development.git development
git clone https://github.com/android/platform_frameworks_base.git frameworks/base
git clone https://github.com/android/platform_manifest.git manifest
git clone https://github.com/android/platform_frameworks_support.git frameworks/support
git clone https://github.com/android/platform_packages_apps_settings.git packages/apps/settings
git clone https://github.com/android/platform_external_dhcpcd.git external/dhcpcd
git clone https://github.com/android/platform_external_webkit.git external/webkit
git clone https://github.com/android/platform_external_protobuf.git external/protobuf
git clone https://github.com/android/platform_packages_providers_mediaprovider.git packages/providers/mediaprovider
git clone https://github.com/android/platform_external_elfutils.git external/elfutils
git clone https://github.com/android/platform_external_strace.git external/strace
git clone https://github.com/android/platform_hardware_libhardware.git hardware/libhardware
git clone https://github.com/android/platform_external_tinyxml.git external/tinyxml
git clone https://github.com/android/platform_external_oprofile.git external/oprofile

测试基本上出现问题,因为getGitRepos.py输出的是git clone命令,执行之后在能执行实际的下载动作;所以,需要将这些输出保存下来;

使用如下命令可将每个子页面解析出的git clone命令保存到一个文本文件内:

for page in `ls android.github.com.*`; do
	echo parse $page
	./getGitRepos.py < $page >> gitRepos.txt;
done

有了写满git clone命令的gitRepos.txt后,可以使用如下命令执行之:

sh gitRepos.txt

一键下载

为了方便广大网友使用,我已将上面需要用的命令写道了下面的cloneRepos.sh:

#!/bin/bash
# clone AOSP each repo from https://github.com/android
# this shell depends on git, curl and python 2.
# INSTALL depends:
#	sudo apt-get install git curl python
#
# by xu(xusiwei1236@163.com).

# download github AOSP sub pages
for ((i=1; i<=5; i++)); do
	echo get AOSP subpage https://github.com/android?page=$i
	curl https://github.com/android?page=$i > android.github.com.$i.html;
done

# parse git repo URL from each sub pages
mkdir repos
cat /dev/null > gitRepos.txt
for page in `ls android.github.com.*`; do
	echo parse $page
	./getGitRepos.py < $page >> gitRepos.txt;
done

# clone repos
sh gitRepos.txt
另外,我已将上面的cloneRepos.sh和getGitRepos.py打包在了一起,可以到http://download.csdn.net/detail/xusiwei1236/7960925下载使用。

当然,如果觉得自己运行麻烦,可以直接copy生成的gitRepos.txt,直接粘贴到终端里执行之,即可下载全部Android源码:

git clone https://github.com/android/platform_dalvik.git dalvik
git clone https://github.com/android/platform_bionic.git bionic
git clone https://github.com/android/platform_system_core.git system/core
git clone https://github.com/android/kernel_common.git kernel/common
git clone https://github.com/android/platform_external_qemu.git external/qemu
git clone https://github.com/android/platform_build.git build
git clone https://github.com/android/platform_development.git development
git clone https://github.com/android/platform_frameworks_base.git frameworks/base
git clone https://github.com/android/platform_manifest.git manifest
git clone https://github.com/android/platform_frameworks_support.git frameworks/support
git clone https://github.com/android/platform_packages_apps_settings.git packages/apps/settings
git clone https://github.com/android/platform_external_dhcpcd.git external/dhcpcd
git clone https://github.com/android/platform_external_webkit.git external/webkit
git clone https://github.com/android/platform_external_protobuf.git external/protobuf
git clone https://github.com/android/platform_packages_providers_mediaprovider.git packages/providers/mediaprovider
git clone https://github.com/android/platform_external_elfutils.git external/elfutils
git clone https://github.com/android/platform_external_strace.git external/strace
git clone https://github.com/android/platform_hardware_libhardware.git hardware/libhardware
git clone https://github.com/android/platform_external_tinyxml.git external/tinyxml
git clone https://github.com/android/platform_external_oprofile.git external/oprofile
git clone https://github.com/android/platform_external_neven.git external/neven
git clone https://github.com/android/platform_external_netperf.git external/netperf
git clone https://github.com/android/platform_external_netcat.git external/netcat
git clone https://github.com/android/platform_external_libxml2.git external/libxml2
git clone https://github.com/android/platform_external_libpng.git external/libpng
git clone https://github.com/android/platform_external_libpcap.git external/libpcap
git clone https://github.com/android/platform_external_libffi.git external/libffi
git clone https://github.com/android/platform_external_jpeg.git external/jpeg
git clone https://github.com/android/platform_external_jhead.git external/jhead
git clone https://github.com/android/platform_external_jdiff.git external/jdiff
git clone https://github.com/android/platform_external_iptables.git external/iptables
git clone https://github.com/android/platform_external_icu4c.git external/icu4c
git clone https://github.com/android/platform_external_giflib.git external/giflib
git clone https://github.com/android/platform_external_freetype.git external/freetype
git clone https://github.com/android/platform_external_fdlibm.git external/fdlibm
git clone https://github.com/android/platform_external_expat.git external/expat
git clone https://github.com/android/platform_external_esd.git external/esd
git clone https://github.com/android/platform_external_emma.git external/emma
git clone https://github.com/android/platform_external_dropbear.git external/dropbear
git clone https://github.com/android/platform_external_apache-http.git external/apache-http
git clone https://github.com/android/platform_external_openssl.git external/openssl
git clone https://github.com/android/platform_external_sonivox.git external/sonivox
git clone https://github.com/android/platform_packages_providers_telephonyprovider.git packages/providers/telephonyprovider
git clone https://github.com/android/platform_packages_providers_downloadprovider.git packages/providers/downloadprovider
git clone https://github.com/android/platform_packages_providers_contactsprovider.git packages/providers/contactsprovider
git clone https://github.com/android/platform_packages_providers_calendarprovider.git packages/providers/calendarprovider
git clone https://github.com/android/platform_packages_apps_voicedialer.git packages/apps/voicedialer
git clone https://github.com/android/platform_packages_apps_stk.git packages/apps/stk
git clone https://github.com/android/platform_packages_apps_soundrecorder.git packages/apps/soundrecorder
git clone https://github.com/android/platform_packages_apps_phone.git packages/apps/phone
git clone https://github.com/android/platform_packages_apps_packageinstaller.git packages/apps/packageinstaller
git clone https://github.com/android/platform_packages_apps_music.git packages/apps/music
git clone https://github.com/android/platform_packages_apps_mms.git packages/apps/mms
git clone https://github.com/android/platform_packages_apps_htmlviewer.git packages/apps/htmlviewer
git clone https://github.com/android/platform_packages_apps_email.git packages/apps/email
git clone https://github.com/android/platform_packages_apps_contacts.git packages/apps/contacts
git clone https://github.com/android/platform_packages_apps_camera.git packages/apps/camera
git clone https://github.com/android/platform_packages_apps_calendar.git packages/apps/calendar
git clone https://github.com/android/platform_packages_apps_calculator.git packages/apps/calculator
git clone https://github.com/android/platform_packages_apps_browser.git packages/apps/browser
git clone https://github.com/android/platform_hardware_ril.git hardware/ril
git clone https://github.com/android/platform_external_zlib.git external/zlib
git clone https://github.com/android/platform_external_yaffs2.git external/yaffs2
git clone https://github.com/android/platform_external_tcpdump.git external/tcpdump
git clone https://github.com/android/platform_external_tagsoup.git external/tagsoup
git clone https://github.com/android/platform_external_srec.git external/srec
git clone https://github.com/android/platform_external_sqlite.git external/sqlite
git clone https://github.com/android/platform_external_skia.git external/skia
git clone https://github.com/android/platform_external_safe-iop.git external/safe-iop
git clone https://github.com/android/platform_external_ppp.git external/ppp
git clone https://github.com/android/kernel_msm.git kernel/msm
git clone https://github.com/android/platform_system_bluetooth.git system/bluetooth
git clone https://github.com/android/platform_prebuilt.git prebuilt
git clone https://github.com/android/platform_packages_providers_drmprovider.git packages/providers/drmprovider
git clone https://github.com/android/platform_external_ping.git external/ping
git clone https://github.com/android/platform_external_dbus.git external/dbus
git clone https://github.com/android/tools_repo.git tools/repo
git clone https://github.com/android/android.github.io.git android.github.io
git clone https://github.com/android/platform_external_wpa_supplicant.git external/wpa/supplicant
git clone https://github.com/android/platform_packages_apps_googlesearch.git packages/apps/googlesearch
git clone https://github.com/android/platform_system_wlan_ti.git system/wlan/ti
git clone https://github.com/android/platform_packages_providers_googlecontactsprovider.git packages/providers/googlecontactsprovider
git clone https://github.com/android/platform_packages_apps_im.git packages/apps/im
git clone https://github.com/android/platform_packages_providers_improvider.git packages/providers/improvider
git clone https://github.com/android/platform_external_tremor.git external/tremor
git clone https://github.com/android/platform_packages_apps_sync.git packages/apps/sync
git clone https://github.com/android/platform_packages_apps_alarmclock.git packages/apps/alarmclock
git clone https://github.com/android/platform_external_aes.git external/aes
git clone https://github.com/android/platform_external_clearsilver.git external/clearsilver
git clone https://github.com/android/platform_external_googleclient.git external/googleclient
git clone https://github.com/android/platform_packages_apps_launcher.git packages/apps/launcher
git clone https://github.com/android/platform_external_bluez.git external/bluez
git clone https://github.com/android/platform_external_gdata.git external/gdata
git clone https://github.com/android/platform_frameworks_opt_com.google.android.git frameworks/opt/com.google.android
git clone https://github.com/android/platform_external_elfcopy.git external/elfcopy
git clone https://github.com/android/platform_frameworks_policies_base.git frameworks/policies/base
git clone https://github.com/android/platform_packages_apps_updater.git packages/apps/updater
git clone https://github.com/android/platform_external_opencore.git external/opencore

从github下载最新Android源码