原本项目可以正常跑起来,在安卓真机上正常运行的,后来不知道怎么滴,突然就跑不起来了,但是在iOS设备上可以正常运行,后续判断主要是版本不匹配导致的,以及无法访问google服务。
话不多说,先来看看问题的呈现
具体问题呈现如下:
Running Gradle task 'assembleDebug'...Exception in thread "main" java.net.SocketException: Connection reset at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186) at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140) at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:478) at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:472) at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:160) at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:111) at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1426) at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1336) at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:450) at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421) at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:572) at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:197) at java.base/sun.net.www.protocol.http.HttpURLConnection.followRedirect0(HttpURLConnection.java:2787) at java.base/sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2699) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1854) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250) 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)Retrying Gradle Build: #2, wait time: 200ms[!] Gradle threw an error while downloading artifacts from the network.
主要解决方案如下:
若是因网络原因无法访问google服务,可以使用国内镜像代替,主要修改之处为:
android文件夹下build.gradle文件修改
将下面两行代码进行注释
google()mavenCentral()
新增阿里云镜像如下
maven { url 'https://maven.aliyun.com/repository/google' }maven { url 'https://maven.aliyun.com/repository/jcenter' }maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
修改gradle版本,android/gradle/wrapper/gradle-wrapper.properties,具体版本可查看
https://services.gradle.org/distributions/
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
项目运行成功的话,~/.gradle/wrapper/dist/gradle-7.2-all/路径下应是如此
若该路径下一直是下方这样的话,则表示gradle相应的版本未下载成功
此时,你可以自己去https://services.gradle.org/distributions/这里找到你要下载的版本手动下载下来,直接用.zip替换上面的两个就好了
此处另外需要关注两件事情,其一是java版本与gradle版本匹配
其二是gradle版本与gradle插件版本匹配兼容
android/build.gradle中classpath 'com.android.tools.build:gradle:4.1.0' 指的就是gradle插件版本,andriod/gradle/wrapper/gradle-wrapper.properties中就是gradle插件所需的gradle版本
flutter sdk路径下,如/Users/fl-10026268/fvm/versions/3.0.5/packages/flutter_tools/gradle/flutter.gradle文件修改
buildscript { repositories { // 注释 // google() // mavenCentral() // 新增镜像 maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/jcenter' } maven { url 'https://maven.aliyun.com/nexus/content/groups/public' } } dependencies { classpath 'com.android.tools.build:gradle:4.1.0' }}
/// https://storage.flutter-io.cn 替换 https://storage.googleapis.comprivate static final String DEFAULT_MAVEN_HOST = "https://storage.flutter-io.cn"; /// "https://storage.googleapis.com";
flutter sdk路径下,如/Users/fl-10026268/fvm/versions/3.0.5/packages/flutter_tools/gradle/resolve_dependencies.gradle文件修改
// http://download.flutter.io 替换 $storageUrl/download.flutter.iorepositories { google() mavenCentral() maven { url "http://download.flutter.io" // url "$storageUrl/download.flutter.io" }}
flutter sdk路径下,如/Users/fl-10026268/fvm/versions/3.0.5/packages/flutter_tools/lib/src/http_host_validator.dart修改
/// Common Flutter HTTP hosts.const String kPubDevHttpHost = 'https://pub.flutter-io.cn/'; /// https://pub.dev/const String kgCloudHttpHost = 'https://storage.flutter-io.cn/'; /// https://cloud.google.com//// MacOS specific required HTTP hosts.const List macOSRequiredHttpHosts = [ 'https://cocoapods.org/',];/// Android specific required HTTP hosts.List androidRequiredHttpHosts(Platform platform) { return [ // If kEnvCloudUrl is set, it will be used as the maven host if (!platform.environment.containsKey(kEnvCloudUrl)) 'https://dl.google.com/dl/android/maven2/', // 'https://maven.aliyun.com/repository/google/', ];}
通过以上几个步骤的修改,基本上就可以解决此问题了,因本人不是安卓开发,我在这个问题上耽误了好些天,终于解决让项目跑起来了,特在此总结一下经验,希望后来人不会因此耽误太多时间。
来源地址:https://blog.csdn.net/fangli11223344/article/details/128835228