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

添加Firebase In-App Messaging时Protobuf重复类构建错误求助

解决Firebase In-App Messaging集成后的Protobuf重复类问题

问题根源

你的构建错误是因为项目同时引入了完整版Protobuf(com.google.protobuf:protobuf-java:2.5.0)和轻量版Protobuf-Javalite(com.google.protobuf:protobuf-javalite:3.22.3),两者类路径完全重叠。旧版Firebase依赖(如firebase-jobdispatcher、firebase-ui-auth:4.3.1)会自动引入旧版完整版Protobuf,而Firebase In-App Messaging依赖的是轻量版Javalite,最终导致类重复冲突。

分步解决方案

1. 全量使用Firebase BOM统一版本管理

你之前仅给In-App Messaging单独配置了BOM,但其他Firebase依赖仍手动指定版本,这会导致版本不兼容。必须将所有Firebase依赖切换到BOM管理,确保版本完全对齐:

首先在项目根目录build.gradle升级相关插件到稳定版:

dependencies {
    classpath 'com.android.tools.build:gradle:8.0.2'
    classpath 'com.google.gms:google-services:4.4.0' // 升级到最新稳定版
    classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.9'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    classpath 'com.google.firebase:perf-plugin:1.4.2' // 升级性能监控插件版本
    // 移除已废弃的gradle-bintray-plugin
}

然后在App模块build.gradle中,用BOM替换所有Firebase依赖的手动版本:

// 引入Firebase BOM,使用最新稳定版(示例为29.2.1,可按需更换)
implementation platform('com.google.firebase:firebase-bom:29.2.1')

// 所有Firebase依赖不再指定版本,由BOM自动管理
implementation 'com.google.firebase:firebase-inappmessaging-display'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-crashlytics'
implementation 'com.google.firebase:firebase-perf'
implementation 'com.google.firebase:firebase-appcheck-playintegrity'

// 移除firebase-core(已被firebase-analytics替代)、firebase-iid(已被firebase-messaging整合)

2. 处理引入旧Protobuf的第三方依赖

你的项目中有两个高风险依赖:

  • com.firebase:firebase-jobdispatcher:0.8.6:已废弃的库,会引入旧版Protobuf,推荐用AndroidX WorkManager替代。
  • com.firebaseui:firebase-ui-auth:4.3.1:版本过于老旧,依赖旧版Firebase和Protobuf。

方案A:替换废弃依赖

用WorkManager替代firebase-jobdispatcher:

// 添加WorkManager依赖
implementation "androidx.work:work-runtime:2.7.1"

之后将JobDispatcher的业务逻辑迁移到WorkManager即可。

方案B:强制排除旧Protobuf(临时方案)

如果暂时无法替换旧库,在依赖中排除所有protobuf-java模块:

// 处理firebase-jobdispatcher
implementation('com.firebase:firebase-jobdispatcher:0.8.6') {
    exclude group: 'com.google.protobuf', module: 'protobuf-java'
}

// 升级firebase-ui-auth到兼容BOM的版本并排除Protobuf
implementation('com.firebaseui:firebase-ui-auth:8.0.2') {
    exclude group: 'com.google.protobuf', module: 'protobuf-java'
}

3. 全局强制Protobuf版本(兜底方案)

如果以上步骤仍存在冲突,在App模块build.gradle中添加版本解析策略,强制所有依赖使用Protobuf-Javalite:

configurations.all {
    resolutionStrategy {
        // 强制使用指定版本的javalite,排除完整版Protobuf
        force 'com.google.protobuf:protobuf-javalite:3.22.3'
        exclude group: 'com.google.protobuf', module: 'protobuf-java'
    }
}

验证步骤

  1. 执行./gradlew app:dependencies(Windows环境用gradlew.bat app:dependencies),搜索protobuf-java,确认无任何依赖引入该模块。
  2. 清理构建缓存:执行./gradlew clean后重新构建项目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 11:25:07