蒙国造博客

解决 flutter run 很慢或者下载 gradle 错误:Exception in thread “main” java.net.SocketException: Connection reset

这个问题是因为国内网络无法正常下载 gradle 引起的,不管你是啥版本,都一样。

详细的错误如下:

 [ +176 ms] Downloading https://services.gradle.org/distributions/gradle-6.7-all.zip
 [+5206 ms] Exception in thread "main" java.net.SocketException: Connection reset
 [        ]      at java.net.SocketInputStream.read(SocketInputStream.java:210)
 [        ]      at java.net.SocketInputStream.read(SocketInputStream.java:141)
 [        ]      at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
 [        ]      at sun.security.ssl.InputRecord.read(InputRecord.java:503)
 [        ]      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
 [        ]      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
 [        ]      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
 [        ]      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
 [        ]      at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
 [        ]      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
 [        ]      at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
 [        ]      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
 [        ]      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
 [        ]      at org.gradle.wrapper.Download.downloadInternal(Download.java:58)
 [        ]      at org.gradle.wrapper.Download.download(Download.java:44)
 [        ]      at org.gradle.wrapper.Install$1.call(Install.java:61)
 [        ]      at org.gradle.wrapper.Install$1.call(Install.java:48)
 [        ]      at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:65)
 [        ]      at org.gradle.wrapper.Install.createDist(Install.java:48)
 [        ]      at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128)
 [        ]      at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
 [   +3 ms] Running Gradle task 'assembleDebug'… (completed in 5.4s)
 [        ] [!] Gradle threw an error while downloading artifacts from the network. Retrying to download…
 [   +4 ms] Exception: Gradle task assembleDebug failed with exit code 1
 [   +2 ms] "flutter run" took 15,740ms.

这是因为我们在 android/gradle/wrapper/gradle-wrapper.properties 文件中指定从 distributionUrl 下载 gradle,一般是从 gradle 官网下载,如: https://services.gradle.org/distributions/gradle-6.7-all.zip,但是国内网络无法从这个地址下载,就会导致上面的问题,不管你指定的是 gradle 6.0 还是 6.7 还是别的版本。

解决办法:使用国内的地址,如下,

distributionUrl=https\://code.aliyun.com/kar/gradle-all-zip/raw/master/gradle-6.7-all.zip

退出移动版