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

Flutter集成Firebase时com.google.gms.google-services插件找不到求助

解决Flutter集成Firebase时的Gradle插件找不到问题

问题概述

新建Flutter项目集成Firebase时,Gradle构建报错,提示找不到com.google.gms.google-services插件(位置在android/build.gradle第16行)。已放置google-services.json到android/app目录,尝试过添加apply plugin、清理缓存重启IDE,问题仍未解决。

核心错误信息

Build file 'C:\Users\kcebu\StudioProjects\fitness_app\android\build.gradle' line: 16
Plugin [id: 'com.google.gms.google-services', version: '4.4.0', apply: false] was not found in any of the following sources:

问题根源

从错误堆栈可见,Gradle仅在Gradle Central Plugin Repository中搜索插件,未访问Google Maven仓库;同时配置存在冗余声明,加重了构建冲突。


修复步骤

1. 调整根目录android/build.gradle

删除plugins块中的google-services声明,保留buildscript内的classpath配置(该方式对Flutter项目兼容性更强):

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

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:google-services:4.4.0"
    }
}

// 移除以下plugins块
// plugins {
//     id "com.google.gms.google-services" version "4.4.0" apply false
// }

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

2. 清理App模块android/app/build.gradle冗余配置

  • 删除重复的id "com.android.application"声明
  • 删除底部重复的apply plugin: 'com.google.gms.google-services'(plugins块已声明)
plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id "com.google.gms.google-services"
}

dependencies {
    // Import the Firebase BoM
    implementation(platform("com.google.firebase:firebase-bom:32.3.1"))

    // TODO: Add dependencies for Firebase products I want to use
    // When using BoM dont specify versions in Firebase dependencies
    implementation("com.google.firebase:firebase-analytics")

    // Add the dependencies for any other desired Firebase products
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {
    namespace "com.example.fitness_app"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID
        applicationId "com.karl.fitness_app"
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

// 移除以下重复声明
// apply plugin: 'com.google.gms.google-services'

3. 彻底清理Gradle缓存

执行以下命令清除旧缓存,避免残留配置干扰:

flutter clean
cd android
gradlew cleanBuildCache
cd ..
flutter pub get

4. 验证settings.gradle仓库配置

确保android/settings.gradle包含Google仓库,让Gradle能正确拉取依赖:

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "fitness_app"
include ':app'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 19:55:08