果移動端訪問不佳,請使用 ==> Github Pages 版。
本文記錄給 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"