0

当我尝试构建 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:在模块中发现重复的 Protobuf 类

使用 firebase 和 google-cloud-texttospeech 时重复类

4

1 回答 1

0

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

要解决此问题,您可以将这样的代码块(将您的库版本放在“强制”之后)到您的 build.gradle 文件(模块: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'
    }
}
于 2021-09-30T07:47:53.657 回答