如何解决 Firebase In-App Messaging Display 库中的“Duplicate class com.google.common.util.concurrent.ListenableFuture”错误?
2024-03-12 19:22:39
Firebase In-App Messaging Display 库中的“Duplicate class com.google.common.util.concurrent.ListenableFuture”错误
背景
如果你使用 implementation 'com.google.firebase:firebase-inappmessaging-display:17.2.0'
,你可能会遇到错误“Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules guava-20.0.jar (com.google.guava:guava:20.0) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)”。
问题根源
此错误是由依赖项冲突引起的。ListenableFuture 类在 Firebase In-App Messaging Display 库和 Guava 库中都被定义了,而 Firebase In-App Messaging Display 库已经依赖于 Guava 库。
解决方案
要解决此问题,你需要从 app/build.gradle
文件中排除 implementation 'com.google.guava:listenablefuture:1.0'
依赖项。
更新后的 app/build.gradle
implementation 'com.google.firebase:firebase-inappmessaging-display:17.2.0'
implementation 'com.google.android.gms:play-services-base:16.1.0'
implementation 'com.google.android.gms:play-services-analytics:16.0.8'
implementation 'com.google.android.gms:play-services-awareness:16.0.0'
implementation 'com.google.android.gms:play-services-cast:16.2.0'
implementation 'com.google.android.gms:play-services-gcm:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-iid:17.1.2'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
implementation 'android.arch.work:work-runtime:1.0.1'
implementation 'com.android.support:multidex:1.0.3'
apply plugin: 'com.google.gms.google-services'
注意事项
在你从 app/build.gradle
中排除依赖项之前,务必检查其他依赖项是否也需要该依赖项。如果是,则需要确保这些依赖项直接依赖于 Guava 库,或者使用依赖项管理工具来解决依赖项冲突。
常见问题解答
1. 我如何知道其他依赖项是否需要 com.google.guava:listenablefuture:1.0
依赖项?
答:你可以检查依赖项的依赖关系。对于 Android Studio,转到 Build > Analyze Dependencies。对于 Gradle,运行 ./gradlew app:dependencies
。
2. 我如何使用依赖项管理工具解决依赖项冲突?
答:你可以使用 Gradle 的 dependencyManagement 功能或 Maven 的 BOM 功能。
3. 如果排除 com.google.guava:listenablefuture:1.0
依赖项后仍出现错误,我该怎么办?
答:可能是其他依赖项与 Firebase In-App Messaging Display 库不兼容。尝试排除其他可能造成冲突的依赖项。
4. 我是否需要在所有情况下都排除 com.google.guava:listenablefuture:1.0
依赖项?
答:不,只有在你使用 Firebase In-App Messaging Display 库时才需要排除该依赖项。
5. 此解决方法是否适用于 Firebase In-App Messaging Display 库的其他版本?
答:此解决方法可能适用于 Firebase In-App Messaging Display 库的其他版本,但建议始终检查最新版本的文档以获取确切的依赖项要求。