Flutter集成Firebase时Gradle插件找不到问题求助
Flutter集成Firebase时Gradle找不到Google Services插件的问题
我按照Firebase官方说明配置Flutter项目集成Firebase,已按要求修改build.gradle文件,并将google-services.json放置在android/app目录下,但构建时始终报错找不到Google Services插件,以下是详细信息:
当前配置文件
根目录/build.gradle
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" } } plugins { id 'com.google.gms.google-services' version '4.3.15' 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 }
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 { namespace "com.example.chat" 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 { applicationId "com.example.chat" minSdkVersion flutter.minSdkVersion targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } plugins { id 'com.android.application' id 'com.google.gms.google-services' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation platform('com.google.firebase:firebase-bom:32.2.0') }
google-services.json(放置于android/app目录)
{ "project_info": { "project_number": "90796036267", "project_id": "chatflutter-92032", "storage_bucket": "chatflutter-92032.appspot.com" }, "client": [ { "client_info": { "mobilesdk_app_id": "1:90796036267:android:54ef4c10ee30c08c5527ee", "android_client_info": { "package_name": "com.example.chat" } }, "oauth_client": [ { "client_id": "90796036267-7lrefr1ltb27cehouki0p77qdlu4q3ei.apps.googleusercontent.com", "client_type": 1, "android_info": { "package_name": "com.example.chat", "certificate_hash": "a84f187140210a747f937ed89ceef6213d9cc676" } }, { "client_id": "90796036267-lh6arnnstp7sumrft3jfnp8ln19bl08f.apps.googleusercontent.com", "client_type": 3 } ], "api_key": [ { "current_key": "AIzaSyB4AkDYnF-F14Jisg2gn3JrBkjH4LL0sIQ" } ], "services": { "appinvite_service": { "other_platform_oauth_client": [ { "client_id": "90796036267-lh6arnnstp7sumrft3jfnp8ln19bl08f.apps.googleusercontent.com", "client_type": 3 } ] } } } ], "configuration_version": "1" }
构建错误信息
FAILURE: Build failed with an exception. * Where: Build file 'D:\cursoFlutter\chat\android\build.gradle' line: 15 * What went wrong: Plugin [id: 'com.google.gms.google-services', version: '4.3.15', apply: false] was not found in any of the following sources: - Gradle Core Plugins (plugin is not in 'org.gradle' namespace) - Plugin Repositories (could not resolve plugin artifact 'com.google.gms.google-services:com.google.gms.google-services.gradle.plugin:4.3.15') Searched in the following repositories: Gradle Central Plugin Repository * 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 1s Exception: Gradle task assembleDebug failed with exit code 1
解决方案
问题根源
- Gradle的
plugins块依赖pluginManagement配置的仓库查找插件,当前根目录build.gradle未配置该块,导致仅从Gradle Central仓库查找,而Google Services插件不在此仓库中。 - app目录
build.gradle重复声明了com.android.application插件,会引发构建冲突。
修改步骤
- 更新根目录/build.gradle,添加
pluginManagement配置:
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } 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" } } plugins { id 'com.google.gms.google-services' version '4.3.15' apply false } // 其余代码保持不变
- 修复app目录/build.gradle的插件声明,删除重复的
com.android.application:
plugins { id 'com.google.gms.google-services' }
- 执行命令清理缓存并重新构建:
flutter clean flutter run
内容的提问来源于stack exchange,提问作者Eduardo Machado
相关产品推荐
相关产品推荐

