Android Studio更新后,commons-logging定义的类与Android现在提供的类发生冲突。

63

我已将安卓工作室更新至版本3,但现在似乎无法编译之前已经成功编译的项目。

错误信息如下:

Error:Error: commons-logging 定义了和Android提供的类冲突的类。解决方案包括寻找更新版本或者没有相同问题的替代库(例如,对于httpclient使用HttpUrlConnection或okhttp代替),或者使用jarjar之类的工具重新打包库。 [DuplicatePlatformClasses]

依赖关系为

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:27.0.0'
    compile 'com.android.support:design:27.0.0'
    compile 'com.google.api-client:google-api-client-android:1.23.0' exclude module: 'httpclient'
    compile 'com.google.http-client:google-http-client-gson:1.23.0' exclude module: 'httpclient'
    compile 'com.google.firebase:firebase-core:11.4.2'
}

错误似乎是由

引起的
compile 'com.google.api-client:google-api-client-android:1.23.0' exclude module: 'httpclient'
compile 'com.google.http-client:google-http-client-gson:1.23.0' exclude module: 'httpclient'

我已经使用exclude module: 'httpclient'了,为什么它不能编译?这是Android Studio 3和/或包含的com.android.tools.build:gradle:3.0.0插件的bug还是我漏掉了什么?和之前的版本一样,编译完全相同的项目没有问题。


那么为什么它无法编译?根据错误,你的问题出在commons-logging上。也许有更新的com.google依赖项版本可供使用。 - CommonsWare
@CommonsWare 我没有找到任何更新的库,这些版本是最新的2017年10月发布的。 - AndreaF
你的libs文件夹里有相同的jar包吗? - Gabriele Mariotti
@GabrieleMariotti 绝对不是这样。我的libs文件夹里没有任何的jar包。这些库只在build.gradle的依赖部分中指定(否则之前的Android Studio也会拒绝构建)。因此,我无法想象为什么在Android Studio更新后无法编译。 - AndreaF
13个回答

0

我必须结合这里的多个解决方案。这是对我有效的方法:

    configurations {
        all*.exclude group: 'com.google.guava', module: 'listenablefuture'
        configureEach {
            exclude module: 'httpclient'
            exclude module: 'commons-logging'
            exclude group: 'commons-logging', module: 'commons-logging'
            exclude group: 'org.apache.httpcomponents'
        }
    }

-1

添加这个然后同步你的Gradle

configurations {
    all*.exclude group: 'com.google.guava', module: 'listenablefuture'
    all*.exclude module: 'httpclient'
    all*.exclude module: 'commons-logging'
}

-1

在我的情况下,Android Studio 无法识别“httpclient”,因此我无法使用@Silverstorm的答案。

相反,我找到了另一个答案: 错误:json定义的类与Android现在提供的类冲突

这意味着需要在应用程序build.gradle中添加以下内容:

configurations {
    all {
        exclude group: 'org.json', module: 'json'
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接