新Android Gradle项目结构下如何添加Firebase Performance Plugin?
新版Gradle配置Firebase Performance插件的正确方式
针对Android Gradle Plugin 7.0+(使用Plugins DSL)的项目,以下是Firebase Performance插件的完整配置步骤,同时覆盖Crashlytics、Google Services等同类插件的通用配置逻辑:
1. 根项目级构建文件配置
在项目根目录的build.gradle(Groovy)或build.gradle.kts(Kotlin)中,通过plugins块声明插件(不直接应用,由模块自行启用):
Groovy 示例(build.gradle)
plugins { // 保留你项目已有的其他插件声明,比如AGP id 'com.android.application' version '7.4.2' apply false id 'com.google.gms.google-services' version '4.3.15' apply false // 声明Firebase Performance插件 id 'com.google.firebase.perf-plugin' version '1.4.2' apply false // 同类插件(如Crashlytics)的声明方式完全一致 id 'com.google.firebase.crashlytics' version '2.9.9' apply false }
Kotlin DSL 示例(build.gradle.kts)
plugins { id("com.android.application") version "7.4.2" apply false id("com.google.gms.google-services") version "4.3.15" apply false id("com.google.firebase.perf-plugin") version "1.4.2" apply false id("com.google.firebase.crashlytics") version "2.9.9" apply false }
2. App模块级构建文件配置
在你的应用模块(通常是app/目录下的构建文件)中,应用插件并添加对应依赖:
Groovy 示例(app/build.gradle)
plugins { id 'com.android.application' id 'com.google.gms.google-services' // 应用Firebase Performance插件 id 'com.google.firebase.perf-plugin' // 如果需要Crashlytics,同样应用它 id 'com.google.firebase.crashlytics' } dependencies { // 添加Firebase Performance核心依赖 implementation 'com.google.firebase:firebase-perf:20.4.1' // Crashlytics依赖示例 implementation 'com.google.firebase:firebase-crashlytics:18.3.2' }
Kotlin DSL 示例(app/build.gradle.kts)
plugins { id("com.android.application") id("com.google.gms.google-services") id("com.google.firebase.perf-plugin") id("com.google.firebase.crashlytics") } dependencies { implementation("com.google.firebase:firebase-perf:20.4.1") implementation("com.google.firebase:firebase-crashlytics:18.3.2") }
3. 问题排查要点
- 版本兼容性:确保插件版本与Firebase依赖版本、AGP版本匹配,避免因版本不兼容导致的同步失败
- 清理缓存:如果配置后仍报错,执行
./gradlew clean build --refresh-dependencies清理Gradle缓存并重新构建 - 验证Google Services配置:确保已在项目中添加
google-services.json文件,且Google Services插件正常应用
内容的提问来源于stack exchange,提问作者Leonardo Sibela
相关产品推荐
相关产品推荐

