90

I recently updated my Android Studio (and I'm pretty sure the Gradle version), and now I've been getting a bunch of errors when trying to compile my project. Here's the one that is plaguing me at the moment:

Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:26.1.0)

This is followed by lots of similar ones.

I tried removing all uses of com.android.support in favor of androidx (see here for what I was using the replace things), but com.android.support libraries are still being used, even when I delete the libraries (they're just remade once I try to compile again).

Here's a link to the full error I get.

Boken
  • 4,825
  • 10
  • 32
  • 42
TheKingElessar
  • 1,654
  • 1
  • 10
  • 30
  • did you invalidate cache and restart? also you might need to look into xmls and make sure support library controls are replaced with androidx controls. – karan Apr 19 '19 at 04:50
  • Same Error for me as well, the only thing different for me is, i am trying to add a 3rd party dependency, now i have tried to exclude the duplicate classes using configurations, but it didn't made any difference – Kashif K. Apr 19 '19 at 05:33
  • @KaranMer I haven't invalidated the cache, but I've tried cleaning and rebuilding and restarting several times. I'm trying hannes ach's answer now, and then if that doesn't help I'll try what you suggested. Can you explain the last part of it? What do you mean by "controls?" I'm still fairly new with this sort of thing. – TheKingElessar Apr 19 '19 at 14:51
  • @TheKingElessar had you get the solution of this issue ,I also get the same issues.please suggest me if you have any suggestion. – Vishwa Pratap Apr 25 '19 at 11:21
  • @VishwaPratap Mine was solved by this answer here: https://stackoverflow.com/a/55756749/7492795 – TheKingElessar Apr 25 '19 at 16:32
  • How to use old android support libraries with Androidx Security? – Ashish M Mar 02 '20 at 06:25
  • @M.Ashish I can't help you with that, but I think you should probably make a new question since this one isn't active anymore. – TheKingElessar Mar 03 '20 at 05:03

6 Answers6

174

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

then you see from where it comes.

hannes ach
  • 16,247
  • 7
  • 61
  • 84
  • 3
    Wow, that did it! I assume that `android.useAndroidX=true` completely blocked any `com.android.support` usage, which then allowed me to find where the usage was (because of the new error from its usage)! My app still crashes, but at least it compiles now! :D – TheKingElessar Apr 19 '19 at 15:15
  • 4
    For those who can't find gradle.properties in the root project directory, just create one file named "gradle.properties" and then inside it add "android.useAndroidX=true android.enableJetifier=true". This fixed my issue. – Hitesh Bisht May 04 '19 at 11:05
  • 1
    I'm using React Native and enableJetifier spits out much more errors. Just for anyone visiting: this does not work for React Native projects where some project dependencies rely on support library. – Can Poyrazoğlu Jun 26 '19 at 19:22
  • 2
    I don't want to migrate androidX then? – Prince Dholakiya Jul 22 '19 at 05:28
  • @Prince Unless you're referring to what Can Poyrazoglu said, you probably do want to migrate. – TheKingElessar Jul 26 '19 at 04:48
  • @DitaAjiPratama read the FIRST line "I hope you have in your gradle.properties these two lines: " – hannes ach Nov 28 '19 at 05:50
11

In your app build.gradle you are using a mix of old support library and new support library artifacts. Remove one of them from your build.gradle to resolve this issue.

Old artifact: com.android.support:support-compat

Corresponding new artifact: androidx.core:core:1.0.0

See this page for more information on migrating to AndroidX.

TheKingElessar
  • 1,654
  • 1
  • 10
  • 30
Ranjan Kumar
  • 1,164
  • 7
  • 12
7

Like others says, the solution is to migrating to 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
  • Finally, Build --> Clean Project and Build --> Rebuild Project
Boken
  • 4,825
  • 10
  • 32
  • 42
fvaldivia
  • 446
  • 5
  • 13
3

This solution from here worked the best for me. Migrating to androidX

https://developer.android.com/jetpack/androidx/migrate

With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar.

The refactor command makes use of two flags. By default, both of them are set to true in your gradle.properties file:

android.useAndroidX=true The Android plugin uses the appropriate AndroidX library instead of a Support Library. android.enableJetifier=true The Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries.

Jayesh Babu
  • 1,389
  • 2
  • 20
  • 34
3

If you face any issue regarding duplicate classes after using Facebook mediation and google ad libraries so follow these steps:

1- Add this in build.gradle(Module) in Top

apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.gms.google-services'

2- Add this in build.gradle(Module) -> android

 packagingOptions {
        exclude "META-INF/DEPENDENCIES"
    }

3- Add this in build.gradle(Module) -> dependencies

    //firebase bom
    implementation platform('com.google.firebase:firebase-bom:29.2.0')
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging:23.0.1'

    //ADS
    def ads_version = "20.6.0"
    implementation "com.google.android.gms:play-services-ads:$ads_version"

    //facebook ads
    implementation 'com.google.ads.mediation:facebook:6.8.0.0'

4- Add this in build.gradle(Project )-> buildscript-> dependencies

classpath "com.android.tools.build:gradle:7.0.4"
classpath 'com.google.gms:google-services:4.3.10'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'

5- Add this in gradle.wrapper.propertise

#Wed Dec 16 15:32:03 PKT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-7.0.2-bin.zip
Hamza Khalid
  • 191
  • 1
  • 7
0

i had the same problem in my ionic project, after uninstalling android.support.v4 and build app works fine without any issues.

apart from it, have these properties as below in your gradle

android.useAndroidX=true android.enableJetifier=true

and also if you want to migrate to androidx, you can do so from Refactor -> Migrate to AndroidX

to build successfully.

Raj
  • 21
  • 2