集成Dialogflow依赖后Android应用遇重复类及启动崩溃问题
解决Firebase与Dialogflow集成的重复类及启动崩溃问题
我太懂你现在的糟心处境了——集成Dialogflow后和Firebase撞了重复类,好不容易排除冲突又触发启动崩溃,这在Google服务依赖组合里真的是高频踩坑点,咱们一步步拆解解决:
问题根源拆解
你碰到的Duplicate class com.google.api.Advice,本质是Dialogflow的com.google.cloud:google-cloud-dialogflow依赖自带了proto-google-common-protos模块,而Firebase的protolite-well-known-types也包含了完全相同的类。之前你用全局排除规则把guava也一并排除了,可com.google.common.base.Preconditions正是Guava库的核心类,这直接导致App启动时找不到必要类而崩溃。
精准修复方案
1. 删掉全局排除的“一刀切”规则
首先把你在android块里加的全局排除配置删掉,它会误删很多必要依赖:
// 把这段彻底移除 configurations { implementation.exclude module:'proto-google-common-protos' implementation.exclude module:'protolite-well-known-types' implementation.exclude module:'guava' implementation.exclude module:'protobuf-lite' }
2. 给Dialogflow依赖做精准冲突排除
只针对Dialogflow的依赖排除冲突模块,不要全局操作。修改你的Dialogflow依赖声明:
implementation('com.google.cloud:google-cloud-dialogflow:0.98.0-alpha') { // 只排除和Firebase冲突的proto模块 exclude group: 'com.google.api.grpc', module: 'proto-google-common-protos' exclude group: 'com.google.protobuf' // 绝对不能排除guava! }
3. (可选)补加Android适配版Guava依赖
如果做完上面两步后还是出现Preconditions找不到的情况,手动添加专为Android优化的Guava版本:
implementation 'com.google.guava:guava:28.1-android'
这个版本剔除了Android不兼容的部分,不会带来额外问题。
修改后的完整依赖配置
你的dependencies块最终应该是这样的:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.3' implementation 'com.android.support:multidex:1.0.3' implementation 'com.google.android.material:material:1.0.0' implementation 'com.jakewharton:butterknife:10.1.0' annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0' implementation 'com.google.firebase:firebase-core:17.0.0' implementation 'com.google.firebase:firebase-auth:18.0.0' implementation 'com.google.firebase:firebase-database:18.0.0' implementation 'com.google.firebase:firebase-firestore:20.1.0' implementation "com.github.VaibhavLakhera:Circular-Progress-View:0.1.2" implementation 'com.github.CardinalNow:Android-CircleProgressIndicator:v0.2' implementation 'ai.api:sdk:2.0.7@aar' implementation 'ai.api:libai:1.6.12' // 修改后的Dialogflow依赖 implementation('com.google.cloud:google-cloud-dialogflow:0.98.0-alpha') { exclude group: 'com.google.api.grpc', module: 'proto-google-common-protos' exclude group: 'com.google.protobuf' } implementation 'io.grpc:grpc-okhttp:1.21.0' implementation 'com.google.apis:google-api-services-oauth2:v1-rev145-1.25.0' implementation 'com.google.apis:google-api-services-dialogflow:v2-rev80-1.25.0' implementation "com.andkulikov:transitionseverywhere:2.0.0-beta01" // 可选:如果仍缺Preconditions则添加 // implementation 'com.google.guava:guava:28.1-android' } apply plugin: 'com.google.gms.google-services' apply plugin: 'com.jakewharton.butterknife'
方案为什么能解决问题
- 精准排除只移除了Dialogflow带来的冲突模块,保留了Firebase自身必需的
protolite-well-known-types - 没有碰Guava依赖,确保
Preconditions等核心类能正常被加载 - 避免了全局排除带来的“误伤”式依赖缺失问题
内容的提问来源于stack exchange,提问作者Husam Gibreel




