将 Google Cloud 语言库添加到 Android 应用时出现重复的类

Posted

技术标签:

【中文标题】将 Google Cloud 语言库添加到 Android 应用时出现重复的类【英文标题】:Duplicate classes occur when adding Google Cloud Language library to Android app 【发布时间】:2021-11-21 13:34:39 【问题描述】:

我在尝试构建 android 应用程序时遇到问题,并且出现错误。我在 Gradle Build 文件(自然语言库)中添加最后一行后出现错误。 下面是我的 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 自然语言库添加到我的应用中。

更新:我尝试过但失败的尝试:

Firebase Android: Duplicate Protobuf classes found in modules

Duplicate Class while using firebase and google-cloud-texttospeech

【问题讨论】:

【参考方案1】:

重复类异常意味着您的依赖项之一隐式使用了您也在项目中使用的某些库的较旧或较新(带+)版本,

要解决此问题,您可以将这样的代码块(将您的库版本放在“强制”之后)到您的 build.gradle 文件(Module:app):

configurations 
   all 
      resolutionStrategy 
          // do not upgrade above 3.12.0 to support API < 21 while server uses
          // COMPATIBLE_TLS, or okhttp3 is used in project
          force 'com.squareup.okhttp3:okhttp:3.12.0'
          force 'com.squareup.okhttp3:logging-interceptor:3.12.0'
      
   

您还可以从依赖项中排除某些组。对于您编写的单个依赖项:

dependencies 

    // example
    implementation('log4j:log4j:1.2.15') 
        exclude group: 'javax.jms', module: 'jms'
    

【讨论】:

以上是关于将 Google Cloud 语言库添加到 Android 应用时出现重复的类的主要内容,如果未能解决你的问题,请参考以下文章

通过 Python 客户端库将多个文件上传到 Google Cloud Storage

将 Google 帐号添加到 Cloud Identity 组

Google Cloud将用户添加到项目中

将文件从 Google Cloud Storage 添加到云端硬盘

如何修改Google Cloud函数中的错误日志?

在哪里可以找到 Google Cloud Python 官方库的旧版本文档?