为 Android Studio 或者 Android 项目设置代理

栏目: IOS · Android · 发布时间: 4年前

内容简介:本文记录给 Android Studio 设置代理,并添加白名单。适用于 Jenkins 等 CI/CD 环境。有修改配置文件和命令行2种方式。前置环境:一个 HTTP 代理需要加入白名单的域名:*.nonproxydomains.com

本文记录给 Android Studio 设置代理,并添加白名单。适用于 Jenkins 等 CI/CD 环境。有修改配置文件和命令行2种方式。

前置环境:一个 HTTP 代理

  • Host:xx.xx.xx.xx
  • Port:xxxx
  • User:userid
  • Password:password

需要加入白名单的域名:*.nonproxydomains.com

配置

有2种使用方式:

gradle.properties

方式一:修改 gradle.properties

如果只想修改当前项目,则修改对象是当前项目下的 gradle.properties 文件

如果想修改后对所有使用 gradle 构建的项目都起作用,则修改当前用户下 .gradle/gradle.properties 文件。对于 类Unix系统(Linux、macOs) 是 ~/.gradle/ 目录(GRADLE_USER_HOME),Windows 是 系统盘下的 /User/XXX/.gradle/ 目录。

要添加的内容如下:

systemProp.http.proxyHost=xx.xx.xx.xx
systemProp.http.proxyPort=xxxx
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=*.nonproxydomains.com|localhost

systemProp.https.proxyHost=xx.xx.xx.xx
systemProp.https.proxyPort=xxxx
systemProp.https.proxyUser=userid
systemProp.https.proxyPassword=password
systemProp.https.nonProxyHosts=*.nonproxydomains.com|localhost

方式二:命令行方式

首先看个 Android 打包的完整命令,然后根据平台差异做一下说明(以macOS下为例):

cd projectRoot
./gradlew :app:assembleDebug -Dhttp.proxyHost=xx.xx.xx.xx -Dhttp.proxyPort=xxxx -Dhttp.nonProxyHosts=*.nonproxydomains.com|localhost -Dhttp.proxyUser=userid -Dhttp.proxyPassword=password -Dhttps.proxyHost=xx.xx.xx.xx -Dhttps.proxyPort=xxxx -Dhttps.nonProxyHosts=*.nonproxydomains.com|localhost -Dhttps.proxyUser=userid -Dhttps.proxyPassword=password
## 换行看下参数,和配置文件的参数对应
# -Dhttp.proxyHost=xx.xx.xx.xx 
# -Dhttp.proxyPort=xxxx 
# -Dhttp.nonProxyHosts=*.nonproxydomains.com|localhost 
# -Dhttp.proxyUser=userid 
# -Dhttp.proxyPassword=password

# -Dhttps.proxyHost=xx.xx.xx.xx 
# -Dhttps.proxyPort=xxxx 
# -Dhttps.nonProxyHosts=*.nonproxydomains.com|localhost 
# -Dhttps.proxyUser=userid 
# -Dhttps.proxyPassword=password

这里特别说下 http.nonProxyHosts=*.nonproxydomains.com|localhost 中的 | 符号,命令行使用时可能因为平台差异、字符转义问题需要特殊处理。

当然,如果按照上面的方式没有问题,则不需要特殊处理。

对于 Unix/Linux 系统:管道字符 | 可能需要反斜杠 \ 来转义,使它不被解释为 shell 管道。

-Dhttp.nonProxyHosts=*.nonproxydomains.com|localhost
# 当上面的使用方式有问题时改为:
-Dhttp.nonProxyHosts=*.nonproxydomains.com\|localhost

对于 Windows 系统: | 可能需要反斜杠 ^ 来转。

-Dhttp.nonProxyHosts=*.nonproxydomains.com|localhost
# 当上面的使用方式有问题时改为:
-Dhttp.nonProxyHosts=*.nonproxydomains.com^|localhost

对于使用字符串(双引号包裹)时,也需要转义处理

-Dhttp.nonProxyHosts=*.nonproxydomains|localhost
# 作为字符串处理时
-Dhttp.nonProxyHosts="*.nonproxydomains\|localhost"

如果有什么问题或者意见,可以通过下面的方式和我联系


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大数据

大数据

涂子沛 / 广西师范大学出版社 / 2013-4-1 / 49.90元

公布官员财产美国是怎么做的,美国能让少部人腐败起来吗,美国式上访是怎么回事,凭什么美国矿难那么少,全民医改美国做得到吗,美国总统大选有什么利器才能赢,下一轮全球洗牌我们世界工厂会被淘汰吗…… 除了上帝,任何人都必须用数据来说话。 大数据浪潮,汹涌来袭,与互联网的发明一样,这绝不仅仅是信息技术领域的革命,更是在全球范围启动透明政府、加速企业创新、引领社会变革的利器。现代管理学之父德鲁克有......一起来看看 《大数据》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具