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

添加Google Cloud Natural Language库到Android应用出现重复类错误如何解决

Android接入Google Natural Language库后构建报Duplicate class冲突

我在构建Android应用时遇到报错,该错误是在我向Gradle构建文件中添加自然语言库依赖后出现的。我的Gradle构建文件代码如下:

plugins {
    id 'com.android.application'
    id 'com.google.secrets_gradle_plugin' version '0.5'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.myvolunteer"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

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

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'com.google.firebase:firebase-firestore:23.0.3'
    implementation 'com.firebaseui:firebase-ui-firestore:8.0.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.google.firebase:firebase-database:20.0.2'
    implementation 'com.google.firebase:firebase-storage:20.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // Import the BoM for the Firebase platform

    // Declare the dependencies for the Firebase Cloud Messaging and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    implementation 'com.google.firebase:firebase-analytics:19.0.1'

    //Glide
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

    //Circle Image View
    implementation 'de.hdodenhof:circleimageview:3.1.0'

    //Picasso
    implementation 'com.squareup.picasso:picasso:2.71828'

    implementation 'com.google.android.gms:play-services-maps:17.0.1'
    implementation 'com.karumi:dexter:6.2.3'
    implementation 'com.google.android.gms:play-services-location:18.0.0'


    //Cloud-lang,but it causes duplicate classes occur.
    implementation 'com.google.cloud:google-cloud-language:2.1.2'
}

我在Android Studio中构建项目时收到的部分报错如下:

Duplicate class com.google.api.Advice found in modules proto-google-common-protos-2.5.0 (com.google.api.grpc:proto-google-common-protos:2.5.0) and protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
Duplicate class com.google.api.AuthProvider$1 found in modules proto-google-common-protos-2.5.0 (com.google.api.grpc:proto-google-common-protos:2.5.0) and protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
Duplicate class com.google.cloud.audit.AuthenticationInfo$Builder found in modules proto-google-common-protos-2.5.0 (com.google.api.grpc:proto-google-common-protos:2.5.0) and protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
Duplicate class com.google.longrunning.Operation$1 found in modules proto-google-common-protos-2.5.0 (com.google.api.grpc:proto-google-common-protos:2.5.0) and protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)

我该如何解决这个问题?我确实需要在应用中接入Google Natural Language库。
更新:我已尝试过网上常见的Firebase和Google Cloud服务依赖冲突解决方案但未成功。

解决方案

冲突原因

该问题是因为Firebase相关依赖内置了精简版的Protobuf通用类型包protolite-well-known-types,而服务端版本的google-cloud-language库依赖了完整的proto-google-common-protos包,两个包存在大量重复定义的类,从而引发构建冲突。

方案1:调整依赖排除规则

保留原有google-cloud-language依赖的前提下,修改依赖声明,排除冲突的包:
将你gradle文件中的自然语言库依赖替换为如下内容:

implementation ('com.google.cloud:google-cloud-language:2.1.2') {
    // 排除冲突的公共proto包
    exclude group: 'com.google.api.grpc', module: 'proto-google-common-protos'
    // 排除服务端专属的grpc相关依赖,减少包体积同时避免其他潜在冲突
    exclude group: 'io.grpc', module: 'grpc-stub'
    exclude group: 'io.grpc', module: 'grpc-protobuf-lite'
}

如果修改后仍有资源冲突报错,可以在android代码块末尾添加如下打包配置:

android {
    // 保留你原有的其他配置不变,新增以下内容
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/*.kotlin_module'
    }
}

修改完成后执行Clean Project再Rebuild Project即可。

方案2:使用移动端适配的替代方案(更推荐)

google-cloud-language本身是为Java服务端设计的SDK,在移动端使用会存在包体积大、依赖冲突多、运行性能差的问题,更推荐使用Google官方针对移动端推出的ML Kit自然语言服务,能力完全覆盖普通自然语言处理需求,且和Firebase生态完全兼容,不会出现依赖冲突:

  • 若需要设备端离线运行的自然语言能力,可以按需引入对应的ML Kit依赖:
// 语言识别
implementation 'com.google.mlkit:language-id:17.0.4'
// 智能回复
implementation 'com.google.mlkit:smart-reply:17.0.2'
// 文本翻译
implementation 'com.google.mlkit:translate:17.0.1'
// 实体提取
implementation 'com.google.mlkit:entity-extraction:16.0.0-beta4'
  • 若必须使用云端的自然语言处理能力,可以直接调用Google Cloud Natural Language的REST接口,不需要引入完整的服务端SDK,从根源避免依赖冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 12:15:02