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
–
–
–
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.
–
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.
changing the extension from jar to zip
extract it
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)
–
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.)
misconfigured package name
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"
Re-create your BuildConfig and set it up well.
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.
Android Studio - after doing upgrade of gradle ':app:checkReleaseDuplicateClasses' task failed
See more linked questions