windows环境下repo下载Android源代码

由于公司的制度的使然,开发环境只能局限于Windows下,所以不得不在Windows下挣扎徘徊。这破逼公司真是手机业界的一朵奇葩,想起来便让人久久不能释怀。

官方推荐Android源码的下载方式是Linux系统上是通过Repo来完成的。Win环境下Repo的使用又依赖Git、Python。源码的下载又涉及到伟大的长城防火墙。所以这篇文章的核心有三个:安装Repo依赖环境、让Repo在Win上面跑起来、使用国内源避开墙。

如果是使用Win10上面用Linux子系统下载,可以参考微软官方的教程(不过依然还是会出问题)。如果直接在Win环境下面,可以参考本教程。

以下是Win下面下载Aosp源代码的步奏。

一、安装git(版本控制工具)https://git-scm.com/download/win 点击下载

安装后

将git添加到环境变量中 C:\Program Files\Git\cmd;

将MinGW添加到环境变量中 C:\Program Files\Git\bin;

二、安装pyton(pyton编辑工具)https://www.python.org/downlo… 点击下载

版本选择2.7+,否则会报异常ypeError: startswith first arg must be bytes or a tuple of bytes, not str

安装后

将Python添加到环境变量中 C:\Program Files\Python27;

三、选择合适的源

所谓的源就是链接。由于官方的源基本处于连接不上的状态,在此我们选择了清华镜像站的源。下面涉及到源的地方全部默认使用清华镜像站的源

Android官方:
https://storage.googleapis.co…

清华大学镜像站:
https://mirrors.tuna.tsinghua…

四、安装Repo

4.1 容易走歪路的Repo

官方提供Repo在Windows跑起来上会报错,我们不建议大家使用官方的Repo。但是我们还是先教大家几种下载Repo的方式,一心只关心源码的同学可以跳过这节

4.1.1 curl url > ~/bin/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添加到了环境变量中,以后敲命令很方便的。

4.1.2 git clone url

这种方式可以下载完整的Repo文件

git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
4.1.3 浏览器

在Windows上最简单的下载方式,把源拷贝到浏览器地址栏中就行。
https://mirrors.tuna.tsinghua…

这种方式和git clone方式得到的文件是一样的

4.2 下载推荐的Repo客户端

如果你下载了原生的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源码

好吧,有些同学实在不想这么折腾,想简单快捷,也不是没有办法的。

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 即可保持同步。 我们强烈建议您保持每天同步,并尽量选择凌晨等低峰时间

七、遗憾的事情

按照上面的教程我们就得到了想要的代码,发个截图给大家参考一下
《windows环境下repo下载Android源代码》

到这里应该编译源代码了,但是我们初始化环境就会发现报错“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吧。

FAQ

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…

    原文作者:杨锘
    原文地址: https://segmentfault.com/a/1190000015279330
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