Android Studio: Resolving Duplicate Classes

Afflatus picture Afflatus · May 2, 2016 · Viewed 46.2k times · Source

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:

enter image description here

Which made my app's folder look like this:

enter image description here

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

Answer

Max picture Max · May 2, 2016

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