您的位置:首页 > 移动开发 > Android开发

ubuntu13.04下载android4.0.1源码过程

2015-10-20 20:31 405 查看
最初我参考的是老罗的博客/article/1363578.html

进行下载安装的,但弄着弄着就发现不太对劲了。这里记录下详细过程:

1,我的前提是已经搭建好了Android开发环境,也即jdk已经安装好了,输入java -version来检查是否成功。搭建android开发环境可以看链接1 链接2.

2,首先要安装git工具,而repo是基于git的。同时要安装所需的若干软件,命令如下:

sudo apt-get install git-core
flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl
valgrind

最后的valgrind是为了编译时用,一次安完了省事。如果是为了下载源码,只需安装curl和git-
core就足够了。curl是一个利用URL语法在命令行方式下工作的文件
传输工具
,它支持很多协议,包括FTP、FTPS、HTTP、HTTPS、TELENT等,我们需要安装它从网络
上获取Repo脚本文件。然后curl就用不到了。repo是基于git的,git是一个分布式版本控制软件。由于android源码是由很多个部分组成
的,如果用git的话需要多次git
clone。为此google提供了repo,其实质就是若干个git命令写的脚本。这是curl git repo三者之间的关系。关于repo和git的使用,可以参考这里:

(1),http://blog.chinaunix.net/uid-20780196-id-3171133.html

(2),/article/4948151.html

(3),http://blog.sina.com.cn/s/blog_8218aa3801013ady.html

(4),http://zhidao.baidu.com/question/466204880.html

3,利用curl下载repo脚本

在普通用户下登录,不是root。这时~表示/home/xxx/的目录,在~下新建一个bin文件夹,用来放下载的repo脚本。参考android的官方指导文档http://source.android.com/source/downloading.html ,然后将~/bin目录添加到PATH。方法为sudo
gedit /etc/profile, 然后添加 PATH=$PATH:~/bin 保存关闭后,再终端输入
source /etc/profile使设置生效。事实上这里也可以用绝对路径来写死,/home/xxx/bin
需要注意的是,这一切都是在普通用户下,如果以root用户登录,~表示的是/root文件夹。所以此次下载android源码一律是普通用户,不要随便
切换。因为里面的在普通用户和root用户下表示的地方不一样。如果用绝对路径写死则无此问题。

然后终端输入:

curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo

用这条命令下载repo脚本到刚建的bin文件夹。

老罗的博客里的命令是:wget https://dl-ssl.google.com/dl/googlesource/git-repo/repo 用的是wget,应该也可以。我用的是上面的curl命令。然后chmod a+x ~/bin/repo为其增加可执行权限。

4,新建一个文件夹,即是repo的工作空间,将源码下载到此处。我新建的目录是/home/administrator/document/androidSource. cd到这个目录下,要确保这个目录一般用户可以访问读写。

然后就要用repo init命令来初始化repo了,如果输入:

repo init -u https://android.googlesource.com/platform/manifest[/code] 则是让repo下载最新的android源码。一般我们可以通过-b参数指定下载的android版本,输入:

repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1

则是让repo下载4.0.1的源码,也可以用-b Jelly Bean来指定android的版本,具体可以参考:http://source.android.com/source/initializing.html

http://source.android.com/source/build-numbers.html,尤其是第二个链接来选定版本号比较好。
如果输入上面的repo init .................xxx,再进入下一步骤虽然可以下载源码,但是下载中间会出现如下报错:

[html]
view plain
copy

error: The requested URL returned error: 406 while accessing https://android.googlesource.com/platform/manifest/info/refs
fatal: HTTP request failed

error: The requested URL returned error: 406 while accessing https://android.googlesource.com/platform/manifest/info/refs
fatal: HTTP request failed

Fetching projects: 9% (20/221) error: The requested URL returned error: 406 while accessing https://android.googlesource.com/platform/external/bison/info/refs
fatal: HTTP request failed

Fetching projects: 10% (23/221) error: Failed to connect to 2404:6800:4008:c01::52: Network is unreachable while accessing https://android.googlesource.com/platform/external/apache-xml/info/refs
fatal: HTTP request failed

error: Failed to connect to 2404:6800:4008:c01::52: Network is unreachable while accessing https://android.googlesource.com/platform/external/astl/info/refs
fatal: HTTP request failed

error: The requested URL returned error: 406 while accessing https://android.googlesource.com/platform/external/bluetooth/glib/info/refs
fatal: HTTP request failed

error: The requested URL returned error: 406 while accessing https://android.googlesource.com/platform/external/astl/info/refs
fatal: HTTP request failed

Fetching projects: 11% (25/221) error: The requested URL returned error: 406 while accessing https://android.googlesource.com/platform/external/apache-xml/info/refs
错误的核心就是 error android.googlesource.com/platform/manifest/info/refs,百度一下参考这里:http://www.rosoo.net/a/201302/16504.html 这是google防止匿名访问连接次数过多而设置的。

