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 theexclude
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
Checkbox Unchecked When I Scroll Listview on Android
Get Text from Web Page to String
Android: Creating Custom Preference
Error Message 'Java.Net.Socketexception: Socket Failed: Eacces (Permission Denied)'
How to Record Phone Calls in Android
Example Communicating with Handlerthread
How to Start Android Service on Installation
Resolved Versions for App (22.0.0) and Test App (21.0.3) Differ
How to Get Root Permissions Through the Android Sdk
Service Not Available While Calling Geocoder.Getfromlocation()
How to Save Geofire Coordinates Along with Other Items in Firebase Database
How to Autoplay HTML5 Mp4 Video on Android
Getrunningtasks Doesn't Work in Android L
Remove Listview Items in Android
How to Set Edittext to Only Accept Numeric Values in Android
Notification to Restore a Task Rather Than a Specific Activity