196

I am developing a news app but I am getting following errors in from gradle console

(androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.core.graphics.drawable.IconCompatParcelizer found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.core.internal.package-info found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.versionedparcelable.CustomVersionedParcelable found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.NonParcelField found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelField found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelImpl found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelImpl$1 found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelUtils found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel$1 found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel$ParcelException found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream$InputBuffer found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules class

below my app.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    implementation 'com.github.bassaer:chatmessageview:2.0.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Robert
  • 7,394
  • 40
  • 45
  • 64
Sweden Edgar
  • 2,039
  • 2
  • 8
  • 8
  • Possible duplicate of [Execution failed for task ':app:checkDebugDuplicateClasses'. Ionic4 Android](https://stackoverflow.com/questions/56716047/execution-failed-for-task-appcheckdebugduplicateclasses-ionic4-android) – Top-Master Jun 29 '19 at 04:32
  • Your project (or one of its sub-projects) is referring to a dependency using the **+** plus-sign at its end; check the answer for [Execution failed for task ':app:checkDebugDuplicateClasses'](https://stackoverflow.com/a/56736684/8740349) (although that question is about Ionic the answer should work). – Top-Master Jun 29 '19 at 04:51

13 Answers13

492

You can add below 2 lines into your gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

Note to check, to not repeat any line that already exists (and ensure existing are true).


Details:

Your project (or one of its sub-projects, or a dependency) is no longer using android.support libraries and instead is using androidx libraries, which causes conflicts if mixed with android.support libraries.

If you want to use androidx-namespaced libraries in an old project, you need to set the compile SDK to Android 9.0 (API level 28) or higher but below "API level 31", and set both of the mentioned Android Gradle plugin flags to true.

android.useAndroidX: When this flag is set to true, the Android plugin uses the appropriate AndroidX library instead of a Support Library. The flag is false by default if it is not specified.

android.enableJetifier: When this flag is set to true, the Android plugin automatically migrates existing third-party libraries to use AndroidX dependencies by rewriting their binaries. The flag is false by default if it is not specified.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Anice Jahanjoo
  • 7,088
  • 3
  • 20
  • 29
  • 3
    Can you add a little more information about what exactly this is doing? – Nathan F. May 27 '19 at 23:52
  • 3
    @NathanF. this is because of mixing support libraries. by adding these lines androidX selected as your support library – Anice Jahanjoo May 28 '19 at 11:40
  • 3
    This will just forcibly replace any dependency on `android.support.*` with `androidx.*` dependencies (but for me, it just resulted in a compile error because `android.support` was used in the codes), in case anyone is seeking a fix instead of a workaround check the answer for [Execution failed for task ':app:checkDebugDuplicateClasses'](https://stackoverflow.com/a/56736684/8740349) (although that question is about Ionic the answer should work). – Top-Master Jun 29 '19 at 04:46
  • same error for me. and I am not using androidX Library but by default is added while compiling. please help me. – Er.nishka Jul 17 '19 at 04:42
  • thank you so much. I've been battling a monster series of version errors for hours and this seems to be the last of them. I explicitly had `android.enableJetifier=false` for some reason. – sam-pyt Aug 13 '19 at 17:32
  • Adding these lines in my project, generated new errors. For example, "package android.support.annotation does not exist", "cannot find symbol class Fragment", "package android.support.v4.content does not exist", etc. – Pablo Alfonso Sep 24 '19 at 16:05
  • @PabloAlfonso if enable jetifier support does not work for you and you should use androidX version of this object such as fragment, etc. – Anice Jahanjoo Sep 30 '19 at 10:35
  • 2
    @nicej I was able to solve the issue by selecting Refactor > "Migrate to AndroidX" from the menu bar. Thanks. – Pablo Alfonso Oct 01 '19 at 11:52
  • if no have gradle.properties u must create in root – Fortran Jun 04 '21 at 17:59
  • not fix, I stay on LinearLayout and didn't use ConstraintLayout ;( – Fortran Jun 04 '21 at 18:46
  • @Fortran It doesn't matter to constraint layout. – Anice Jahanjoo Jun 08 '21 at 03:56
  • @AniceJahanjoo yeah), but didn't find "constraint layout" without androidX in my case – Fortran Jun 09 '21 at 06:15
  • @Fortran yes you exactly right but this problem is related to the whole androidX not just a component like constraint layout. – Anice Jahanjoo Jun 09 '21 at 06:22
  • yeah I know)))) – Fortran Jun 10 '21 at 14:23
  • 2
    Perhaps this resolution shouldn't be your first choice guys, it tends to slow down project builds, as stated in the docs: "Caution: As of late 2021, most of the library ecosystem already supports AndroidX natively. This means that your project is most likely already using AndroidX libraries directly and there is no need to follow the steps in this migration guide. Additionally, the enableJetifier flag mentioned in this guide can lead to slower build times and should not be used unless it’s necessary." – Filipe Bezerra de Sousa Jun 25 '22 at 18:49
  • 1
    https://developer.android.com/jetpack/androidx/migrate – Filipe Bezerra de Sousa Jun 25 '22 at 18:50
  • 1
    Instead you should try to use gradle ResolutionStrategy API to solve this problem: https://developer.android.com/studio/build/dependencies#custom_dep_resolutions – Filipe Bezerra de Sousa Jun 25 '22 at 18:52
35

Adding these lines to gradle.properties file solves "Duplicate Class" errors:

android.useAndroidX=true

android.enableJetifier=true

