【问题标题】:commons-logging defines classes that conflict with classes now provided by Android after Android Studio Updatecommons-logging 定义了与 Android Studio 更新后 Android 现在提供的类冲突的类
【发布时间】:2018-04-09 21:38:09
【问题描述】:

我已将 Android Studio 更新到版本 3,但现在似乎无法编译我之前编译的项目而没有错误。

错误信息如下

Error:Error: commons-logging 定义的类与 现在由 Android 提供的类。解决方案包括寻找更新的 没有相同问题的版本或替代库 (例如,对于 httpclient 使用 HttpUrlConnection 或 okhttp 代替), 或使用 jarjar 之类的东西重新打包库。 [重复平台类]

依赖关系是

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 插件还是我遗漏了什么?与以前的版本编译完全一样的项目没问题。

【问题讨论】:

  • "那为什么它不编译?" -- 你的问题是commons-logging,基于错误。也许您应该使用这些 com.google 依赖项的更新版本。
  • @CommonsWare我没有找到任何更新的库这些版本是 2017 年 10 月的最新版本
  • libs 文件夹中是否有相同的 jar?
  • @GabrieleMariotti 绝对不是。我在 libs 文件夹中没有任何罐子。这些库仅在 build.gradle 的依赖项部分中指定(否则以前的 Android 工作室也会拒绝构建)。出于这个原因,我无法弄清楚为什么 Android Studio 更新后无法编译。

标签: android android-studio android-gradle-plugin android-studio-3.0 android-gradle-3.0


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在我的情况下,android studio 无法识别“httpclient” 所以我不能使用@Silverstorm 答案。

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

    这意味着在 app build.gradle 中添加以下内容:

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

    【讨论】:

      【解决方案3】:

      添加这个然后同步你的 gradle

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

      【讨论】:

        【解决方案4】:

        我按照上面的建议删除了 commons-logging,当然它在某些带有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:

        【讨论】:

        • 这些类不作为公共 API 公开,尽管它们被用作实现的一部分。要查看此类冲突的示例,请查看this issue。
        【解决方案5】:

        因为 'org.apache.httpcomponents:httpclient:4.3.3' 在 SDKversion 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'
        

        【讨论】:

          【解决方案6】:

          如果您想继续使用async-http,请仅在app/build.gradle 中添加以下代码

          configurations {
              all {
                  exclude module: 'commons-logging'
              }
          }
          

          【讨论】:

            【解决方案7】:

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

             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 中? @Raja Peela
            • @PratikSaluja android 内的应用级别{}
            • 奇怪的是它在 android 块之外工作。
            【解决方案8】:

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

            ./gradlew app:dependencies > dependencies.txt
            

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

            【讨论】:

            • 注意:app:dependencies - 这里app 是您的应用模块的名称,例如myAwesomeApp
            【解决方案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

            【讨论】:

            • compile:为每个包含当前模块的库编译。实现:为当前模块编译库,这意味着实现的库对于“父”模块不可见
            • 但编译在新的 Android Studio 中已被弃用,因此您会收到一条警告,将其替换为“实现”或“api”&这取决于您的需要
            【解决方案10】:

            如果问题出在公共日志记录上,那么它也必须被排除。 在 app/build.gradle 中添加如下代码

            configurations {
                all {
                    exclude module: 'httpclient'
                    exclude module: 'commons-logging'
                }
            }
            

            【讨论】:

            • 这将排除org.apache.http.client.HttpClient 如果我们的代码已经在使用这个库怎么办?
            • 那么您需要找到导致此重复 httpclient 导入的特定库,并需要从该特定库中删除传递 httpclient 依赖项。
            • 在我的例子中,org.apache.http.client.HttpClient 导致了冲突,因为 android 现在自己提供了这个库。幸运的是,我找到了一个 org.apache.http.client.HttpClient 的克隆 github repo,它具有不同的名称空间,解决了我的问题。
            • 这里是克隆,以防万一有人想使用implementation group: 'cz.msebera.android', name: 'httpclient', version: '4.4.1.2'
            【解决方案11】:

            添加到位于应用模块中的build.gradle

            configurations {
                all {
                    exclude module: 'httpclient'
                }
            }
            

            【讨论】:

            • 如果排除 httpclient,我无法获得发布 apk
            • @RameshSambu 上面的代码有没有放在app模块的内部build.gradle中?
            • 是的,我是这样添加的
            • 如果我使用httpclient怎么办?我的代码用于在升级到 Android Studio 3.0 之前进行编译。我实际上在我的代码中使用了 httpclient,我不想切换到 okhttp。
            • 配置{全部{排除模块:'httpclient'排除模块:'commons-logging'排除组:'org.json',模块:'json'排除组:'org.apache.httpcomponents'排除模块:'opengl-api' 排除模块:'xmlParserAPIs' 排除模块:'xpp3' } } 这对我有用
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-06-18
            • 2021-12-08
            • 2018-10-22
            • 2015-11-22
            • 2019-11-11
            相关资源
            最近更新 更多