在Unix终端下配置Proxy,加快Android Studio构建

Gradle下载jar包慢到飞起?Github同步一半断了?Docker下载频繁报错?是时候为你的终端统一设置一个代理了。本文以Mac(Unix平台)与HTTP_PROXY为例,讲解如何在终端下进行配置代理。

本文章属于笔记类型,质量远不如技术类文章,但是网上关于此内容的靠谱信息实在不多,故记录下来。

1. 选择Proxy

首先,要购买挑选一个Proxy,购买前需要注意服务商需要能够提供Http Proxy,如下,某良心服务商提供了多种配置方法

Proxy_list2. 配置终端

    打开终端

     open -t  ~/.bash_profile

    .bash_profile 可以理解为Term打开后能够自动执行的脚本文件

    在文本中添加如下内容

#ideaexport JAVA8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Homeexport JAVA7_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Homeexport JAVA_HOME=$JAVA8_HOME#Androidexport ANDROID_HOME=/usr/local/Cellar/android-sdk/24.4.1_1export ANDROID_SDK=$ANDROID_HOMEexport ANDROID_NDK=/usr/local/Cellar/android-ndk/r10e#http proxyHOST=www.xxx.comPORT=xxxx#if use lantern, removes comments# LANTERN=127.0.0.1 # HOST=$LANTERN# PORT=8787export http_proxy=http://$HOST:$PORTexport all_proxy=$http_proxyexport https_proxy=$http_proxyexport ftp_proxy=$http_proxyexport dns_proxy=$http_proxyexport rsync_proxy=$http_proxyexport no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,.coding.net,192.168.99.100"#java(works fine on gradle/mvn/android)export JAVA_OPTS="-Dhttp.proxyHost=$HOST -Dhttp.proxyPort=$PORT -Dhttps.proxyHost=$HOST -Dhttps.proxyPort=$PORT"#gradle will use `JAVA_OPTS` as concatexport GRADLE_OPTS="-Dgradle.user.home=$HOME/.gradle"#mavenexport MAVEN_OPTS=$JAVA_OPTScurl ip.cn

    保存配置

     source ~/.bash_profile

    该方法配置好后,在IDEA等工具的终端下同样有用

3. 查看效果

重新打开终端后,可以发现系统自动执行了curl的命令,此网站会自动告诉了你当前的IP,我们也可以使用wget进行SpeedTest

wget -O /dev/null http://speedtest-sfo1.digitalocean.com/10mb.test

speed_test_digital_ocean

最后,体验Git,Brew,Gradle等工具的飞速下载吧!

注意:

    Mac的Docker环境下需要在虚拟机/VPS中进行配置Proxy亲测Go,Git,Brew,Curl,Wget,NPM是可以正常使用的,可以达到物理带宽不支持git://svn://,那两个非要自己搞一套,必须搞一个包装类,所以算了吧如果需要设置内网地址的正则(比如192,10,172),目前暂时没有完美的方案,只能枚举并手动加入no_proxy中Gradle是java相关的,用的不是curl,所以需要GRADLE_OPTS作为环境变量,注意这个环境变量只是终端的,而不是IDE下的

4. AndroidStudio项目实例

打开终端

git clone --depth=1 https://github.com/Yalantis/StarWars.Android.gitcd StarWars.Android/./gradlew build --debug

由于目前有高速Proxy,所以下载依赖非常快,等成功后,打开AndroidStudio导入当前现成的项目,就可以复用缓存而不用卡在Building对话框了

5. 使用Ping进行测速

#proxy arraySERVER=(www.xx.com www.xx2.com)for s in ${SERVER[*]}do    echo testing $s    rank[$counter]=$(ping -c 4 $s |awk -F '/' 'END {print $5}')donemin=0for i in "${!rank[@]}"; do    if [ $(bc <<< "${rank[$min]}>${rank[$i]}") == 1 ];     then        let min=$i    fidoneecho "best proxy is ${SERVER[$min]}, ping is ${rank[$min]}"HOST=${SERVER[$min]}PORT=xxxx# 进行export

更多Android Studio相关内容可以看看以下的有用链接:

在Ubuntu 15.04下安装Android Studio http://www.68idc.cn/Linux/2015-06/119318.htm

Android Studio 2.0中的模拟器已提速50倍 http://www.68idc.cn/Linux/2015-11/125481.htm

Ubuntu 12.04(64位)安装Android Studio 全过程 http://www.68idc.cn/Linux/2013-05/84812.htm

Android Studio v0.1尝鲜 http://www.68idc.cn/Linux/2013-05/84681.htm

Android Studio使用教程 http://www.68idc.cn/Linux/2013-05/84579.htm

Android Studio开发指南 http://www.68idc.cn/Linux/2013-05/84543.htm

Android Studio设置主题 和 不支持中文的问题解决方法 http://www.68idc.cn/Linux/2013-05/84488.htm

Android Studio 下载安装以及不能打开的解决办法 http://www.68idc.cn/Linux/2013-05/84409.htm

Android Studio安装使用图文教程 http://www.68idc.cn/Linux/2014-09/106914.htm

Ubuntu上安装Android Studio 1.3(谷歌 Android IDE 开发) http://www.68idc.cn/Linux/2015-08/121004.htm

Android Studio 的详细介绍:请点这里Android Studio 的下载地址:请点这里

微风吹过,海面上金光闪闪,泛起一道道美丽的浪花,

在Unix终端下配置Proxy,加快Android Studio构建

相关文章:

你感兴趣的文章:

标签云: