Flutter项目真机编译失败:找不到gradle:7.3.0依赖
Flutter 真机编译报错:无法找到 com.android.tools.build:gradle:7.3.0
错误详情
FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\PcMax\development\projects\shop_appre\android\app\build.gradle' line: 26 * What went wrong: A problem occurred evaluating project ':app'. > Could not resolve all files for configuration 'classpath'. > Could not find com.android.tools.build:gradle:7.3.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.3.0/gradle-7.3.0.pom - https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/7.3.0/gradle-7.3.0.pom Required by: unspecified:unspecified:unspecified
项目配置文件
android/app/build.gradle
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } 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.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.shop_app" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration. minSdkVersion flutter.minSdkVersion targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
android/build.gradle
buildscript { ext.kotlin_version = '1.6.10' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } task clean(type: Delete) { delete rootProject.buildDir }
解决方案
1. 同步Android Gradle Plugin版本
错误提示找不到7.3.0版本的AGP,但你的android/build.gradle里配置的是7.1.2。这是因为你使用的最新版Flutter SDK,其内置的flutter.gradle脚本依赖了更高版本的AGP。
修改android/build.gradle中的AGP版本为7.3.0:
dependencies { classpath 'com.android.tools.build:gradle:7.3.0' // 替换原来的7.1.2 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
2. 匹配Gradle Wrapper版本
AGP 7.3.0需要搭配Gradle 7.4及以上版本使用,因此需要修改gradle/wrapper/gradle-wrapper.properties中的distributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
3. 清理并重新构建
执行以下命令清理缓存并重新构建:
flutter clean flutter pub get flutter run
补充说明
如果修改后仍无法下载,检查网络是否能访问Google Maven仓库,或者可以尝试在buildscript和allprojects的repositories中添加国内镜像作为备选:
repositories { google() mavenCentral() maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/public' } }
内容的提问来源于stack exchange,提问作者Anas Fayad
相关产品推荐
相关产品推荐

