60

I'm currently working on an ionic4 application, but recently it stopped working while building the application on an Android real device after adding https://ionicframework.com/docs/native/fcm plugin to the application.

At first, it was complaining about the fabric key, however, I never aimed to use fabric crashlytic on my app.

The error logs:

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

  Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 21s

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings
35 actionable tasks: 5 executed, 30 up-to-date
cmd: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

  Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 21s
[ERROR] An error occurred while running subprocess cordova.

        cordova run android exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.

android/app/build.gradle

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' } // Fabrics Maven repository from cordova-plugin-firebase
    }
    dependencies {

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.android.tools.build:gradle:3.4.0+'
        classpath 'com.google.gms:google-services:4.2.0' // google-services dependency from cordova-plugin-firebase
        classpath 'io.fabric.tools:gradle:1.+' // fabric dependency from cordova-plugin-firebase
    }
}

allprojects {
    repositories {
      
        jcenter()
        google() // Google's Maven repository from cordova-plugin-firebase
    }
    //This replaces project.properties w.r.t. build settings
    project.ext {
      defaultBuildToolsVersion="28" //String
      defaultMinSdkVersion=19 //Integer - Minimum requirement is Android 4.4
      defaultTargetSdkVersion=28 //Integer - We ALWAYS target the latest by default
      defaultCompileSdkVersion=28 //Integer - We ALWAYS compile with the latest by default
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

cordova-plugin-firebase/raihan-build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
       classpath 'com.android.tools.build:gradle:3.4.0+'
        classpath 'com.google.gms:google-services:4.2.0'
    }
}


dependencies {
    compile 'me.leolin:ShortcutBadger:1.1.4@aar'
    compile 'com.google.firebase:firebase-auth:+'
    compile('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
       transitive = true
    }
}
cdvPluginPostBuildExtras.add({
    // apply plugin: 'com.google.gms.google-services'
    apply plugin: 'io.fabric'
})

cordova-plugin-fcm-with-dependecy-updated/raihan-FCMPlugin.gradle

buildscript {
    repositories {
       google()
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0+'
        classpath 'com.google.gms:google-services:4.2.0'
    }
}
repositories {
    google()
    jcenter()
}
dependencies {
    compile 'com.google.firebase:firebase-core:16.0.8'
}
// apply plugin: 'com.google.gms.google-services'
// class must be used instead of id(string) to be able to apply plugin from non-root gradle file
// apply plugin: com.google.gms.googleservices.GoogleServicesPlugin

android/project.properties

 target=android-27
    android.library.reference.1=CordovaLib
    android.library.reference.2=app
    cordova.gradle.include.1=cordova-android-support-gradle-release/raihan-cordova-android-support-gradle-release.gradle
    cordova.system.library.1=com.squareup.okhttp3:okhttp-urlconnection:3.10.0
    cordova.system.library.2=com.android.support:support-v4:24.1.1+
    cordova.system.library.3=com.soundcloud.android:android-crop:1.0.0@aar
    cordova.system.library.4=com.google.firebase:firebase-core:16.0.8
    cordova.system.library.4=com.google.firebase:firebase-iid:17.1.1
    cordova.system.library.5=com.google.firebase:firebase-messaging:17.5.0
    cordova.gradle.include.2=cordova-plugin-fcm-with-dependecy-updated/raihan-FCMPlugin.gradle
    cordova.gradle.include.3=cordova-support-google-services/raihan-build.gradle
    cordova.system.library.6=com.google.firebase:firebase-auth:16.2.0
    cordova.system.library.6=com.google.firebase:firebase-perf:16.2.4
    cordova.system.library.7=com.android.support:support-annotations:27.+
    cordova.system.library.8=com.android.support:appcompat-v7:23+
    cordova.system.library.8=com.google.firebase:firebase-iid:17.1.1
    cordova.system.library.8=com.google.android.gms:play-services-tagmanager:16.0.8
    cordova.system.library.8=com.google.firebase:firebase-config:16.4.0
    cordova.gradle.include.4=cordova-plugin-telerik-imagepicker/raihan-ignorelinterrors.gradle
    cordova.gradle.include.5=cordova-plugin-telerik-imagepicker/raihan-androidtarget.gradle
    cordova.gradle.include.6=cordova-android-support-gradle-release/raihan-cordova-android-support-gradle-release.gradle
    cordova.gradle.include.7=cordova-plugin-firebase/raihan-build.gradle

I have provided the above files, let me know if I need to upload anything else.

Abdu4
  • 1,269
  • 2
  • 11
  • 19

19 Answers19

52
  1. Go to gradle.properties(project properties)

  2. Add
    android.enableJetifier=true

  3. And also most of time android.useAndroidX=true is present.Check your gradle.properties(Project Properties) and if it does not exist then add
    android.useAndroidX=true -> Look Likes This Image

enter image description here

Suman
  • 529
  • 2
  • 5
  • 20
21

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
  • 3
    that's the solution which worked for me, you saved my day. – Abdu4 Jun 25 '19 at 04:16
  • 3
    an alternative that worked for me using cordova 8 is to enable AndroidX support by adding https://github.com/dpa99c/cordova-plugin-androidx and then adding which is a shim to update the dependencies – MStoner Sep 17 '19 at 11:26
  • 1
    I will never be able to thank you enough. I had to remove all androidx dependencies (which were almost all caused by firebase and a few other things). I also noted that there were a lot of top-level androidx dependencies (as if I included them) but they disappeared when I disabled firebase. They are some sort of "replection". Also, multidex is used implicitly by android studio when the "multiDexEnabled" is to true. Just use the 1.0.3 multidex which does not depend on androidx. Rince and repeat until you don't have any more androidx deps and then you are done – ci0ccarellia Jul 21 '22 at 16:00
