flutter 报错解决

报错(Error)

首先你要确保你的 Flutter 环境是没有问题的,可以 flutter doctor 检查一下:
在这里插入图片描述

首先声明一下,我的 Flutter 开发环境是在 Window10 下 AS 工具中编译运行的,项目是一个 demo 。今天创建并运行新项目时突然报错:
在这里插入图片描述
这里贴出代码,便于 SEO 蜘蛛程序检索,帮助更多 Flutter 开发者。

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not download kotlin-gradle-plugin.jar (org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50)
      > Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.3.50/kotlin-gradle-plugin-1.3.50.jar'.
         > Could not GET 'https://d29vzk4ow07wi7.cloudfront.net/635d624afedac53fc9eae611c40825a1ea05dda3699f39aa49432ba57d871f26?response-content-disposition=attachment%3Bfilename%3D%22kotlin-gradle-plugin-1.3.50.jar%22&Policy=eyJTdGF0ZW1lbnQiOiBbeyJSZXNvdXJjZSI6Imh0dHAqOi8vZDI5dnprNG93MDd3aTcuY2xvdWRmcm9udC5uZXQvNjM1ZDYyNGFmZWRhYzUzZmM5ZWFlNjExYzQwODI1YTFlYTA1ZGRhMzY5OWYzOWFhNDk0MzJiYTU3ZDg3MWYyNj9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPWF0dGFjaG1lbnQlM0JmaWxlbmFtZSUzRCUyMmtvdGxpbi1ncmFkbGUtcGx1Z2luLTEuMy41MC5qYXIlMjIiLCJDb25kaXRpb24iOnsiRGF0ZUxlc3NUaGFuIjp7IkFXUzpFcG9jaFRpbWUiOjE1Nzc0Mzk2MTZ9LCJJcEFkZHJlc3MiOnsiQVdTOlNvdXJjZUlwIjoiMC4wLjAuMC8wIn19fV19&Signature=iGadNAXiUAk8YmhdDzs6jOUY8OS3s1yyBvjPvrJvIZDMdlVD4Oo6q~Fu8mZfY4HiNSE6bSuxPnBfpEMZX8Vj9I5QJhV7916BUZtMkZ12KFNcDPLXskrFFMMuvSa8N8ySNmJDGdPDDJnYHT6~yWPx-xN-Q50V4AzjGYlqUAfma9KgOxnl-L4F8uzcXzbTMeWRPuNMP8c62x7EA~Ei0d5T2q1agsO51z3gy~4R6C0iNX80d~mzImM7mU4GUwktZjvn1SpXA5W6oWkiOlWRwMLBEmnHQ7KdcaV32XyPqjtWLORAoqUurfvWIFw7icoUTlcJl7lTb8tEVXNmd6Ibbyy43g__&Key-Pair-Id=APKAIFKFWOMXM2UMTSFA'.
            > d29vzk4ow07wi7.cloudfront.net
   > Could not download protobuf-java.jar (com.google.protobuf:protobuf-java:3.4.0)
      > Could not get resource 'https://jcenter.bintray.com/com/google/protobuf/protobuf-java/3.4.0/protobuf-java-3.4.0.jar'.
         > Could not GET 'https://d29vzk4ow07wi7.cloudfront.net/dce7e66b32456a1b1198da0caff3a8acb71548658391e798c79369241e6490a4?response-content-disposition=attachment%3Bfilename%3D%22protobuf-java-3.4.0.jar%22&Policy=eyJTdGF0ZW1lbnQiOiBbeyJSZXNvdXJjZSI6Imh0dHAqOi8vZDI5dnprNG93MDd3aTcuY2xvdWRmcm9udC5uZXQvZGNlN2U2NmIzMjQ1NmExYjExOThkYTBjYWZmM2E4YWNiNzE1NDg2NTgzOTFlNzk4Yzc5MzY5MjQxZTY0OTBhND9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPWF0dGFjaG1lbnQlM0JmaWxlbmFtZSUzRCUyMnByb3RvYnVmLWphdmEtMy40LjAuamFyJTIyIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxNTc3NDM5Njg5fSwiSXBBZGRyZXNzIjp7IkFXUzpTb3VyY2VJcCI6IjAuMC4wLjAvMCJ9fX1dfQ__&Signature=nfXz1ICAESgVr2oNwTuWwFRmg5M7b0gztJEAz61sMot5JD~VN24PRH1mCRzcJ5XPms--be0ArrswT8UWPwtWdFGv3XMJ10JtOLKOfiiNo2Z0nOBklleMCKsXNtncHuuQeDzI-cEfKH4v42PFzF3E68TihzuTODgXTwUbQxJJBWzut~0EggasaBEBJDfFUesldL8vnyftT-iwpkpgl7A4fOpRVbQBAukT0kYgda60xK~x5ShpkLEgRb8k6NImnVt5yaH5kb3d9MohB2BFVTQW5ds6t86PvVDtOLp1jZwC3yNyNOVZcJaqbMR57Z4p-~TI-s4J5NUK7GybBBBjAbDGNw__&Key-Pair-Id=APKAIFKFWOMXM2UMTSFA'.
            > d29vzk4ow07wi7.cloudfront.net

如果你的报错信息前几行与我的相同,那么你适用下面的解决方案。

解决方案

从报错信息中可以看到,是 文件加载失败所导致,因此需要更换 gradle 文件中的镜像路径

1. 依次展开项目目录:【android】→【build.gradle】文件

2. 打开 build.gradle 文件进行编辑,请直接移步到我写注释的地方,从而对照你的文件进行更改。

buildscript {
    ext.kotlin_version = '1.3.50'
    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.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    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'}
        
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

可以看到,我注释掉了 google() / jcenter(),替换成了三个绝对地址。

测试

这时最好重启编辑器或重启电脑,然后再进行 Debug(调试),绝对不会再报错了。


我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2h27kafz2y4gw