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个回答

75
在位于应用模块的build.gradle中添加。
configurations {
    all {
        exclude module: 'httpclient'
    }
}

4
如果不包括httpclient,我将无法获取发布版apk。 - Ramesh sambu
是的,我就是这样添加的。 - Ramesh sambu
@NerendraJi 在配置文件中。如果缺失,只需复制粘贴我已发布的代码。 - Silverstorm
4
如果我正在使用httpclient呢? 在升级到Android Studio 3.0之前,我的代码是可以编译的。 实际上,我正在我的代码中使用httpclient,并且不想切换到okhttp。 - Stevey
1
配置 { 所有 { 排除模块: 'httpclient' 排除模块: 'commons-logging' 排除组: 'org.json',模块: 'json' 排除组: 'org.apache.httpcomponents' 排除模块: 'opengl-api' 排除模块: 'xmlParserAPIs' 排除模块: 'xpp3' } }这对我有效。 - gbansal
显示剩余7条评论

51
如果问题与commons-logging有关,则必须将其排除。 在app/build.gradle中添加以下代码。
configurations {
    all {
        exclude module: 'httpclient'
        exclude module: 'commons-logging'
    }
}

1
如果我们的代码已经在使用这个库,那么该怎么办? - Muhammad Saqib
然后,您需要找到导致重复httpclient导入的特定库,并需要从该特定库中删除传递的httpclient依赖项。 - srs
1
在我的情况下,org.apache.http.client.HttpClient 是导致冲突的原因,因为 Android 现在自己提供这个库。幸运的是,我找到了一个克隆的 Github 仓库 org.apache.http.client.HttpClient,并使用了不同的命名空间,解决了我的问题。 - Muhammad Saqib
1
这是克隆版本,以防有人想使用 implementation group: 'cz.msebera.android', name: 'httpclient', version: '4.4.1.2' - Muhammad Saqib
谢谢 @MuhammadSaqib,你的克隆解决方案(. implementation group: 'cz.msebera.android', name: 'httpclient', version: '4.4.1.2')救了我的一天。 - Parsania Hardik

13

我遇到了同样的问题。我已经做了以下更改

 configurations {
    all{
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents'
    }
}


packagingOptions {
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'org/apache/http/version.properties'
    exclude 'org/apache/http/client/version.properties'
}

把这段代码放在哪里?放在应用程序级别的Gradle还是项目级别的Gradle中?@Raja Peela - Pratik Saluja
@PratikSaluja Android中的应用程序级别 {} - Raja Peela
对我来说,它在 Android 代码块之外运行起来很奇怪。 - Pratik Saluja

9
你应该将“compile”替换为“implementation”,因为在最新的gradle中已经弃用,并且从Google API客户端库中排除“org.apache.httpcomponents”。
implementation('com.google.api-client:google-api-client-android:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}
implementation('com.google.http-client:google-http-client-gson:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}

这个解决方案可以在此处找到:https://developers.google.com/google-apps/activity/v1/quickstart/android


编译:为每个包含当前模块的库进行编译。 实现:为当前模块编译库,这意味着已实现的库对“父”模块不可见。 - Vahe Gharibyan
但是在新的Android Studio中,编译已经被弃用了,因此您会收到警告,建议将其替换为“implementation”或“api”,具体取决于您的需求。 - khammami

8

在终端中运行,在项目文件夹内:

./gradlew app:dependencies > dependencies.txt

然后检查 dependencies.txt 文件,找出使用冲突依赖项的人,并根据情况采取行动(检查更新、摆脱它或按 @Silverstorm 建议使用 exclude)。


注意app:dependencies - 这里的 app 是您的应用程序模块的名称,例如 myAwesomeApp - Kirill Karmazin

2
如果您想继续使用 async-http,请仅在 app/build.gradle 中添加以下代码。
configurations {
    all {
        exclude module: 'commons-logging'
    }
}

1
由于SDK版本23之后,'org.apache.httpcomponents:httpclient:4.3.3'已被弃用,请使用以下内容进行替换:
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

带有。
compile 'org.apache.httpcomponents:httpclient:4.3.3'

1
我今天收到了这两个错误。
1. commons-logging defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.

2. httpclient defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.

经过一段时间的努力,我发现是我正在使用的Firebase库导致了这些错误。

implementation platform('com.google.firebase:firebase-bom:29.2.0')

所以,我进行了更新:
implementation platform('com.google.firebase:firebase-bom:29.2.1')

我执行了清除缓存并重新启动项目,结果完美解决。

Earlier BOM version of Firebase was also working fine.

implementation platform('com.google.firebase:firebase-bom:29.1.0')

Please don't update Firebase BOM version: 29.2.0


0

我按照上面的建议删除了commons-logging,结果在某些手机上崩溃了,错误信息为“Fatal Exception: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory;”。当Android API不包含任何这些类时,Android怎么能声称commons-logging与其API冲突呢?!在https://developer.android.com/reference/packages中没有org.apache.commons.logging :facepalm:

我已经将implementation 'commons-logging:commons-logging:1.0.4'添加回build.gradle - Android Studio用红色下划线标记它,但gradle编译很顺利。 :facepalm:

Android :triple_facepalm:


那些类并没有作为公共 API 公开,尽管它们被用作实现的一部分。如果想要了解这种冲突的示例,请查看此问题 - Alex Lipov

0
如果你由于依赖于 org.apache.httpcomponents:httpmime 而遇到此问题,则请在你的应用级别 build.gradle 文件中使用以下内容:
implementation('org.apache.httpcomponents:httpmime:4.5.12') {
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
implementation "org.apache.httpcomponents:httpcore:4.4.13"

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