上篇博客记录了在Linux系统(Ubuntu)下搭建Android环境,接下来看下怎么获取Android源码,这里主要以Android4.4的源码为例。

       先附上官网的地址:http://source.android.com/source/initializing.html.在上述地址里对Android的开发环境的搭建和源码的获取等有详细的介绍,按照其讲述过程完全可以搞定。下面我主要介绍我自己的获取的过程,其实是对官网的步骤的精简及我遇到的一些问题,后面还收集了一些在网友遇到的问题。简述如下:


        1.安装两个软件curl和git

           

$ sudo apt-get install curl
                  $ sudo apt-get install git-core


2.建立bin文件夹,添加到环境变量

     

$ mkdir ~/DevelopHelp/bin

          $ gedit ~/.bashrc
          在文件的末尾添加环境变量:
          export PATH=~/DevelopHelp/bin:$PATH

          $ source ~/.bashrc(使环境变量生效)

上述环境变量设置放法只对当前用户有效


     3.下载repo工具,保证其可执行

$ curl http://git-repo.googlecode.com/files/repo-1.13 > ~/DevelopHelp/bin/repo
                 $ chmod a+x ~/DevelopHelp/bin/repo

                

官网提供:

                 $ curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

                 下载路径貌似无法访问。修改为以上路径下载即可。

查看所有repo版本:http://code.google.com/p/git-repo/downloads/list?can=1&q=。下载任何一个版本repo

找不到公钥的问题,折腾了半天也没有解决,若大家有解

                 决该问题的经历,欢迎提供方法及原因。 


4.建一个空目录放Android源代码,可以修改为最大权限,进入该目录

$mkdir ~/Android/Android4.4
$sudo chmod –R 777 ~/Android/Android4.4
$cd ~/Android/Android4.4


5.初始化repo

$ repo init -u https://android.googlesource.com/platform/manifest
 期间需要填写名字和邮箱验证。注册google帐号和gmail邮箱。
 继续,初始化到需要下载的分支。
问题:
这里有可能报出一下错误:
fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
fatal: error [Errno 101] Network is unreachable 
解决办法:
   按官网的步骤一步步执行  
        用浏览器登录https://android.googlesource.com/new-password,并且gmail帐号登录;  
        点击网页上的“允许”,得到类似的结果:  
        
     machine android.googlesource.com login git-<userName>.gmail.com password <password>  
     machine android-review.googlesource.com login git-<userName>.gmail.com password <password>  
         将上边的两行追加到~/.netrc文件结尾。(如果你的linux系统中从来没有建立过此文件,就在home目录下建立.netrc文件,将上面的信息加到新建的文件中去),  
        下载地址的URI更改为https://android.googlesource.com/a/platform/manifest(中间加上了“/a”)。
        因为访问基本是匿名的,为了防止连接过多,对同一IP地址的连接数做了一定的限制。用      gmail帐号进行认证。  
        在公司网络内或者用虚拟机下载的话,会经常遇到这问题。 
接着往下:
$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.4_r1.2
通过下面的网址可以查看想要下载的分支
 https://android.googlesource.com/platform/manifest/+refs
6.执行repo sync 进行同步
$repo sync
$repo sync下载。可以写脚本自动进行,网上比较多,下面提供一个:

#!/bin/bash    
echo "======start repo sync======"    
repo sync    
while [ $? = 1 ]; do    
echo "======sync failed, re-sync again======"    
sleep 3    
repo sync    
done 
 

备注:
下面是整理的一些问题和解决办法,希望大家在评论中也一起总结和讨论。
(1)“A new repo command ( 1.18) is available”          
解决:依照提示输进去就可以了, cp /home/cfy/android/.repo/repo/repo  /home/cfy/bin/repo

(2) error: Failed connect to android.googlesource.com:443;
     Connection refused while accessing 
     https://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  
        等错误信息。 
  解决: 
            编辑/etc/hosts文件  
             $ sudo gedit /etc/hosts  
             增加下面内容,保存              74.125.237.1 dl-ssl.google.com
            74.125.71.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
注:
 
    
   
     google source 的ip 是会改动的,所以使用如下命令:
     $ nslookup googlesource.com
     $ nslookup android.googlesource.com
 
        查看实际ip,将上面得到ip地址按照74.125.31.82  android.googlesource.com 格式加到/etc/hosts 
   

(3)   fatal: '../platform/abi/cpp.git' does not appear to be a git repository
        fatal: The remote end hung up unexpectedly
        error: Cannot fetch platform/abi/cpp解决:
            在~/JK_WorkSpace/Android/AndroidResource/Android4.4/.repo下的
            manifest.xml里找到fetch属性,改成fetch="git://Android.git.linaro.org"即可
(4)   fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
        fatal: error [Errno 101] Network is unreachable:
             代理环境变量造成的问题,用下列指令清除代理:
              unset HTTP_PROXY

待续...