Why I'm Getting Duplicate Class When Running My Android Project

Why I'm Getting Duplicate Class When Running My Android Project

The exception means, There were duplicated classes in 2 or more different dependencies and the compiler wouldn't be able to distinguish which of them should be used at run-time and the exception was thrown.

Most often, Duplicity happens when you are trying to import modules that carrying their required libraries. (transitive dependencies)

You have to exclude duplicated classes from libraries in the build.gradle.
As Log shows, support-core-ui and support-compat modules have duplicated classes.

apply plugin: 'com.android.application'

android {
...
defaultConfig {
...
}
buildTypes {
...
}
configurations {
all { // You should exclude one of them not both of them
exclude group: "com.android.support", module: "support-core-ui"
exclude group: "com.android.support", module: "support-compat"
}
}
}

Sometimes you don't need to exclude anything and you only need to change the imported module to that one that does not bring its dependencies.

Other situation that causes duplicated classes is when you have added *.jar to the project libs directory. Therefore, You need to delete them if they are not begin used in the project.

project->app->libs->*.jar

I see there are some solutions mentioned using these 2 lines will resolve the problem But if you've migrated to Androidx it would be enabled by default.

android.useAndroidX=true
android.enableJetifier=true

Jetifier is

Jetifier tool migrates support-library-dependent libraries to rely on
the equivalent AndroidX packages instead. The tool lets you migrate an
individual library directly, instead of using the Android gradle
plugin bundled with Android Studio.

And for more information take a look at Exclude transitive dependencies

As an app grows in scope, it can contain a number of dependencies
including direct dependencies and transitive dependencies (libraries
which your app's imported libraries depend on). To exclude transitive
dependencies that you no longer need, you can use the exclude
keyword

If you have problems excluding classes, check this thread: How do I exclude...

Duplicate class caused by Android Gradle plugin

Managed to fix this by using https://github.com/shevek/jarjar and removing the duplicate classes using the below.

implementation jarjar.repackage {
from 'com.nukkitx.fastutil:fastutil-common:8.3.1'

classDelete "it.unimi.dsi.fastutil.ints.IntIterator"
classDelete "it.unimi.dsi.fastutil.longs.LongIterator"
classDelete "it.unimi.dsi.fastutil.objects.ObjectIterator"
}

Any ideas on how to solve Duplicate class with android jetified?

This is now solved. I contacted the maintainers of the libraries, they added flattenpackagehierarchy to proguard which solved it.

Duplicated classes found in modules classes.jar

If there are duplicates, use exclude:

implementation ('com.cxpublic:cxense-android:1.0.1') {
exclude group: "com.android.support", module: "support-v4"
}

Or remove implementation 'com.android.support:appcompat-v7:27.1.1' in favour of support-v4.

See: https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991

Duplicate classes from androidx and com.android.support

Add these lines to your gradle.properties

android.useAndroidX=true
android.enableJetifier=true

If gradle.properties is absent, create the file in the root directory of your project.

It looks like you have some support relics anywhere in your dependencies, maybe from a lib? To investigate and see the dependencies tree, please run:

./gradlew :yourApp:dependencies

and then you see from where it comes.



Related Topics



Leave a reply



Submit