34

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

 5 years ago
source link: https://www.tuicool.com/articles/nEnEFzy
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本文记录给 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"

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK