无论是第一次还是不是第一次运行应用程序,当您点击“运行”时,您会收到一个X gradle构建错误。如果您的错误中有以下任何一行甚至一行,则请尝试此解决方案。
Launching lib\main.dart on sdk gphone x86 in debug mode...
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 builder.jar (com.android.tools.build:builder:3.5.0)
> Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/builder/3.5.0/builder-3.5.0.jar'.
> Premature end of Content-Length delimited message body (expected: 8174407; received: 4456416
> Could not download bundletool.jar (com.android.tools.build:bundletool:0.9.0)
> Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/bundletool/0.9.0/bundletool-0.9.0.jar'.
> Premature end of Content-Length delimited message body (expected: 5248142; received: 4456416
解决方案
前往此位置:.flutter/packages/flutter_tools/gradle/flutter.gradle
**
- 在编辑文件之前备份该文件到另一个地方
- 搜索名为
buildscript
的内容;
- 它应该像这样出现(或类似,不用担心你有备份文件)
并覆盖此代码
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
通过这种方式
buildscript {
repositories {
maven {
url 'https://dl.google.com/dl/android/maven2'
}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
你的问题还没有解决吗?如果已经解决了,请不要尝试下一个步骤。否则,请继续尝试。(即使这个方法不起作用,也请将备份文件放回原处并查看是否可以正常工作)
然后,在你的 android
文件夹中找到 build.gradle
(在你的项目文件中进入 android/build.gradle
),并将 buildscript
更改为以下内容(不用担心代码与此不完全相同,只需在指定位置添加指定行即可)。
buildscript {
repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}