commons-logging 定义 类 与 类 冲突,现在由 Android 在 Android Studio 更新后提供

commons-logging defines classes that conflict with classes now provided by Android after Android Studio Update

我已经将 Android Studio 更新到版本 3,现在似乎无法正确编译我之前编译过的项目。

报错信息如下

Error:Error: 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. [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 and\or 包含的 com.android.tools.build:gradle:3.0.0 插件的错误,还是我遗漏了什么?跟之前的版本编译完全一样的项目没问题

添加到位于应用程序模块

中的 build.gradle
configurations {
    all {
        exclude module: 'httpclient'
    }
}

如果问题出在 commons-logging 上,则必须将其排除。 在app/build.gradle

中添加如下代码
configurations {
    all {
        exclude module: 'httpclient'
        exclude module: 'commons-logging'
    }
}

您应该将 "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

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

./gradlew app:dependencies > dependencies.txt

然后检查 dependencies.txt 以找出谁在使用冲突的依赖项并采取相应措施(检查更新,删除它,或按照@Silverstorm 的建议使用排除)

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

 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'
}

如果您想继续 async-http 则仅在 app/build.gradle

中添加以下代码
configurations {
    all {
        exclude module: 'commons-logging'
    }
}

由于 'org.apache.httpcomponents:httpclient:4.3.3' 在 SDK 版本 23 之后被弃用,所以

替换为:

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'

我按照上面的建议删除了 commons-logging, 当然 它在某些 phone 和 Fatal Exception: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory; 上崩溃了。当 Android API 不包含任何 类 时,Android 怎么能声称 commons-logging 与 Android 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:

添加这个然后同步你的 gradle

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

就我而言 android 工作室无法识别“httpclient” 所以我不能使用@Silverstorm 的回答。

而是找到了另一个答案: Error: json defines classes that conflict with classes now provided by Android

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

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

如果您因为 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"

我今天收到这两个错误。

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')

还有 Invalidated Caches and Restarted 这个项目,它非常有效。

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