开发杂闻

MAC下载android源码(不翻墙)

2017-10-08  本文已影响0人  李全栋

本文参考Mac 下载 编译 debug Android 源码一文,介绍使用清华大学开源软件镜来下载android源码的方式,无需翻墙。

1 repo工具安装

repo下载,执行如下命令

curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo

将repo安装目录添加到path,修改~/.bash_profile文件。

export PATH=$PATH:~/bin

修改REPO_URL,不然在执行过程中会提示无法连接到 gerrit.googlesource.com,在~/.bash_profile文件下添加

export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

要使上述的修改立即生效,执行

source ~/.bash_profile

2 创建磁盘镜像

建立一个大小写敏感的磁盘镜像,之所以需要大小写敏感,是因为android源码编译需要,否则无法进行编译。磁盘大小选择40GB。
执行以下命令创建磁盘镜像

hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g -volname android ~/android

这样就在当前用户的home目录下创建了一个名为android.sparseimage的磁盘,接着执行以下命令挂载该磁盘

hdiutil attach ~/android.sparseimage -mountpoint /Volumes/android

3 下载源码

本文以下载android-6.0.1_r77为示例,先在新建的磁盘中创建一个android-6.0.1的文件夹,在该文件夹下执行如下命令

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-6.0.1_r77

执行完成后会生成一个名为.repo文件夹,修改其manifests文件夹下的default.xml文件,在每个project标签中添加clone-depth="1"配置,意思是下载源码时只下载最新的一层代码,不下载历史提交的记录,这样可以提升下载速度并节省磁盘空间。


image.png

使用如下python脚本(名称为modify_default.py)对default.xml文件进行修改

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

fileName = sys.argv[1]
file_object = open(fileName,'r')

change_content = ''
while 1:
    line = file_object.readline()
    if not line.__contains__('clone-depth') and line.__contains__('<project'):
        try:
            if line.__contains__('/>'):
                endpos = line.index("/>")
            else:
                endpos = line.index(">")
            line = line[0:endpos] + ' clone-depth="1"' + line[endpos: line.__len__()]
            
            pass
        except Exception, e:
            print(e)
            pass

    change_content += line
    if not line:
        break
    pass  # do something
write_file = open(fileName, 'w')
write_file.write(change_content)
write_file.close()
file_object.close()

执行python脚本

python modify_default.py [default文件完成路径]

最后就可以下载源码了,由于下载时间比较久,防止下载过程中出现断网等异常情况,使用如下脚本进行失败重试。

#!/bin/bash 
#FileName  sync.sh

repo sync 
while [ $? = 1 ]; do 
echo "================sync failed, re-sync again =====" 
sleep 3 
repo sync 
done

将该脚本放在源码文件夹下,并执行

sh sync.sh
上一篇 下一篇

猜你喜欢

热点阅读