启动 flutter项目时报Could not find com.android.tools.build:gradle:3.2.1.

Error running Gradle:
ProcessException: Process "D:\Android\APP path\flutter_app2\flutter_app2\android\gradlew.bat" exited abnormally:

> Configure project :app

Project evaluation failed including an error in afterEvaluate {}. Run with --stacktrace for details of the afterEvaluate {} error.

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\Android\APP path\flutter_app2\flutter_app2\android\app\build.gradle' line: 25

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not resolve all artifacts for configuration 'classpath'.
   > Could not find com.android.tools.build:gradle:3.2.1.
     Searched in the following locations:
       - https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom
       - https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.jar
       - http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom
       - http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.jar
     Required by:
         unspecified:unspecified:unspecified

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full
insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s
Picked up _JAVA_OPTIONS: -Djava.NET.preferIPv4Stack=true
  Command: D:\Android\APP path\flutter_app2\flutter_app2\android\gradlew.bat app:properties

Please review your Gradle project setup in the android/ folder.

大致意思是说:运行Gradle时出错,导致构建flutter构建失败

原因是因为获取不到 com.android.tools.build:gradle:3.2.1

原因一:

获取不到的原因一般是因为在中国没办法访问谷歌资源(有外网,等的除外),此时的解决办法是 改用阿里的镜像

分两步:

  • 第一步:修改掉项目下的 android目录下的 build.gradle 文件,把 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' }
    
    如下:
    buildscript {
        repositories {
    //        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' }
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:3.2.1'
        }
    }
    
    allprojects {
        repositories {
    //        google()
    //        jcenter()
    //        maven {
    //            url 'https://google.bintray.com/exoplayer/'
    //        }
    		// 添加
            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' }
        }
    }
    
    rootProject.buildDir = '../build'
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
    }
    subprojects {
        project.evaluationDependsOn(':app')
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    
  • 第二步:修改 Flutter SDK 包下的 flutter.gradle 文件
    路径根据你的sdk存放位置,例如我的是D:\SDK\Flutter\packages\flutter_tools\gradle\flutter.gradle
    同样将 google()jcenter() 修改为阿里的链接
    maven { url 'https://maven.aliyun.com/repository/jcenter' }
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    
    如下:
    buildscript {
        repositories {
            //google()
            //jcenter()
            // 添加
    		maven { url 'https://maven.aliyun.com/repository/jcenter' }
    		maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.2.1'
        }
    }
    

至此,在国内没办法访问谷歌资源的问题就解决了

然而,我的问题并没解决

仔细看报错信息后,发现找不到以下资源地址

    -  https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom
    -  https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.jar
    -  http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom
    -  http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.jar

看到这里说明阿里链接是配置上了的,之所以找不到是因为gradle-3.2.1这个3.2.1版本太高了,应该是最新的

阿里连接中的gradle最高版本好像才3.1.2,所以获取不到

所以,解决办法是将项目下的android目录下的build.gradle文件和flutter SDK包下的flutter.gradle文件中的gradle版本改为阿里镜像中存在的版本

	dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }

来自于 https://blog.csdn.net/qq_33684444/article/details/86656301

你可能感兴趣的:(Exception,Flutter)