However, this generated new errors in my project:

"package android.support.annotation does not exist"

"cannot find symbol class Fragment"

"package android.support.v4.content does not exist"

"Program type already present"

etc.

However, the refactoring worked for me:

Migrate your project to AndroidX by selecting Refactor > 
"Migrate to AndroidX" from the menu bar.

Select Build > Clean project

Restore Android Studio

Now, my project (Match4app) works with AndroidX and I was able to publish it in the PlayStore without any issues.

Comment: This task also allowed me to upgrade all other libraries that depend on AndroidX (i.e. com.firebaseui:firebase-ui-auth:6.0.2, com.google.android.gms:play-services-games:18.0.1, com.google.android.gms:play-services-auth:17.0.0, com.google.android.gms:play-services-ads:18.2.0, etc.).

Pablo Alfonso
  • 2,317
  • 1
  • 18
  • 24
15
I run into something like this, and based on my other answer:

Your project (or one of its sub-projects) is referring to a dependency using a + (plus-sign) at its end, like com.google.firebase:firebase-auth:+, which means, use any higher version when possible, and that newer version is no longer using android.support libraries and instead is using androidx; to fix this issue, either follow the below steps, or, update to androidx by following the other answer.

Steps:

  1. Ensure the ANDROID_HOME environment-variable is set, and then, open a console app (but for Windows, use git-bash), and cd into your android directory (for Ionic projects it should be platforms/android).

  2. First, List all dependencies into a file, by running below (in said console):

    ./gradlew :app:dependencies > my-list.txt
    

    Note to change :app part with another Gradle module's name, if above does not work.

    See list of said module names, like:

    ./gradlew -q projects
    
  3. Open the resulting my-list.txt file into your preferred text-editor, and search for androidx.

  4. If you found something follow below steps, else you are done! (and you do not need to repeat these steps).

  5. Scroll up until you see -> at the end of any line, like for example 16.0.8 -> 19.0.0 or + -> 19.0.0, which both mean that the version was auto-resolved (to something higher than specified by you because of +).

  6. Hence, set the version down manually:

    • When possible, in your project find and replace the + sign with a specific version.
    • Or, force a specific version of the dependencies like mentioned below.
  7. At last, repeat above steps (but just skip step one, unless you closed said console).


To Force specific version of the dependencies add to your root build.gradle file something like below (which is what worked for me), but of course edit below and add your own rules (because these might not work for your case):

allprojects {
  // ...
  configurations.all {
    resolutionStrategy {
      force 'com.google.firebase:firebase-common:17.0.0'
      force 'com.google.android.gms:play-services-basement:16.2.0'
      force 'com.google.firebase:firebase-iid:16.0.0'
      force 'com.google.firebase:firebase-auth:17.0.0'
    }
  }
}
Top-Master
  • 7,611
  • 5
  • 39
  • 71
14

I was having this same issue and this is how i resolved it

Expand Graddle Scripts and then Go to graddle.properties and add these 2 lines

Note: Maybe the first line android.useAndroidX=true is already there so do not repeat it.

android.useAndroidX=true android.enableJetifier=true

9

Just add these 2 lines in gradle.properties file,

android.useAndroidX=true

android.enableJetifier=true

Now your project will not have any duplicate class error and will work fine,

Thank you

9

Solved Just set your Gradle properties this way

ilidiocn
  • 323
  • 2
  • 5
5

I had the same issue.

android.enableJetifier=true

just paste this line in the gradleproperties file.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 05 '23 at 13:38
3

enter image description here

Taken from here. AndroidX is the open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack.

Ahmed
  • 2,966
  • 7
  • 42
  • 69
3

Like @Ahmed says, the solution is to implement AndroidX, it works for me. However, it isn´t an easy way and it requires a bit of pacience... These are the steps that I did:

  • First, is very important that you do all this changes in a different branch or you make a backup of your project.

  • You need to have the Android Gradle Plugin Version 3.5.1. So, in build.gradle set: dependencies { classpath 'com.android.tools.build:gradle:3.5.1' ...

  • Migrate to AndroidX using Android Studio Tool : Refactor --> Migrate to AndroidX...
  • When it finishes, it has done all pertinents modification, but posibly you can´t deploy the project correctly because you find any errors. These are the problems that I found and the solutions:

  • If you use Kotlin, in build.gradle set: buildscript { ext.kotlin_version = '1.3.10' ... and compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

  • If you use destination method, add "file" parameter: destination file("$reportsDir/checkstyle/checkstyle.xml")
  • If you use Butterknife, use 10.0.0 version
fvaldivia
  • 446
  • 5
  • 13
2

I was having this same issue and this is how i resolved it

android.enableJetifier=true
vimuth
  • 5,064
  • 33
  • 79
  • 116
1

Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency

android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

dependencies {
    implementation "androidx.multidex:multidex:2.0.1"
}

Dev Dox
  • 11
  • 1
0

Top-Master's answer only work if you can downgrade to a specific version. In my case, I have a React Native app and one of the libraries I was using had migrated to AndroidX. The previous version had issues with iOS, so I had to use the most recent version. What I had to do was:

  1. Migrate the Android project to AndroidX (Refactor > Migrate to AndroidX).
  2. Use jetify to convert node_module dependencies to AndroidX. For React Native 0.60 and above, jetify is run automatically, so you don't have to install this library.
ataravati
  • 8,891
  • 9
  • 57
  • 89
0

android.nonTransitiveRClass=true

add this on gradle properties

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 15 '22 at 00:10