解决重复的类依赖性-Gradle

时间:2019-07-09 15:59:23

标签: android gradle speech-recognition gcloud

我一直在寻找解决方案,但是没有运气。 我在同一项目中添加了Google Cloud Speech API和Google Cloud Translation API。删除任何依赖项都会构建项目,但是保留它们都将显示重复的类错误,我不知道如何解决此问题的确切线索。

依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
// Support libraries
implementation "com.android.support:design:$supportLibraryVersion"
implementation "com.android.support:cardview-v7:$supportLibraryVersion"
implementation "com.android.support:recyclerview-v7:$supportLibraryVersion"

// gRPC
implementation "io.grpc:grpc-okhttp:$grpcVersion"
implementation "io.grpc:grpc-protobuf-lite:$grpcVersion"
implementation "io.grpc:grpc-stub:$grpcVersion"
implementation 'javax.annotation:javax.annotation-api:1.3.2'
protobuf 'com.google.protobuf:protobuf-java:3.8.0'

// OAuth2 for Google API
implementation('com.google.auth:google-auth-library-oauth2-http:0.7.0') {
    exclude module: 'httpclient'
}

//Translation
 implementation('com.google.cloud:google-cloud-translate:1.12.0') {
    exclude group: 'org.apache.httpcomponents'
    exclude group: 'org.json', module: 'json'
}
annotationProcessor 'com.google.cloud:google-cloud-translate:1.12.0'

// Tests
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}

尝试:

  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 'project.properties'

}

什么都没解决。

错误:

 Duplicate class com.google.protobuf.AbstractMessageLite found in modules protobuf-java-3.4.0.jar (com.google.protobuf:protobuf-java:3.4.0) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules protobuf-java-3.4.0.jar (com.google.protobuf:protobuf-java:3.4.0) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)

我应该排除哪些类,并且应该对gradle属性做些什么?

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题

添加:

排除群组:“ com.google.protobuf”

 implementation('com.google.cloud:google-cloud-translate:1.12.0') {
            exclude group: 'org.apache.httpcomponents'
            exclude group: 'org.json', module: 'json'
            exclude group: 'com.google.protobuf'  

        }
        annotationProcessor 'com.google.cloud:google-cloud-translate:1.12.0'
    }

并确保拥有这些属性对我有用:

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 'project.properties'

}

答案 1 :(得分:-1)

您可以通过在导航选项下单击选项迁移到androidx来将项目转换为androidx。也许这会有所帮助。