解决Android Studio创建Flutter项目异常

Android Studio成功集成Flutter插件以后,就可以新建一个Flutter项目了。创建Flutter项目成功之后,运行Flutter项目出现如下异常:

* Where:

Build file '/Users/guotianhui/Downloads/flutter_app2/flutter_appw2/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 download guava.jar (com.google.guava:guava:23.0)

      > Could not get resource 'https://jcenter.bintray.com/com/google/guava/guava/23.0/guava-23.0.jar'.

        > Could not GET 'https://d29vzk4ow07wi7.cloudfront.net/7baa80df284117e5b945b19b98d367a85ea7b7801bd358ff657946c3bd1b6596?response-content-disposition=attachment%3Bfilename%3D%22guava-23.0.jar%22&Policy=eyJTdGF0ZW1lbnQiOiBbeyJSZXNvdXJjZSI6Imh0dHAqOi8vZDI5dnprNG93MDd3aTcuY2xvdWRmcm9udC5uZXQvN2JhYTgwZGYyODQxMTdlNWI5NDViMTliOThkMzY3YTg1ZWE3Yjc4MDFiZDM1OGZmNjU3OTQ2YzNiZDFiNjU5Nj9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPWF0dGFjaG1lbnQlM0JmaWxlbmFtZSUzRCUyMmd1YXZhLTIzLjAuamFyJTIyIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxNTYwNzUwMTk3fSwiSXBBZGRyZXNzIjp7IkFXUzpTb3VyY2VJcCI6IjAuMC4wLjAvMCJ9fX1dfQ__&Signature=BoSQcFEL3Wy5BF-bG56IkHfoEYKYHwgjjlfYH9mJ-fZtvTZeigVj9CtP01QtxQJHQsCsNeiFKD2j7SuxOupMU7zjaiG4xrk2-0f-xpFJo~dto51u3n~kmRXqZEBjDZcFUch3LAusdsMbP1p-EOoZvmv9E6VjVeVkc-zR-JMuTpPYuy0TTDGEc2JfhSwVa0JE75yy1ikrlOlqAoH3JGMZjcUhQYeiCe56LKlX5f5QEFsU-aMQhuAwe12bco5~XuSf4IzdFRr~iUQ-n10e8OBykoV7ke9apVI45DMAVALTSozU0ZwyyFPvnFWO8LLNhRjdvY65RAE6mfE8RSRJFZSZMA__&Key-Pair-Id=APKAIFKFWOMXM2UMTSFA'.

            > Connect to d29vzk4ow07wi7.cloudfront.net:443 [d29vzk4ow07wi7.cloudfront.net/99.84.231.221, d29vzk4ow07wi7.cloudfront.net/99.84.231.76, d29vzk4ow07wi7.cloudfront.net/99.84.231.93, d29vzk4ow07wi7.cloudfront.net/99.84.231.174] failed: Read timed out

* 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 6m 26s

  Command: /Users/guotianhui/Downloads/flutter_app2/flutter_appw2/android/gradlew app:properties

Finished with error: Please review your Gradle project setup in the android/ folder.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

这里提示在Android目录找不到Guava包,并提示错误在app bulid.gradle文件的第25行:

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

1

定位到flutterRoot指向的是flutter.sdk的配置。

def flutterRoot = localProperties.getProperty('flutter.sdk')

if (flutterRoot == null) {

    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")

}

1

2

3

4

我们在local.properties文件里面可以查看到flutter sdk的路径:

flutter.sdk=/Users/guotianhui/Android/flutter

1

目前除了找不到GradleException这个异常类之外,其他都没有发现异常问题。然后百度了通过dependencies依赖类库的方式:

    testImplementation 'com.google.guava:guava:24.0-android'

    testImplementation 'com.google.guava:guava:24.0-jre'

1

2

集成到项目里面,但是依然没有解决问题。还是报下面的错误:

* Error running Gradle:

ProcessException: Process "/Users/guotianhui/Downloads/flutter_app2/flutter_appw2/android/gradlew" exited abnormally:

  Command: /Users/guotianhui/Downloads/flutter_app2/flutter_appw2/android/gradlew app:properties

Finished with error: Please review your Gradle project setup in the android/ folder.

1

2

3

4

5

6

使用Android studio 集成Flutter插件,除了在Android Studio IDE里面通过File–>Settings–>

Plugins–>下载Flutter插件,下载Flutter插件时会提示你是否下载Dart插件,点击确认下载就行。

