26

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Afflatus
  • 2,302
  • 5
  • 25
  • 40
  • 2
    May I know how did you solve your problem, I got exactly the same problem as yours. – chubao Aug 10 '16 at 10:25
  • 1
    @DavidCheung I don't think I solved it. I think I tried Anders' suggestions but nothing seemed to work for my particular case. Maybe it will for yours? – Afflatus Aug 10 '16 at 13:18
  • you can find good materials here :https://stackoverflow.com/questions/56029393/why-im-getting-duplicate-class-when-running-my-android-project/56029604#56029604 – A Farmanbar Nov 15 '19 at 00:27

8 Answers8

26

I added this line to my gradle.properties file and my app worked

android.enableJetifier=true

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Anas Ansari
  • 405
  • 4
  • 6
15

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

Denis Dmitrienko
  • 1,532
  • 2
  • 16
  • 26
  • how to use `implementation project(path: ':videocropper')` after force it's not working with project path.. Please tell me how to make work with it. – Sayyed Rizwan May 05 '20 at 16:10
  • According to the logs in your question here: https://stackoverflow.com/questions/61619084/how-to-fix-duplicate-class-error-in-gradle try to exclude group: "com.googlecode.mp4parser", module: "isoparser" as described above and write back if it helps – Denis Dmitrienko May 06 '20 at 17:52
  • What is jms and why are you excluding it, how is it related to the original error? Why is the module jms? Why is the group javax.jms? Where did this information come from? – toby yeats Jan 20 '22 at 19:31
10

That's because you have added some library two times in libs folder, this could happen sometimes when you have multiple versions of the same library in the libs folder. Check it and remove any duplicate jar files.

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

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

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Atiq
  • 14,435
  • 6
  • 54
  • 69
3

Delete files with duplicate jar extensions in the libs folder. However, if there are no duplicate files and there is still a "Duplicate classes" error, look for the name in the rest of the "Duplicate classes ...." clause in the error section. For example, "duplicated classes 'dagger' bla bla". Delete the file named 'dagger' from the libs folder. (Be careful not to delete it with shift.)

Geylani ARCA
  • 308
  • 1
  • 11
2

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)

Community
  • 1
  • 1
chubao
  • 5,871
  • 6
  • 39
  • 64
  • Does this work for you to exclude a class packagingOptions { exclude 'okhttp-2.0.0.jar' exclude 'crashlytics.jar' exclude 'DimenRes.class' exclude 'support-v4' exclude 'AccessToken$1.class' } – amdstorm Sep 12 '16 at 13:59
  • I do not think it is related to the exclusion you mentioned. – chubao Sep 12 '16 at 14:03
0

This error can be caused by several things;

  1. misconfigured package name
  2. Activity views that is not well binded. - simply go to your launcher activity view and ensure context is defined well e.g "com.yourdomain.package"
  3. Re-create your BuildConfig and set it up well.
kham Ham
  • 5
  • 1
-1

Check if your project build.gradle. There it might be some maven duplicate dependency

Ishtdeep Hora
  • 310
  • 2
  • 4
-1

Here's another situation that can cause duplicate class during the mergeDexClasses task. This can happen with later versions of android gradle.

If your build.gradle.kts script has a dependency in the form:

implementation(project(":mylib", configuration="default"))

that can cause duplicate classes. The correction is simple. Just change it to:

implemenation(project(:mylib"))

Here's the Android Studio's Team explanation:

Having both project(":lib") and project(path: ":lib", configuration: "default") in the runtime classpath means that AGP gets both build/classes/java/main and build/libs/lib.jar (run ./gradlew :lib:outgoingVariants --all to verify). Because paths differ, we'll get 2 dexing transforms happening: 1 incremental that produces dex per class under build/.transforms (the one processing dir) and another one which produces single dex (the one processing jar). Later on during merging this causes failure. AGP never publishes to the default configuration, in fact java-library plugin does it only so it does not break older build scripts. Having an explicit configuration name used in the dependency declaration is discouraged and Gradle attributes should be used instead.

In an older version of AGP, I ran into a problem where adding the configuration value "default" fixed some issue I was having. Well that no longer works, and adding the "default" configuration you can get duplicate classes.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15