Flutter中Missing google_app_id致Firebase Analytics禁用问题求助
Firebase Analytics 报错「Missing google_app_id」问题排查与修复
Crashlytics可正常工作,但Firebase Analytics持续抛出「Missing google_app_id. Firebase Analytics disabled」错误,无任何数据记录,Debug View也无法查看上报信息。已尝试StackOverflow所有方案、通过FlutterFire CLI配置应用、确认google-services.json无误、导入core/crashlytics/analytics包,但问题依旧。
当前配置文件
项目根目录 build.gradle
buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.android.tools.build:gradle:8.1.2' classpath 'com.google.gms:google-services:4.4.0' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1' } } allprojects { repositories { google() mavenCentral() maven { url "https://phonepe.mycloudrepo.io/public/repositories/phonepe-intentsdk-android" } } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
app模块 build.gradle
plugins { id "com.android.application" id "com.google.gms.google-services" apply true id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" // Make sure that you have the Google services Gradle plugin // id 'com.google.gms.google-services' // Add the Crashlytics Gradle plugin // id 'com.google.firebase.crashlytics' } 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.jbullsinfotech.flutter_template" compileSdkVersion 34 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.jbullsinfotech.flutter_template" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-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 { // Import the BoM for the Firebase platform // implementation(platform("com.google.firebase:firebase-bom:32.7.0")) // Add the dependencies for the Crashlytics and Analytics libraries // When using the BoM, you don't specify versions in Firebase library dependencies // implementation("com.google.firebase:firebase-crashlytics") // implementation("com.google.firebase:firebase-analytics") // This dependency is downloaded from the Google’s Maven repository. // So, make sure you also include that repository in your project's build.gradle file. implementation 'com.google.android.play:app-update:2.1.0' // For Kotlin users also add the Kotlin extensions library for Play In-App Update: implementation 'com.google.android.play:app-update-ktx:2.1.0' } // This must appear at the bottom of the file //apply plugin: 'com.google.firebase.crashlytics'
修复步骤
启用Firebase BOM与Analytics依赖
当前app/build.gradle中注释了Firebase BOM和Analytics依赖,导致Analytics资源无法正确引入。取消注释相关配置:dependencies { // 导入Firebase平台BOM implementation(platform("com.google.firebase:firebase-bom:32.7.0")) // 添加Crashlytics和Analytics依赖(使用BOM无需指定版本) implementation("com.google.firebase:firebase-crashlytics") implementation("com.google.firebase:firebase-analytics") // 其他依赖保持不变 implementation 'com.google.android.play:app-update:2.1.0' implementation 'com.google.android.play:app-update-ktx:2.1.0' }正确应用Crashlytics插件
虽然Crashlytics当前可用,但注释的插件声明可能影响Analytics联动。在app/build.gradle的plugins块中取消注释Crashlytics插件,并删除文件底部重复的注释语句:plugins { id "com.android.application" id "com.google.gms.google-services" apply true id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" // 启用Crashlytics插件 id 'com.google.firebase.crashlytics' }验证
google-services.json配置- 确认文件放置在
app/目录下,而非项目根目录。 - 检查文件中
mobilesdk_app_id和project_id与Firebase控制台当前应用信息完全匹配。 - 确保文件未被
.gitignore排除,构建时能被Gradle正常读取。
- 确认文件放置在
清理缓存并重新构建
执行以下命令清理旧缓存,避免配置不生效:flutter clean cd android && ./gradlew clean build --refresh-dependencies cd .. && flutter run检查Flutter端初始化代码
确保正确初始化Firebase核心并启用Analytics:import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_analytics/firebase_analytics.dart'; import 'package:flutter/material.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); // 初始化Analytics并发送测试事件 FirebaseAnalytics analytics = FirebaseAnalytics.instance; await analytics.logEvent(name: 'app_launch'); runApp(const MyApp()); }激活Debug View验证
- 确保测试设备与Firebase控制台处于同一网络。
- 在设备上执行命令启用Debug模式:
adb shell setprop debug.firebase.analytics.app com.jbullsinfotech.flutter_template - 重启应用后,查看Firebase控制台Debug View是否有事件上报。
内容的提问来源于stack exchange,提问作者J Bulls Infotech
相关产品推荐
相关产品推荐

