Android Studio: Resolving Duplicate Classes

…衆ロ難τιáo~ 提交于 2019-11-28 10:14:35

its because you have added some Library two times in libs folder, this could happen sometimes when you have multiple versions of same library in libs folder. Check it and remove any duplicate jar file.

and second option could be you have also added the dependency in gradle.build and also have jar in libs folder.

So check both places and remove duplicate entries and the clean and build APK again.

Hope it helps

David Cheung

In my case, I am using sensorocloud.jar and the compile 'com.loopj.android:android-async-http:1.4.9' in my gradle which caused the same error as yours. Because sensoro cloud SDK used loopj's async-http.

I managed to solve it by manually removing the duplicate .class files in the jar file. (i.e.

  1. changing the extension from jar to zip
  2. extract it
  3. remove the com.loopj.android .class files)

(P.S. I have tried to search through the web to see if I could exclude certain class of a jar in gradle, but not succeed, e.g. I referenced this SO post)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!