解决方法是:浏览器登录https://android.googlesource.com/new-password,输入自己的gmail帐号,点击网页上的“确定”或“允许访问”会得到如下信息(我只找到了确定按键):

[html]
view plain
copy

Login for Git:

Username: git-yanzi1225627.gmail.com

Password: 1/IwTl_VomaQWIk0I4gRjdDL5SybS8MGDv15Cb0bLonMg

Staying Authenticated:

To stay authenticated by saving the password, append the following line to the ~/.netrc configuration file:

machine android.googlesource.com login git-yanzi1225627.gmail.com password 1/IwTl_VomaQWIk0I4gRjdDL5SybS8MGDv15Cb0bLonMg machine android-review.googlesource.com login git-yanzi1225627.gmail.com password 1/IwTl_VomaQWIk0I4gRjdDL5SybS8MGDv15Cb0bLonMg

Make sure you have set the permissions on ~/.netrc so that only your user account can read the file. If your home directory is on a network filesystem, consider moving it to a local disk and making ~/.netrc a symbolic link to the local version.

新建一个.netrc文件,gedit ~/.netrc,如果有的话就不用新建了。在linux里前面有个"."表示这个文件是隐藏文件。在里面输入machine开头的两行话保存即可。

然后将下载的地址中间加一个/a,也即最终的repo init的命令是

repo init -u https://android.googlesource.com/a/platform/manifest -b android-4.0.1_r1

5,开始同步源码前的两处修改,就这两个地方我摸了两天

经过4步骤里的改.netrc操作,我发现下载时报的错依旧,大爷的。报错如下:

error: Failed connect to android.googlesource.com:443;Connection refused while accessinghttps://android.googlesource.com/a/platform/frameworks/base/info/refs

fatal: HTTP request failed

error: Cannot fetch platform/tools/motodev

error: Cannot fetch platform/frameworks/base

error: Cannot fetch platform/prebuilts/sdk

error: Exited sync due to fetch errors

参考文章http://blog.csdn.net/lengxibo/article/details/8299888#reply 原来是需要改/etc/hosts, 终端输入ping www.googlesource.com或ping android.googlesource.com,得到IP是www.googlesource.com和 android.googlesource.com的IP, 我的电脑上两个IP都是74.125.31.82.

然后sudo gedit /etc/hosts, 在里面输入:

[html]
view plain
copy

74.125.31.82 www.googlesource.com

74.125.31.82 android.googlesource.com

203.208.46.172 cache.pack.google.com

59.24.3.173cache.pack.google.com

保存就ok了。

6,开始同步源码喽

按理说在repo init
....之后使用repo
sync就可以开始下载源码了,但是在下载过程中经常会出现没网速“死”的情况。当然,我修改了/etc/hosts文件之后就再也么有死过。在没网速提
示连接不上时,可以按ctrl+z按键来暂停同步,然后再输入repo
sync就可以了。但尽管这样也不太方便,比如大半夜的谁会守着电脑监督下载情况,源码貌似都要一夜才下好,汗。为此,可以参考http://www.rosoo.net/a/201302/16503.html
在工作目录,我的是~/document/androidSource文件夹下新建一个download.sh脚本文件,里面的内容是:

[html]
view plain
copy

<span style="font-size:18px;color:#FF0000;"> #!/bin/bash

echo "======start repo sync======"

repo sync

while [ $? == 1 ]; do

echo "======sync failed, re-sync again======"

sleep 3

repo sync

done </span>

chomd a+x download.sh让其可以运行,然后终端输入./download.sh就可以开始下载了,用这一步代替官方文档里的repo sync。

当连接不上终端时休息3毫秒然后再自动repo sync,这样就很完美了。

7,下载的源码在哪里

在工作目录~/document/androidSource文件夹下,输入命令 ls -a,可以看到有个.repo文件夹。很显示这是个隐藏文件夹,进去这个目录后查看结果如下:

[html]
view plain
copy

<span style="font-size:18px;">administrator@ubuntu:~/document/androidSource/.repo$ ls

manifests manifests.git manifest.xml projects repo</span>

里面的projects文件夹即是下载的源码:

[html]
view plain
copy

<span style="font-size:18px;">administrator@ubuntu:~/document/androidSource/.repo/projects$ ls

abi build.git development.git external libcore.git prebuilt.git

bionic.git cts.git device frameworks ndk.git sdk.git

bootable dalvik.git docs hardware packages system

</span>

当然我的还么下载完,估计里面的内容还不太全。下面附一个下载时的截图:

http://blog.csdn.net/zhanglongit/article/details/9263009
有点奇怪的时我在repo init -u的时候已经用-b指定下载源码版本为4.0.1_r1, 不知到为啥下载的时候还会出现4.2.x的信息,有时候还会出现2.3.x的相关信息,知道为啥的大神告知一下哈。

据说会有10G左右,源码还在下载。等下载完后用mv .repo repo强制把这个隐藏文件夹弄成可见的看着就方便了。源码下好了,

原创:
http://blog.csdn.net/zhanglongit/article/details/9263009
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: