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

集成Firestore与Pub/Sub出现重复类错误,求解决方案

问题描述

单独使用Firestore时功能完全正常,但同时集成Firestore与Pub/Sub库后出现大量重复类错误,不想通过排除依赖的方式修复问题,附上Build.gradle配置及部分错误信息,寻求解决办法。

Build.gradle配置

apply plugin: 'com.android.application'
apply plugin: "com.google.protobuf"


android {
    namespace 'com.example.lottieanimation'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.lottieanimation"
        minSdk 28
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        pickFirst 'lib/*/libc++_shared.so'
        exclude 'META-INF/*'
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    //implementation project(path: ':PubSubSync')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    implementation 'com.google.firebase:firebase-firestore:24.4.5'
    implementation 'com.google.firebase:firebase-auth:21.2.0'
    implementation 'com.google.auth:google-auth-library-oauth2-http:1.16.0'

    implementation('com.google.cloud:google-cloud-pubsub:1.123.7') {
        exclude group: 'org.apache.httpcomponents'
    }
}

部分错误信息

Duplicate class com.google.api.Advice found in modules proto-google-common-protos-2.14.3 (com.google.api.grpc:proto-google-common-protos:2.14.3) and protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
Duplicate class com.google.api.Advice$1 found in modules proto-google-common-protos-2.14.3 (com.google.api.grpc:proto-google-common-protos:2.14.3) and protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
Duplicate class com.google.api.Advice$Builder found in modules proto-google-common-protos-2.14.3 (com.google.api.grpc:proto-google-common-protos:2.14.3) and protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
...(其余错误信息略)


解决办法

1. 强制统一Protobuf依赖版本

在android代码块外添加依赖解析策略,强制所有依赖使用同一版本的Protobuf相关库,消除版本冲突:

configurations.all {
    // 可选择强制使用其中一个版本,优先尝试较高版本
    resolutionStrategy.force 'com.google.api.grpc:proto-google-common-protos:2.14.3'
    // 或者强制使用Firestore依赖的版本
    // resolutionStrategy.force 'com.google.firebase:protolite-well-known-types:18.0.0'
}

2. 换用Android适配的Pub/Sub客户端

你当前使用的是后端场景的google-cloud-pubsub库,对于Android应用,更推荐用Firebase Cloud Messaging(FCM) 替代,它和Firestore生态完全兼容,不会有依赖冲突问题。如果必须用原生Pub/Sub,也可以尝试引入轻量化的移动版依赖,减少冲突概率。

3. 调整Protobuf插件配置

确保com.google.protobuf插件生成的类和Firestore兼容,添加以下配置到build.gradle:

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.24.4'
    }
    plugins {
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.24.4'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite {}
            }
        }
    }
}

这个配置会使用javalite版本的Protobuf生成器,和Firestore使用的实现更匹配。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 02:24:59