Flutter新建项目运行报错Exception in thread "main" java.net.ConnectException: Connection timed out: connect

问题:Flutter create新建项目之后运行,一直报错显示Exception in thread "main" java.net.ConnectException: Connection timed out: connect

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)    
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)     
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)   
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:666)
        at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)  
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
        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)
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done                        26.4s
Exception: Gradle task assembleDebug failed with exit code 1

其实主要估计就是网络超时问题,我上网搜了一波发现都建议
需要修改项目下 android/build.gradle 和 flutter 安装目录flutter/packages/flutter_tools/gradle/flutter.gradle 两个文件中 buildscript 和allprojects 中的

google()
jcenter()

替换成阿里云镜像

maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url'http://maven.aliyun.com/nexus/content/groups/public' }

但是我替换后还是回显示同样的错误,开始思考其实之所以这样修改,是因为更快下载到需要的gradle.zip文件,所以我就直接在http://services.gradle.org/distributions/这里下载我所需要的grade文件直接引用本地的来运行,结果成功运行了

步骤一:到你项目里面的gradle-wrapper.properties文件里面查找到你所需要的版本,我这里是gradle-6.7-all.zip

 

 步骤二:到这里http://services.gradle.org/distributions/找到对应的版本下载

 

 步骤三:找到你的.grade地方,我电脑所在是 C:\Users\husk\.gradle ,然后将刚刚下载成功的zip包放在该目录下(其实你喜欢放在哪都无所谓,反正到时候引入本地这个zip就好)

 

 

步骤四:到你项目里面的gradle-wrapper.properties文件里面将原来的 distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip 换乘本地的

 

 

然后运行,运行成功了!!!

 

posted @ 2021-05-14 10:38  KIU的博客  阅读(8484)  评论(1编辑  收藏  举报