為 Android Studio 或者 Android 項目設(shè)置代理

如果移動端訪問不佳,請使用 ==> 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種使用方式:

  1. 添加代理配置到當前項目或者全局環(huán)境下的 gradle.properties
  2. 命令行方式

方式一:修改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)系

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,716評論 0 5
  • 前言 這三個知識點不難,但經(jīng)常用到,如果不看官方文檔,有時候并不知道怎么使用,出現(xiàn)問題也不知道原因,所以有必要做一...
    十思葉閱讀 11,170評論 2 6
  • 一、Python簡介和環(huán)境搭建以及pip的安裝 4課時實驗課主要內(nèi)容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 6,338評論 0 10
  • http://www.itdecent.cn/p/7c288a17cda8 總的來說,Android的系統(tǒng)體系結(jié)...
    燕京博士閱讀 1,421評論 0 6
  • 嗯 這也許不是第一次了。 到今天好像下雨,大家來的都比較晚,我的門卡放公司了,所以我現(xiàn)在坐在樓下星巴克喝濃郁摩卡。...
    mmmmmming閱讀 156評論 1 1

友情鏈接更多精彩內(nèi)容