如果移動端訪問不佳,請使用 ==> Github Pages 版。
本文記錄給 Android Studio 設(shè)置代理,并添加白名單。適用于 Jenkins 等 CI/CD 環(huán)境。有修改配置文件和命令行2種方式。
前置環(huán)境:一個 HTTP 代理
- Host:xx.xx.xx.xx
- Port:xxxx
- User:userid
- Password:password
需要加入白名單的域名:*.nonproxydomains.com
配置
有2種使用方式:
- 添加代理配置到當前項目或者全局環(huán)境下的
gradle.properties - 命令行方式
方式一:修改gradle.properties
如果只想修改當前項目,則修改對象是當前項目下的 gradle.properties 文件
如果想修改后對所有使用 gradle 構(gòu)建的項目都起作用,則修改當前用戶下 .gradle/gradle.properties 文件。對于 類Unix系統(tǒng)(Linux、macOs) 是 ~/.gradle/ 目錄(GRADLE_USER_HOME),Windows 是 系統(tǒng)盤下的 /User/XXX/.gradle/ 目錄。
要添加的內(nèi)容如下:
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 打包的完整命令,然后根據(jù)平臺差異做一下說明(以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
## 換行看下參數(shù),和配置文件的參數(shù)對應(yīng)
# -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 中的 | 符號,命令行使用時可能因為平臺差異、字符轉(zhuǎn)義問題需要特殊處理。
當然,如果按照上面的方式?jīng)]有問題,則不需要特殊處理。
對于 Unix/Linux 系統(tǒng):管道字符 | 可能需要反斜杠 \ 來轉(zhuǎn)義,使它不被解釋為 shell 管道。
-Dhttp.nonProxyHosts=*.nonproxydomains.com|localhost
# 當上面的使用方式有問題時改為:
-Dhttp.nonProxyHosts=*.nonproxydomains.com\|localhost
對于 Windows 系統(tǒng): | 可能需要反斜杠 ^ 來轉(zhuǎn)。
-Dhttp.nonProxyHosts=*.nonproxydomains.com|localhost
# 當上面的使用方式有問題時改為:
-Dhttp.nonProxyHosts=*.nonproxydomains.com^|localhost
對于使用字符串(雙引號包裹)時,也需要轉(zhuǎn)義處理
-Dhttp.nonProxyHosts=*.nonproxydomains|localhost
# 作為字符串處理時
-Dhttp.nonProxyHosts="*.nonproxydomains\|localhost"
如果有什么問題或者意見,可以通過下面的方式和我聯(lián)系