到这里我们就在Android Studio IDE里面集成了Flutter插件,但是我们还是不能直接开发Flutter项目,在我们启动Studio的时候,我们发现多了一个可以新建Flutter项目选项。但是我们并没有配置好Flutter的开发环境,也就是Flutter的Sdk。类似与Java Sdk,我们需要配置Flutter Sdk的绝对路径到Path环境变量。

Winddows用户操作比较简单,选中电脑–>属性–>高级选项–>环境变量–>新建一个FLUTTER_PATH环境变量,设置值为你直接从Flutter官网下载的SDK绝对路径。下载完Flutter文件之后,我们就不需要再去Dart官网下载Dart Sdk文件了,因为Fluter Sdk的bin/cache/目录下就包含了dart-sdk文件。

那么我们说一些Mac用户如何配置Flutter Sdk环境变量,当然你也可以查看官网的配置教程:

open .bash_profile

1

这个是本人比较喜欢的Mac添加环境变量的命令,直接打开环境变量文件,然后更改里面的环境变量,添加下面配置:

export PATH=安装路径/flutter/bin:$PATH

1

最后更改好之后,直接关闭文件,默认就保存了。但是还要记得执行下面的命令来保存你更改的环境变量配置:

source .bash_profile

1

最后我们可以运行下面的命令查看Flutter Sdk有没有保存成功:

flutter doctor

1

如果出现下面的界面,说明你的Flutter Sdk安装成功了。

成功安装Flutter插件和Dart插件,还有配置好Flutter 和Dart的绝对路径之后,我们再去新建一个Flutter项目发现并没有报错。但是运行之后报下面的错:

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.2.71)

      > Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.2.71/kotlin-gradle-plugin-1.2.71.jar'....

1

2

3

4

5

这是因为我们升级了kotlin的版本导致的,项目找不到1.2.71的kotlin jar包。我们打开android项目配置里面的build.gradle文件成和你项目一致的kotlin版本和android studio版本就可以了。

buildscript {

    ext.kotlin_version = '1.3.31'

    repositories {

        google()

        jcenter()

    }

    dependencies {

        classpath 'com.android.tools.build:gradle:3.4.1'

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    }

}

1

2

3

4

5

6

7

8

9

10

11

这里我们的android studio 的版本是3.4.1,对应的最新的kotlin版本是1.3.31,如果你不知道你的studio版本,可以查看IDE的导航栏,查看help->搜索about studio 查看版本,查看kotlin的最新版本可以查看Tools->kotlin->Configeru Kotlin Plugin Updates。需要查看kotlin版本对应的android studio版本,可以前往Kotlin Plugin官网。

但是现在虽然我的项目可以单独运行android项目了,但是直接在Flutter项目里面运行还是会报下面的错:

* What went wrong:

A problem occurred configuring root project 'android'.

> Could not resolve all artifacts for configuration ':classpath'.

  > Could not download kotlin-compiler-embeddable.jar (org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.31)

      > Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-compiler-embeddable/1.3.31/kotlin-compiler-embeddable-1.3.31.jar'.

        > Could not GET 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-compiler-embeddable/1.3.31/kotlin-compiler-embeddable-1.3.31.jar'.

            > Read timed out

  > Could not download kotlin-build-common.jar (org.jetbrains.kotlin:kotlin-build-common:1.3.31)

      > Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-build-common/1.3.31/kotlin-build-common-1.3.31.jar'.

        > Could not GET 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-build-common/1.3.31/kotlin-build-common-1.3.31.jar'.

            > Read timed out

  > Could not download kotlin-daemon-client.jar (org.jetbrains.kotlin:kotlin-daemon-client:1.3.31)

      > Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-daemon-client/1.3.31/kotlin-daemon-client-1.3.31.jar'.

        > Could not GET 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-daemon-client/1.3.31/kotlin-daemon-client-1.3.31.jar'.

            > Read timed out

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

这里提示我下载不了kotlin compiler编译器读取超时,百度了一下说是链接链接超时的问题,需要替换成阿里的链接。把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' }

1

2

3

替换成功之后,再次运行就可以成功运行了。

总结一下遇到的问题:

如果你下载了Flutter插件和Dart插件之后,新建Flutter项目之后;

第一,如果你的项目报编译错误,那么可能是你没有正确的配置好Flutter和Dart Sdk的路径。

第二,如果你配置好了Flutter和Dart 插件的Sdk路径,但是运行时报Kotlin jar包找不到,需要检查你的kotlin版本和android studio版本是否配对,然后在确认respositorier的路径是否改成阿里的地址。

以上就是全部坑,希望对大家有用!

————————————————

版权声明:本文为CSDN博主「技术承载梦想」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/u013823101/article/details/92774333

推荐阅读更多精彩内容