問題背景
困擾博主近一個月的問題,導致近一個月沒辦法進行 Android 開發的問題終於解決了!
問題錯誤特征
Could not run phased build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-5.5.1-all.zip'
A problem occurred configuring root project 'PlanAssistant'.
Could not resolve all artifacts for configuration ':classpath'.
Could not resolve com.android.tools.build:gradle:3.5.3.
Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.5.3/gradle-3.5.3.pom'.
Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.5.3/gradle-3.5.3.pom'.
- Read timed out
就是這一句 Read timed out
,在過去的一個月里,我天天都看到這句報錯,始終找不到比較好的解決方案。經過各種 Google,Baidu,bing,發現解決方案大致分為兩種:
- 為 Gradle 配置 socks 代理
- 配置阿里雲 maven 倉庫源
問題解決
嘗試了以上兩種方法之后全都無法解決,依然是那一句可惡的 Read timed out.終於在今天我發現了自己存在的問題:全局Gradle代理配置錯有問題。

當我為 Android Studio 配置了代理后,編譯時詢問是否要為 Gradle 也配置代理,年少無知的我果斷選擇了 配置 ,但事實證明,這樣的方式配置的是 HTTP Proxy
,但是我的 ssr
運行的是 socks
協議,這么一來就算我如何操作,都無法擺脫Read timed out
的詛咒了。

解決方法很簡單,找到 gradle
的位置,將其中 gradle.properties
文件中關於 HTTP Proxy
的部分全部注釋:
## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Mon Jan 27 12:42:16 CST 2020
#systemProp.https.nonProxyHosts=localhost, 127.0.0.0/8, \:\:1
#systemProp.http.proxyHost=127.0.0.1
#systemProp.https.proxyPort=1080
#systemProp.https.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1080
這樣一來,就可以通過重新配置 socks
代理或是改用阿里雲 maven 庫的方式解決問題。如果需要使用socks
代理,需要在文件中使用這一句:
org.gradle.jvmargs=-Xmx4536m -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080
如果使用 HTTP
代理就是以上導致我出錯的部分:
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=1080
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=1080
代理總結
下面根據個人理解對 Androoid Studio 中的代理進行一下小結,代理分兩種
- Android Studio 自身
- Gradle
配置代理時二者也是分開的,為 Android Studio 配置代理就可以在下載 SDK,IDE更新時走代理;而 Gradle 代理就是在進行編譯時走代理,配置方法在上文已經詳述了 Gradle 的配置,想為 Android Studio 配置代理只需在設置中配置即可(注意區分代理協議)。

下面作一次搬運工,將阿里雲官網給出的配置阿里雲 maven 庫 的方法在此一並敘述,方便需要的人。
配置國內 maven 庫
在build.gradle文件中加入以下代碼:
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenLocal()
mavenCentral()
}
}
ps: 倉庫位置順序有講究,如果想要阿里雲庫優先就把它放在靠前的位置。更多信息見 阿里雲文檔/公共代理庫
想要告別 Read timed out,本文介紹了兩種方法,一種需要您會點魔法,第二種就是配置國內倉庫源,方法在文中都有介紹,若本文有不嚴謹之處歡迎在我的博文下留言。