历时一周,终于在安卓模拟器跑起来了。。。感动到哭!!!
一直iOS 模拟器没有问题,可是安卓模拟器,无论是 VS Code 还是AndroidStudio 都运行不了flutterApp。一直在探索哪里出了问题。今天终于跑起来了,先是在自己电脑搞定的,由于公司电脑无法上外网,所以耗费了很长时间。建议自己先用自己电脑连家里的wifi 去配置安卓模拟器环境。
开始最新的 3.4.1 版本有问题。请下载AndroidStudio 3.2.1 版本安装。
首先贴下出现的问题:
1.是因为gradle 被墙。所以下载本地。
gradle-4.10.2-all可以在网站:http://services.gradle.org/distributions/ 上免费下载。该网站提供几乎所有的gradle版本下载,找到你需要的版本直接下载。
找到gradle-4.10.2-all 下载到本地桌面
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
* Error running Gradle:
ProcessException: Process "/Users/suning/Desktop/flutterdemos/flutter_demo/android/gradlew" exited abnormally:
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 resolve com.android.tools.build:gradle:3.2.1.
Required by:
project :
> Could not resolve com.android.tools.build:gradle:3.2.1.
> Could not get resource 'https://maven.aliyun.com/repository/google/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom'.
> Could not GET 'https://maven.aliyun.com/repository/google/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom'.
> http://it.cnsuning.com/zongbu.pac: nodename nor servname provided, or not known
> Could not resolve com.android.tools.build:gradle:3.2.1.
> Could not get resource 'https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom'.
> Could not GET 'https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom'.
> http://it.cnsuning.com/zongbu.pac
> Could not resolve com.android.tools.build:gradle:3.2.1.
> Could not get resource 'http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom'.
> Could not GET 'http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom'.
> http://it.cnsuning.com/zongbu.pac
* 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 5s
Command: /Users/suning/Desktop/flutterdemos/flutter_demo/android/gradlew app:properties
Finished with error: Please review your Gradle project setup in the android/ folder.
看到上面的你肯定疯了,不要急,我来帮你。记得关注+点赞。
Could not resolve all artifacts for configuration ':classpath'.
Could not resolve com.android.tools.build:gradle:3.2.1.
提示:classpath 对应的版本找不到
Could not get resource 'https://maven.aliyun.com/repository/google/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom'.
这个句:我们大概知道是拿不到对应路径下的资源。我们可以copy 链接去路径下
https://maven.aliyun.com/mvn/view 下查看报错的路径下有没有,我当时看的是没有gradle-3.2.1的。现在好像又有了gradle-3.2.1。总之gradle-3.2.1不太靠谱,改使用gradle-3.1.2。
http://it.xxxx.com/zhongbu.pac: nodename nor servname provided, or not known
这一句:是因为公司是代理上网,说明你的网络设置了代理节点。想办法去掉代理,下面全部取消勾选。
解决步骤:
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.1.2'
}
}
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' }
}
}
buildscript {
repositories {
//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.1.2'
}
}
9fahxiiecdb76a5g3aw9oi8rv 这个是随机的文件名,你的可能不一样。将下载的桌面的gradle-4.10.2-all.zip copy 到此文件下,双击解压。
修改过程的 gradle下载地址
找到项目中的android->gradle->wrapper->gradle-wrapper.properties:
distributionUrl=file: 修改为本机4.10.2.all.zip的路径
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=file:/Users/xxx/.gradle/wrapper/dists/9fahxiiecdb76a5g3aw9oi8rv/gradle-4.10.2-all.zip
加粗的名字要和上面存放zip的一直。 如下图:保存重启AndroidStudio
总结:如果你做好以上步骤,重启AndroidStudio 选中安卓模拟器,run 即可。或者在工程终端栏运行:flutter run 等待运行。。。
运行起来的效果,此处应该插播一个喽诶楼诶的世界杯的音乐!!!
????????
如果还不成功,不要担心,多重启几次。
这个问题是由于 Android Studio 我从3.2.1 升级到3.4.2 然后修改了gradle
修改build.gradle(Project )中gradle的版本,根据实际情况把版本改低或改高。
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
改成
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
}
Flutter. 也同步改成了
Macintosh HD ▸ 用户 ▸ suning ▸ Flutter_SDK ▸ flutter ▸ packages ▸ flutter_tools ▸ gradle ▸ flutter.gradle文件也修改了
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
}
造成的。
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
编译正常
总结:这一问题花费很长时间,不过失败很多次都没放弃,最终也尝到了成功的喜悦!值得了。