背景:用Android Studo 创建了Flutter项目,编译运行报错java.net.ConnectException: Connection timed out: connect
我自己的环境:
windows11
Android Studio
Flutter
截图如下:
将错误日志展开之后:
Exception in thread "main" java.net.ConnectException: Connection timed out: connectat java.base/sun.nio.ch.Net.connect0(Native Method)at java.base/sun.nio.ch.Net.connect(Net.java:579)at java.base/sun.nio.ch.Net.connect(Net.java:568)at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)at java.base/java.net.Socket.connect(Socket.java:633)at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:304)at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:174)at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183)at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:531)at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:636)at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:266)at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:380)at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:193)at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242)at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128)at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:179)at java.base/sun.net.www.protocol.http.HttpURLConnection.followRedirect0(HttpURLConnection.java:2902)at java.base/sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2811)at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1927)at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)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)
Exception: Gradle task assembleDebug failed with exit code 1
得知是gradle报错。
解决办法:
法 一、
1、打开文件夹 C:\Users\haipi.gradle\wrapper\dists
2、查看里面已经下载的gradle版本。
3、将项目中 \android\gradle\wrapper\gradle-wrapper.properties该文件中的gradle版本改成已经下载好的版本
比如:
将
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
改成
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
法 二、
1、查看项目需要的gradle版本。
路径: \android\gradle\wrapper\gradle-wrapper.properties该文件中的gradle版本
2、进入gradle官网下载该版本
h ttps://gradle.org/releases/?_gl=11qa9v1k_gaNTI3MTI1ODEuMTY4OTE2MDQyNg.._ga_7W7NC6YNPT*MTcwNTU4NTk1OC40LjAuMTcwNTU4NTk1OC42MC4wLjA.
3、等指定版本的gradle下载完成之后,将该zip包复制到 C:\Users\haipi.gradle\wrapper\dists下
4、解压该压缩包
5、修改项目的gradle-wrapper.properties文件
将gradle改为采用本地文件
比如:
将
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
改成
distributionUrl=file:///C:/Users/haipi/.gradle/wrapper/dists/gradle-7.5-all.zip
以上两种方法都可以解决该问题。