You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter构建失败:任务':location:generateDebugRFile'执行出错

构建失败:Execution failed for task ':location:generateDebugRFile'

错误详情

FAILURE: Build failed with an exception

* What went wrong:
Execution failed for task ':location:generateDebugRFile'.
> Could not resolve all files for configuration ':location:debugCompileClasspath'.
   > Could not find com.google.android.gms:play-services-location:16..
     Required by:
         project :location

* 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 19s
Exception: Gradle task assembleDebug failed with exit code 1

配置文件

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 FileNotFoundException("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'
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
// END: FlutterFire Configuration
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 33
    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.fun"
        // 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 23
        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-gradle-plugin:$kotlin_version"
    implementation 'com.google.android.gms:play-services-auth:20.4.1'
    implementation 'com.google.android.gms:play-services-location:21.0.1'

}
apply plugin: 'com.google.gms.google-services'
// Work around for onesignal-gradle-plugin compatibility
googleServices.disableVersionCheck = true

android/build.gradle

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.3.15'
        // END: FlutterFire Configuration
        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
}

flutter doctor 检测结果

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.71.0)
[✓] Connected device (4 available)
[✓] HTTP Host Availability

• No issues found!

补充说明

仅需启动应用生成debug.keystore文件,已尝试多种同类问题解决方案但无效,请求帮助。


解决方案

1. 强制统一依赖版本

错误显示location模块引用了版本号错误的com.google.android.gms:play-services-location:16..(多了一个点),需要强制所有子模块使用你在app中指定的21.0.1版本。

修改android/build.gradle中的subprojects部分,添加版本强制策略:

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.configurations.all {
        resolutionStrategy {
            force 'com.google.android.gms:play-services-location:21.0.1'
        }
    }
}
subprojects {
    project.evaluationDependsOn(':app')
}

2. 清理缓存并重新构建

执行以下命令清理缓存:

flutter clean

然后删除android/.gradle文件夹,最后重新运行:

flutter run

3. 检查并更新location插件版本

打开pubspec.yaml,查看你使用的location插件版本,确保是最新稳定版。如果版本较旧,更新后执行flutter pub get再重新构建。


内容的提问来源于stack exchange,提问作者Mustafa Khetran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:10:20