Flutter编译报错Connection timed out: connect

背景:用Android Studo 创建了Flutter项目,编译运行报错java.net.ConnectException: Connection timed out: connect

我自己的环境:

windows11

Android Studio

Flutter

截图如下:

将错误日志展开之后:

java 复制代码
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
	at 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

以上两种方法都可以解决该问题。

相关推荐
yuanlaile6 小时前
纯Dart Flutter库适配HarmonyOS
flutter·华为·harmonyos·flutter开发鸿蒙·harmonyos教程
yuanlaile6 小时前
Flutter开发HarmonyOS 鸿蒙App的好处、能力以及把Flutter项目打包成鸿蒙应用
flutter·华为·harmonyos·flutter开发鸿蒙
zacksleo7 小时前
鸿蒙原生开发手记:04-一个完整元服务案例
flutter
Calvin88082813 小时前
Android Studio 的革命性更新:Project Quartz 和 Gemini,开启 AI 开发新时代!
android·人工智能·android studio
tanghongchang12320 小时前
Mac Android studio 升级LadyBug 版本,所产生的bug
macos·android studio·ladybug
叶羽西1 天前
Android Studio打开一个外部的Android app程序
android·ide·android studio
jcLee951 天前
Flutter/Dart:使用日志模块Logger Easier
flutter·log4j·dart·logger
tmacfrank1 天前
Flutter 异步编程简述
flutter
tmacfrank1 天前
Flutter 基础知识总结
flutter
叫我菜菜就好1 天前
【Flutter_Web】Flutter编译Web第三篇(网络请求篇):dio如何改造方法,变成web之后数据如何处理
前端·网络·flutter