Android Studio:解决重复类 [英] Android Studio: Resolving Duplicate Classes

查看:77
本文介绍了Android Studio:解决重复类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 Android 设备上run我的 android 应用程序时,gradle 控制台报告以下错误:

错误:任务:app:transformClassesWithJarMergingForDebug"的执行失败.>com.android.build.api.transform.TransformException:java.util.zip.ZipException:重复条目:com/loopj/android/http/AsyncHttpClient$1.class

当我搜索AsyncHttpClient"类时,我发现它确实在两个不同的位置被找到:

/Users/Afflatus/.gradle/caches/modules-2/files-2.1/com.loopj.android/android-async-http/1.4.9/5d171c3cd5343e5997f974561abed21442273fd1/android-async-9-sources.jar!/com/loopj/android/http/AsyncHttpClient.java/Users/Afflatus/.ideaLibSources/android-async-http-1.4.9-sources.jar!/com/loopj/android/http/AsyncHttpClient.java

第一条路径似乎表明它是一个缓存"文件......所以我尝试过 使 & 无效重新启动我的缓存,但是在 gradle 重建后这两个文件仍然存在,我尝试 run 应用程序.我在替代

这使我的应用程序文件夹看起来像这样:

但不幸的是,这并没有帮助我仍然遇到同样的错误.我究竟做错了什么?我该怎么办?

解决方案

有时重复的类异常意味着您的依赖项之一隐式使用了您在项目中也使用的某个库的较旧或较新(带有 +)版本,

要解决此问题,您可以将这样的代码块(将您的库版本放在 'force' 之后)添加到您的 build.gradle 文件(模块:app)中:

配置{全部 {解析策略{//不要升级到 3.12.0 以上以支持 API <21 而服务器使用//COMPATIBLE_TLS,或者项目中使用的是okhttp3强制'com.squareup.okhttp3:okhttp:3.12.0'强制 'com.squareup.okhttp3:logging-interceptor:3.12.0'}}}

您也可以从您的依赖项中排除某些组.对于单个依赖项,您可以这样写:

依赖项{//例子实现('log4j:log4j:1.2.15'){排除组:'javax.jms',模块:'jms'}}

经测试可在 Android Studio 上使用 Gradle 插件版本 3.4.2 和 Gradle 版本 5.4.1.感谢来自 gradle org 讨论组的 Dimitar Dimitrov 和 Schalk Cronjé

When I try to run my android application on an Android device, the gradle console reports the following error:

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/loopj/android/http/AsyncHttpClient$1.class

When I search for the "AsyncHttpClient" class, I see that it's indeed being found in two separate locations:

/Users/Afflatus/.gradle/caches/modules-2/files-2.1/com.loopj.android/android-async-http/1.4.9/5d171c3cd5343e5997f974561abed21442273fd1/android-async-http-1.4.9-sources.jar!/com/loopj/android/http/AsyncHttpClient.java

/Users/Afflatus/.ideaLibSources/android-async-http-1.4.9-sources.jar!/com/loopj/android/http/AsyncHttpClient.java

The first path seems to suggest it's a "cache" file... so I've tried invalidating & restarting my cache, but both files are still there after the gradle gets rebuilt and I try to run the application. I've read in alternate posts that it can be resolved by deleting one of the files... So I went to the cache location and deleted all the files found in the "1.4.9" folder... unfortunantly after reopening Android Studio, a new cache file gets created and I get the same error.

Other posts (here, here,here, and here) suggest if I add "./gradlew clean" to the root directory it would rebuild the gradle again just for the run (as far as I understand). So I tried doing that as well:

Which made my app's folder look like this:

But unfortunantly, that didn't help things I still get the same error. What am I doing wrong? What should I be doing?

解决方案

Sometimes duplicate classes exception means that one of your dependencies uses implicitly the older or newer (with +) version of some library you also use in your project,

To resolve this issue you may add such block of code (put your library version after 'force') to your build.gradle file (Module:app):

configurations {
   all {
      resolutionStrategy {
          // do not upgrade above 3.12.0 to support API < 21 while server uses
          // COMPATIBLE_TLS, or okhttp3 is used in project
          force 'com.squareup.okhttp3:okhttp:3.12.0'
          force 'com.squareup.okhttp3:logging-interceptor:3.12.0'
      }
   }
}

You may also exclude some group from your dependencies. For a single dependency you way write:

dependencies {

    // example
    implementation('log4j:log4j:1.2.15') {
        exclude group: 'javax.jms', module: 'jms'
    }
}

Tested to work on Android Studio with Gradle Plugin version 3.4.2 and Gradle version 5.4.1. Credits go to Dimitar Dimitrov and Schalk Cronjé from gradle org discussion group

这篇关于Android Studio:解决重复类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