14

Simply add below 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).

Finally, just rebuild your project.

But sometimes you may need to manually delete the build folder.


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
10

The issue I was facing was in the react-native something similar to

Execution failed for task ':app:checkDebugDuplicateClasses'.

1 exception was raised by workers: java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.0.jar (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.21.jar (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21) Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations found in modules jetified-kotlin-stdlib-1.8.0.jar (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.21.jar (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21)

It means that there is a conflict between two different versions of the Kotlin standard library, specifically the "jetified-kotlin-stdlib-1.8.0.jar" and "jetified-kotlin-stdlib-jdk8-1.6.21.jar" versions. The error message specifically states that there is a duplicate class "kotlin.collections.jdk8.CollectionsJDK8Kt" found in both versions.

and the solution was here there were two approaches but i found this one easier go to android/app/build.gradle and update the dependencies

dependencies {
constraints {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
        because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
    }
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
        because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
    }
}
Dev Gaud
  • 676
  • 6
  • 10
8

I came across the same problem and My solution is as follows :

  1. Go to gradle.properties files

  2. Add these two lines :

    android.useAndroidX=true
    android.enableJetifier=true
    
  3. Rebuild your project.

that's it.

Note: If your project already shows this android.useAndroidX=true then Just add android.enableJetifier=true and rebuild your project.

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
UncagedMist
  • 111
  • 1
  • 4
6

For me, just installing the plugins "cordova-plugin-androidx" and "cordova-plugin-androidx-adapter" solved this problem:

$ ionic cordova plugin add cordova-plugin-androidx
$ ionic cordova plugin add cordova-plugin-androidx-adapter
Fernando Ghisi
  • 419
  • 4
  • 8
2

I think you kept same tag multiple time in gradle.properties like following

    org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    org.gradle.jvmargs=-Xmx4608m
Mohd Qasim
  • 896
  • 9
  • 20
1

I had this issue when I migrated to Androidx using the Android Studio feature but at the first time the migration was not successful so each time I tried to compile I ran into this issue.

To resolve this issue, I did the following:

[1] Comment all androidx dependencies in the app bundle.gradle file
[2] Try the Migrate to Androidx. You can see this link in Refactor -> Migrate to Androidx. If the migration was successful, then
[3] Uncomment all androidx dependencies in the app bundle.gradle file

You may clean and build your project again, hopefully this error should disappear.

CanCoder
  • 1,073
  • 14
  • 20
1

In my case, I removed this implementation under dependencies section in app level build.gradle file -

implementation 'com.google.android.gms:play-services-ads:19.2.0'

Remove or comment it out!!

Ash
  • 39
  • 2
1

Experienced the same problem. add this dependency to your gradle

implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'

Cynthia
  • 11
  • 2
1

Had this same issue. What worked for me was adding this to my android/app/build.gradle file:

configurations.all {
    resolutionStrategy {
        eachDependency {
            if ((requested.group == "org.jetbrains.kotlin") && (requested.name.startsWith("kotlin-stdlib"))) {
                useVersion("1.8.0")
        }
    }
}

Just switch the useVersion("X.X.X") with whatever you need

0

In my Ionic project I needed to remove the line

cordova.system.library.19=com.google.android.gms:play-services-analytics:11.0.1

from my /platforms/android/project.properties to get a build

Malcolm Swaine
  • 1,929
  • 24
  • 14
0

In my case, i was creating a separate module and importing firebaseAnalytics into that module but it turns out that adding dependency on main module from IDE help caused the issue of duplicate class found.

i removed the dependency and injected it directly, that solved the problem.

hope that helps.

0

I had the same problem but I solved it as below. implemetation code in build.gradle

def lifecycle_version = "2.4.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

Confirmed in this article

dogdduddy
  • 1
  • 1
0

maybe ./gradlew clean shold work . It work for me.

0
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'

    ***the solution***
----------
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
moken
  • 3,227
  • 8
  • 13
  • 23
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 10 '23 at 15:28
0
The below solution worked for me:
 1. Update build.Gradel (app) file with the below method inside the Android class.

Exception Code:   Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules **classes**.jar (**androidx.core**:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

 configurations{
        all*.exclude group:'androidx.core', module:'classes'
 }
2. Sync project
3. Clean project
4. Build a project 
vinod wakade
  • 139
  • 1
  • 1
  • 8
-1

In my case, I have added modules in the dependencies as follows:

modules {
    module("org.jetbrains.kotlin:kotlin-stdlib-jdk7") {
        replacedBy("org.jetbrains.kotlin:kotlin-stdlib", "kotlin-stdlib-jdk7 is now part of kotlin-stdlib")
    }
    module("org.jetbrains.kotlin:kotlin-stdlib-jdk8") {
        replacedBy("org.jetbrains.kotlin:kotlin-stdlib", "kotlin-stdlib-jdk8 is now part of kotlin-stdlib")
    }
}

Reference

user16217248
  • 3,119
  • 19
  • 19
  • 37
-2

I had the same problem but i solved it as below. Open Android studio> File > Invalidate Caches/Restart…

  • 1
    You could explain how/why this solution fixed OP's problem. I am a reviewer. StackOverflow requested a review due to you being a relatively new contributor. Your answer is appreciated & helpful. This comment is intended to help you make a good answer into an excellent one. – Andreas ZUERCHER Apr 28 '21 at 16:17