最近在学习Android底层框架,今天打算研究一下Android源代码,源码的下载过程可没有想像的顺利。主要原因是网上的很多资料都是多年以前的,而一些网站都发生了变更,所以域名也会失效。OK,废话少说,我把遇到的各种问题和大家分享一下,希望大家能对号入座,避免浪费不必要的时间~
以下是我按照正常下载过程总结出的步骤,遇到的问题将在后面讲述。
第一步:创建下载脚本文件(repo)的存放目录(可任意),这里我选用 ~/android/bin
- # mkdir ~/android/bin //创建repo目录
- #PATH= ~/android/bin:$PATH //为repo工具配置环境变量,方便后面使用
第二步:下载repo文件:(出错点)
- # curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~ /bin/repo//下载repo文件
- # chmod a+x ~/bin/repo //修改repo文件的权限设定
第三步:创建用于存放Android源代码的目录(可任意),这里我选用~/android/bin/android_source:
- # mkdir android_source //创建文件目录
- # cd android_source //进入该目录
第四步:初始化:(出错点)
- # repo init -u https://android.googlesource.com/platform/manifest //repo的初始化
此时便会要求repo工具自动下载Android源代码(默认是Master分支,也可以下载其他分支)。
如果下载其他分支,使用一下语句:
- # repo init -u https://android.googlesource.com/platform/manifest -bandroid-4.4.2_r2 //下载Android4.4.2源代码
随后可看到终端输出如下信息:
- Getting repo ...
- from git://codeaurora.org/tools/repo.git
- remote: Counting objects: 2818, done.
- remote: Compressing objects: 100% (863/863), done.
- remote: Total 2818 (delta 1907), reused 2818 (delta 1907)
- Receiving objects: 100% (2818/2818), 959.24 KiB | 798 KiB/s, done.
- Resolving deltas: 100% (1907/1907), done.
- From git://codeaurora.org/tools/repo
- * [new branch] aosp-new/maint -> origin/aosp-new/maint
- * [new branch] aosp-new/master -> origin/aosp-new/master
- * [new branch] aosp-new/stable -> origin/aosp-new/stable
- * [new branch] caf-stable -> origin/caf-stable
- * [new branch] master -> origin/master
- * [new branch] stable -> origin/stable
- * [new tag] v1.10.4 -> v1.10.4
- * [new tag] v1.12.16 -> v1.12.16
- * [new tag] v1.12.2 -> v1.12.2
- * [new tag] v1.6.8.1 -> v1.6.8.1
- * [new tag] v1.7.5 -> v1.7.5
可以看到Android源代码中的分支([new branch]和[new tag])。
第五步:开始下载Android源代码:
- # repo sync //同步,即下载源代码
之后会正式开始下载源代码,下载过程漫长,会根据具体情况的不同速度差异很大,整个过程Git会尽可能占用带宽,朋友们需要耐心等待。
下载过程中可能遇到的问题:
1.首先容易出错的是第二步,这里由于网站或者本地网络的问题,可能会出现如下故障:
- root@mc-virtual-machine:~/android/bin# curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/android/bin/repo
- % Total % Received % Xferd Average Speed Time Time Time Current
- Dload Upload Total Spent Left Speed
- 0 0 0 0 0 0 0 0 --:--:-- 0:00:21 --:--:-- 0
下载会卡在一开始,而且卡住的概率很高。此时不必急着检查问题,可能是暂时的网络故障,可以ctrl + c 中断之后再次执行,尝试多次通常可解决此问题。
2.其次容易出错的地方便是第四步。
由于站点的问题,有些朋友在第四步执行repo init命令的时候会报错,如下:
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: rgb(231, 229, 220); width: 693px; overflow: auto; padding-top: 1px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[html]</strong> <a target=_blank href="http://blog.csdn.net/mc_hust/article/details/33304733#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/mc_hust/article/details/33304733#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/mc_hust/article/details/33304733#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/mc_hust/article/details/33304733#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><a target=_blank href="https://code.csdn.net/snippets/336920" target="_blank" title="在CODE上查看代码片" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/336920/fork" target="_blank" title="派生到我的代码片" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 406px; top: 2956px; width: 26px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-xml" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">./bin/repo: 行 1: 未预期的符号 `newline' 附近有语法错误 </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">./bin/repo: 行 1: `<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"<span class="tag" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">' </span></span></li></ol></div> <span style="font-size: 18px;">解决方法:更换repo下载站点,使用如下命令</span> <pre class="best-text mb-10" code_snippet_id="336920" snippet_file_name="blog_20140509_9_9007086" name="code" style="white-space: pre-wrap; word-wrap: break-word;"># curl "http://php.webtutor.pl/en/wp-content/uploads/2011/09/repo" > ./repo //获取repo
此时再次执行第四步:
- # repo init -u https://android.googlesource.com/platform/manifest //repo的初始化
3.在第四步的时候终端会询问如下问题:
- root@mc-virtual-machine:~/android/bin/android_source# repo init -u https://android.googlesource.com/platform/manifest
- error: Failed to connect to 2404:6800:4005:c00::52: Network is unreachable while accessing https://android.googlesource.com/platform/manifest/info/refs
- fatal: HTTP request failed
- fatal: cannot obtain manifest https://android.googlesource.com/platform/manifest
通常在公司内部网络或者用虚拟机下运行的话,会经常遇到这个问题。
解决方法:
1. 浏览器登录https://android.googlesource.com/new-password,并用gmail帐号登录;
2. 点击网页上的“是”(有些资料上写的是点击“允许访问”,我看到到是“是”),得到类似如下结果:
- Username: git-hust.mczy.gmail.com
- Password: 1/3t2difm8LDvgpYCdci5nripSiQuWBaIv
<pre class="best-text mb-10" name="code" style="white-space: pre-wrap; word-wrap: break-word;"><span style="font-size: 18px;">然后将网页上由machine开头到两行复制下来,在linux终端里新建~/.netrc文本文档,复制进去,保存退出。最后在域名中加一个“/a”,完整命令如下 </span>
- # repo init -u https://android.googlesource.com/platform/manifest //repo的初始化
- Your Name [root]: MC
- Your Email [root@mc-virtual-machine.(none)]: hust.mczy@gmail.com
- Your identity is: MC <hust.mczy@gmail.com>
- is this correct [y/n]? y
- Testing colorized output (for 'repo diff', 'repo status'):
- black red green yellow blue magenta cyan white
- bold dim ul reverse
- Enable color display in this user account (y/n)?
此时需要验证gmail邮箱,然后输入用户名和Gmail邮箱,选择Y回车即可。
4.最后一步在执行repo sync时,可能会报如下错误:
- fatal: '../platform/abi/cpp.git' does not appear to be a git repository fata
这说明获取的路径有问题。
解决方法:
进入安装repo的bin目录下一个叫.repo的目录,打开manifest.xml文件(命令gedit manifest.xml)并找到fetch属性,在我的文件中显示fetch="..",将fetch修改为fetch="git://Android.git.linaro.org/",保存并退出。再次执行repo sync 即可开始下载。
5.另外还有一个很少出现的错误。同样是在repo sync时,如果出现如下错误:
- remote: Sending approximately 1.11 GiB ...
- remote: Counting objects: 42, done
- remote: Finding sources: 100% (42/42)
- remote: Sending approximately 200.67 MiB ...
- remote: Counting objects: 18, done
- remote: Finding sources: 100% (18/18)
- error: RPC failed; result=56, HTTP code = 2000 KiB/s
- fatal: The remote end hung up unexpectedly
- fatal: 过早的文件结束符(EOF)
- fatal: index-pack failed
- error: RPC failed; result=56, HTTP code = 200
- fatal: The remote end hung up unexpectedly
- fatal: early EOF
- fatal: unpack-objects failed
- error: RPC failed; result=56, HTTP code = 200
- fatal: The remote end hung up unexpectedly
- fatal: 过早的文件结束符(EOF)
- fatal: index-pack failed
- error: RPC failed; result=56, HTTP code = 200
- fatal: The remote end hung up unexpectedly
- fatal: 过早的文件结束符(EOF)
- fatal: index-pack failed
解决方法:
网络不顺通常是一时的,这里的办法除了换网之外只有反复执行 repo sync。当然这里完全不需要自己去反复执行,可以通过编写一个脚本程序,让计算机在下载崩溃后自动重新下载。在代码目录建立一个脚本(sh)文件,输入如下:
- #!/bin/bash
- echo ¨================start repo sync===============¨
- repo sync -f -j10
- while [ $? == 1 ]; do
- echo ¨================sync failed, re-sync again=============¨
- sleep 3
- repo sync -f -j10
- done
之后可以做自己的事,让程序自动下载,当你蓦然回首的时候,它已经默默下载成功了。。