327

Image Example

I kept on getting error that there is duplicate error in classes. This is what i have under org.jetbrains.kotlin folder .idea/libraries

it seems like the problem is because there is two different dependencies for the kotlin, from stdlib and stdlibjdk8 but I don't know how to remove either one from my module/project dependencies. Please help :(

Here's the full error code :

Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0)
Duplicate class kotlin.internal.jdk8.JDK8PlatformImplementations found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.io.path.ExperimentalPathApi found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0)
Duplicate class kotlin.io.path.PathRelativizer found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0)
Duplicate class kotlin.io.path.PathsKt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0)
Duplicate class kotlin.io.path.PathsKt__PathReadWriteKt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0)
Duplicate class kotlin.io.path.PathsKt__PathUtilsKt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0)
Duplicate class kotlin.jdk7.AutoCloseableKt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk7-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.0)
Duplicate class kotlin.jvm.jdk8.JvmRepeatableKt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.random.jdk8.PlatformThreadLocalRandom found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.streams.jdk8.StreamsKt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.streams.jdk8.StreamsKt$asSequence$$inlined$Sequence$1 found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.streams.jdk8.StreamsKt$asSequence$$inlined$Sequence$2 found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.streams.jdk8.StreamsKt$asSequence$$inlined$Sequence$3 found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.streams.jdk8.StreamsKt$asSequence$$inlined$Sequence$4 found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.text.jdk8.RegexExtensionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)
Duplicate class kotlin.time.jdk8.DurationConversionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.0 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0)

I've tried deleting the files from the org.jetbrains.kotlin but whenever i build the app in Android Studio, the files will be re-downloaded again.

this is my module app gradle code


plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.example.umfs"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        viewBinding true
    }

    namespace 'com.example.umfs'
}

dependencies {


    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.firebase:firebase-database:20.1.0'
    implementation 'androidx.preference:preference:1.2.0'
    implementation 'com.google.firebase:firebase-auth:21.1.0'
    implementation 'com.google.firebase:firebase-core:21.1.1'
    implementation 'androidx.navigation:navigation-fragment:2.5.3'
    implementation 'androidx.navigation:navigation-ui:2.5.3'
    implementation 'com.google.firebase:firebase-firestore:24.4.1'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation "androidx.cardview:cardview:1.0.0"
    implementation 'com.google.firebase:firebase-firestore:24.4.1'
    implementation 'com.google.firebase:firebase-storage:20.1.0'
    implementation 'com.google.firebase:firebase-database'
    implementation platform('com.google.firebase:firebase-bom:28.4.0')
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.makeramen:roundedimageview:2.3.0'
    implementation 'com.github.bumptech.glide:glide:4.14.2'
    implementation 'com.github.marlonlom:timeago:4.0.3'
    implementation "androidx.core:core-ktx:+"

}

apply plugin: 'com.google.gms.google-services'
Boken
  • 4,825
  • 10
  • 32
  • 42
midnighthowlers
  • 3,271
  • 2
  • 3
  • 4
  • 47
    I thought I was the only one who got this error while having had no use of Kotlin anywhere in my large project.. Then I came across this very-obvious-to-me-now answer and it solved it very simply for me... https://stackoverflow.com/a/75315276/4355877 – Mick Mar 06 '23 at 16:11
  • 2
    @Mick Thank you so much for the tip, a lifesaver for those shunning Kotlin. – Hong Mar 28 '23 at 15:58
  • @midnighthowlers Try this solution once: https://stackoverflow.com/a/75974885/16765223 – M DEV Apr 10 '23 at 06:59
  • This answer worked for me. It's on page 2, that's why I'm adding this as a comment here: https://stackoverflow.com/a/75550269/4833705 – Lance Samaria Jul 26 '23 at 10:37

53 Answers53

276

Simply. For me just change this gradle plugins from 1.7.10 to 1.8.0 Then sync. => invalidate caches => build

From:

plugins {
....
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}

To:

plugins {
....
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}
Jurpro
  • 2,785
  • 1
  • 1
  • 3
  • 66
    Note: After updating to '1.8.0', do not forget to update kotlinCompilerExtensionVersion as '1.4.0' . – canerkaseler Feb 04 '23 at 23:22
  • 14
    I get a compilation error: **This version (1.3.2) of the Compose Compiler requires Kotlin version 1.7.20 but you appear to be using Kotlin version 1.8.0 which is not known to be compatible. Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).** – IgorGanapolsky Feb 10 '23 at 12:15
  • Question as a noob: Where should I add the above? – Prasannjeet Singh Feb 12 '23 at 22:02
  • If you are using ksp, do not forget to update it too. https://github.com/google/ksp/releases. For kotlin version 1.8.0 ksp must be 1.8.0 – Enes Kayıklık Feb 18 '23 at 11:26
  • got this error:The 'kotlin-android-extensions' Gradle plugin is no longer supported. Please use this migration guide (https://goo.gle/kotlin-android-extensions-deprecation) to start working with View Binding (https://developer.android.com/topic/libraries/view-binding) and the 'kotlin-parcelize' plugin. – Reijo Korhonen Mar 06 '23 at 22:20
  • 3
    @PrasannjeetSingh It is `build.gradle` file – kangkyu Mar 25 '23 at 08:24
  • 5
    @IgorGanapolsky - You can update the kotlinCompilerExtensionVersion to 1.4.4 in app level build.gradle. You can find this in ComposeOptions {} block. Check the link below to find the correctly mapped version (https://developer.android.com/jetpack/androidx/releases/compose-kotlin) – Mazhar May 05 '23 at 15:18
  • This is useful but you also need edit kotlinCompilerExtensionVersion= "1.4.3". https://developer.android.com/jetpack/androidx/releases/compose-kotlin?hl=zh-cn – Ven Ren May 14 '23 at 08:43
  • I had the same issue. By default Android Studio creates a project with `kotlinCompilerExtensionVersion '1.3.2'` (**Compose Compiler Version**) and `id 'org.jetbrains.kotlin.android' version '1.7.20' apply false` (**Kotlin Version**). And the problem comes when you try to use/update library versions: For example `implementation 'androidx.core:core-ktx:1.8.0'` to 1.10.1 , result in that error(you'll have to look for the right version). **To avoid this use the latest version of Compose Compiler Version and Kotlin version** (https://developer.android.com/jetpack/androidx/releases/compose-kotlin) – Carlos Leonardo Camilo Vargas Jun 13 '23 at 02:11
  • I replaced: - apply plugin: 'com.android.application' with : plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' version '1.8.0' apply false } And it works. – Armando Peña Jun 29 '23 at 18:08
168

See this note in the official kotlin docs for an explanation of how alignment works with these dependencies and build flags. The below original answer is an alternative approach which doesn't take this into account.

This is because kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8 are older dependencies that should be replaced by kotlin-stdlib (As @MrPNG pointed out from the what's new docs for kotlin 1.8). However, other libraries may still declare a dependency on them, which needs to be replaced with a dependency on kotlin-stdlib.

You can override how the jdk7 and jdk8 versions are resolved like this (in your app/build.gradle):

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")
        }
    }
}

The constraints section above can be added in alongside your other dependencies. Choose the version (here 1.8.0) to match the one you actually want to be used, which should be the one that your non-jdkN-named version is in the duplicate class error (e.g. (org.jetbrains.kotlin:kotlin-stdlib:1.8.0))

If you have apply plugin: 'kotlin-android' in this file, then it doesn't need to separately also say to depend on the stdlib in dependencies, so remove any lines that look like this:

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

(thanks to this blog)

David Fraser
  • 6,475
  • 1
  • 40
  • 56
  • 3
    I have a project where the build failed for the same reason as above. And I am not using any kotlin code in my project. Is there a way to disable any and all kotlin dependencies so that I can't possibly get a "duplicate classes" for kotlin dependencies? – AJW Mar 09 '23 at 00:10
  • 1
    If you're not using any kotlin code, it sounds like a very different problem even if it has the same error message... probably worth posting as a separate question – David Fraser Mar 10 '23 at 14:22
  • 4
    @AJW You may think that you don't use any Kotlin code in your project, but the dependencies you use can use Kotlin code inside them (for example various AndroidX libraries are written in Kotlin). – Robyer Mar 17 '23 at 11:00
  • 1
    It works now. You can also find information in [youtrack.jetbrains.com](https://youtrack.jetbrains.com/issue/KT-54136/Duplicated-classes-cause-build-failure-if-a-dependency-to-kotlin-stdlib-specified-in-an-android-project). – Blue Ocean Mar 23 '23 at 10:22
  • With Android Studio Giraffe (2022.3.1 Beta 1), I get a warning in the IDE: `'implementation' is defined by 'dependencies' but used within 'constraints'`. Any idea why, and how to get rid of it? – David Scherfgen Apr 26 '23 at 08:39
  • This solution worked for me but I also tried out a much cleaner approach from the official kotlin docs. Here is the link: https://kotlinlang.org/docs/whatsnew18.html#usage-of-the-latest-kotlin-stdlib-version-in-transitive-dependencies. – fidel morris omolo May 01 '23 at 18:43
  • 1
    Thanks for the solution. Wish `gradle` can't be little smart to detect and fix this type of issues. – jdevp2 Jul 03 '23 at 00:55
  • 1
    it works - Thankyou. – Pavan Aug 02 '23 at 18:06
  • thanks for the solution,works like a charm now. this could be a answer – Nilesh Singh Dahiya Aug 21 '23 at 16:44
107

I have tried all the previous solutions but after adding the below code it was working.

dependencies {
   ... ... ...
   implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
   ... ... ...
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
59

I was getting same error after upgrading android annotation libray from ver 1.4.0 to 1.6.0,

implementation 'androidx.annotation:annotation:1.6.0'

So I downgraded to 1.5.0 and error gone

implementation 'androidx.annotation:annotation:1.5.0'

Hope this helps you too

Prashanth
  • 993
  • 8
  • 18
  • 9
    I'm not using kotlin in a project and this solved my problem. – joninx Mar 23 '23 at 11:46
  • Unfortunately Kotlin is now a dependency for many of the Android core libraries, so now you are using Kotlin indirectly whether you want it or not. Unfortunately, that means we get the build problems that come with it. – Dustin Jun 12 '23 at 03:00
39

Upgrade kotlin android plugin and kotlin compiler

plugins {
    id 'org.jetbrains.kotlin.android' version '1.8.10'
}
android {
    composeOptions {
        kotlinCompilerExtensionVersion = "1.4.3"
    }
}
Tran Chien
  • 614
  • 4
  • 10
  • 3
    This worked for me. Although I only needed to upgrade the kotlin android plugin. Thanks! – Frankdroid7 Apr 10 '23 at 20:11
  • 4
    Adding the plugin worked for me, although my project is only Java and I don't use Kotlin at all. This really looks to me like a flaw in Android Studio, if not completely a bug. – Gabriel Apr 11 '23 at 13:57
  • same here; adding "implementation (platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))" fixed the issue, EVEN if I have no kotlin code. – HX_unbanned Jun 19 '23 at 10:24
26

You can now update the lifecycle dependencies to the most up-to-date versions, and simply add the following line in your build.gradle

dependencies {

    (...)

    // Fix Duplicate class
    implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))

}
alea
  • 980
  • 1
  • 13
  • 18
Mada AFLAK
  • 261
  • 3
  • 3
  • 2
    Everything complied fine. Then I updated androidx fragments from 1.5.7 to 1.6 and about 100 kotlin duplicate class errors occurred. What worries me is that they did not vanish when I got back to 1.5.7 and deleted build and invalidate cache. Its like black magic - it messes you up and sticks with you even if you revert back to older version! Still this implementation fixes those duplicate classes thank you – FrankKrumnow Jun 15 '23 at 12:22
  • Can anyone please elaborate on why this works? I have no clue how it fixed my issue of duplicate classes. – Wahaj Mubeen Jul 15 '23 at 06:35
  • the only fix that worked for me is this, thanks! – Elle Bishop Jul 28 '23 at 13:13
  • 1
    @WahajMubeen apparently we are using mutliple dependencies in pubsec.yaml file, and they all need the kotlin versions to be aligned, but each uses a diff version, which causes this issue, the fix mentioned above , is a solution to fix the duplicate class error. It resolves the conflict by using the Kotlin Bill of Materials (BOM) to ensure that all Kotlin dependencies in your project use the same version. – Elle Bishop Jul 28 '23 at 13:16
23

I tried many ways to solve this same prolem but didnt work I tried to add this dependencies below in my "build.gradle(module:app)"..

implementation (platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))

and then synce now, it will resolve this error.

Humayun MHA
  • 363
  • 3
  • 6
20

I solved the problem by changing kotlin class path dependency in the root build.gradle file from

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20-RC"

to

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
Mohamed Medhat
  • 761
  • 11
  • 16
13

I was using org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10 before in our SDK and then I updated kotlin to v1.8.0 and that's when I started getting the above error in our Android Java front end application. Using org.jetbrains.kotlin:kotlin-bom resolved the issue with transitive dependencies and might be a cleaner approach since it doesn't leak Kotlin details into the Java application.

so I just replaced

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10"

with below which solved the problem for me.

// Align versions of all Kotlin components
implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.8.0"
patel dhruval
  • 1,002
  • 10
  • 12
9

If you are using Compose you need to update the Compose Compiler (https://developer.android.com/jetpack/androidx/releases/compose-compiler) to 1.4.2 and Kotlin to 1.8.10 at the same time.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
riggaroo
  • 2,584
  • 20
  • 34
8
configurations.all {
    resolutionStrategy {
        force 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10'
    }
}

adding this to the end of gradle file helps

6

I had the same error on my Kotlin project. When I add Ktor to my project and upgraded the library versions, and then I got the wired long error. This is how I fixed it.

I used compose version 1.4.0

ext {
    compose_ui_version = '1.4.0'
}

kotlin version 1.8.0, the latest one 1.8.10 not compatible with compose 1.4.0 So I used,

id 'org.jetbrains.kotlin.android' version '1.8.0' apply false

Then, adjust the composeOptions on app level build.gradle file, like

composeOptions {
    kotlinCompilerExtensionVersion '1.4.0'
}

These are the three changes that I have done and I got escaped from the wired error. Hope this will help someone.

Ashana.Jackol
  • 3,064
  • 28
  • 22
6
lifecycle-viewmodel-ktx-2.6.1

down

lifecycle-viewmodel-ktx-2.5.1

I managed to get it working by lowering the dependency values.

Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
6

I fixed it by making Compose Compiler and JetBrains Kotlin plugins compatible

First, make sure are you using org.jetbrains.kotlin.android plugin version above 1.8.0.

build.gradle(Project)

plugins {
  ...
  id 'org.jetbrains.kotlin.android' version '1.8.21' apply false
}

Then check compatible version of Compose Compiler from below link and update build.gradle(app):
https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility

android {
  ...
  composeOptions {
    kotlinCompilerExtensionVersion '1.4.7'
  }
}
5

In this problem solve when I add dependency in our bulid.gradle(app) file

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0"

it's works properly

Harsh kumar
  • 61
  • 1
  • 4
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/34728815) – Dhvani023 Jul 25 '23 at 12:25
4

According to the release notes of Kotlin 1.8.0 (https://kotlinlang.org/docs/whatsnew18.html#updated-jvm-compilation-target), you shouldn't specify kotlin-stdlib-jdk7 nor kotlin-stdlib-jdk8 as dependencies anymore and, instead, just use kotlin-stdlib.

Luiz Rodrigo
  • 525
  • 1
  • 3
  • 13
4

It is maybe some updated version dependency. In my new compose app, when i updated

implementation 'androidx.compose.material3:material3:*'(new version) from 1.0.0-alpha11

I will got this error. Try revert updated dependency.

Also I was getting this error when android studio add in my gradle modules ktx:+ dependency

Gregory
  • 49
  • 5
4

I ran into this issue today and I fixed it by upgrading both Kotlin std-lib for android and the std-lib itself. Also it's worth mentioning that if you're using compose, you have upgrade the kotlinCompilerExtensionVersion

Here is the final setup

  • gradle file at Module level
composeOptions {
    kotlinCompilerExtensionVersion '1.4.3'
}
  • root project gradle file
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
    id 'org.jetbrains.kotlin.jvm' version '1.8.10' apply false
}
AouledIssa
  • 2,528
  • 2
  • 22
  • 39
4

Workaround for earlier KGP versions now is:
*file app/build.gradle

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")
    }
}

}

4

Step 1 :- Add this

plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
    }

Step 2 :- Add this on the dependencies

dependencies {

.....

  constraints{
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.10'
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10'
    }
}

Note:- version '1.8.10', jdk7:1.8.10 and jdk7:1.8.10 are same...

Ashfaque
  • 179
  • 4
4

edit app/build.gradle file in flutter

dependencies {

    (...)

    // Fix Duplicate class
    implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))

}
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
3

I had the same errors in my project. What finally helped me was the following steps

1-I have updated the buildScript: as below

buildscript {
    ext {
        compose_ui_version = '1.3.3'
    }
}
then change pluggin 
From

plugins {
....
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false

}

To

plugins {
....
id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
}

lastly updated in build.grade(app)

Change in android scope {}

    android {
    ......
    composeOptions {
            kotlinCompilerExtensionVersion '1.2.0'
        }
    .......
    }

To the following and the errors were gone

 composeOptions {
        kotlinCompilerExtensionVersion = "1.4.3"
    }

Hope it helps someone else 
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Mawuli
  • 41
  • 4
3

kotlinCompilerExtensionVersion(compose) -> kotlin_version

1.4.4 -> 1.8.10

1.4.3 -> 1.8.10

1.4.2 -> 1.8.10

1.4.1 -> 1.8.0

1.4.0 -> 1.8.0

1.4.0-alpha02 -> 1.7.21

1.4.0-alpha01 -> 1.7.20

1.3.2 ->1.7.20

...

Other versions: https://developer.android.com/jetpack/androidx/releases/compose-kotlin

Fortran
  • 2,218
  • 2
  • 27
  • 33
3

I'm getting similar error. These settings worked for me.

Note: I'm using Android Studio Flamingo | 2022.2.1

build.gradle(:project) settings

Old use:

buildscript {
    ext.kotlin_version = "1.7.20"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
...

New use:

buildscript {
    ext.kotlin_version = "1.8.0"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
...

build.gradle(:app) settings

Old use:

...
compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
...

New use:

...
compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
...

Gradle Projects Settings

enter image description here

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
3

This happened to me when upgrading one of these

implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3'

To version 2.6.0. When rolling back to 2.5.3 the error went away.

MartijndeM
  • 146
  • 1
  • 9
3

Adding this code inside app level/build.gradle

dependencies{
 ....
    constraints{
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.10'
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10'
    }
}

and adding this code inside project level/build.gradle

dependencies {
    .....
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"
}

It fixed this error 100%. It worked for me.

3

If I update kotlin version to 1.8.0 then it forces me to use view binding

i.e. classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10'

then if you want to keep kotlin version as it was before then just add it in app level gradle-

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0"

it will solve duplicate class issues though it is a temporary solution. Here my AGP version is 7.4.1 and Gradle version is 7.5

Rasel
  • 5,488
  • 3
  • 30
  • 39
2

Update 2023

Recently, I had the same issue where most of the duplicate classes belonged to Kotlin. One of the errors was something like this:

Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and jetified-kotlin-stdlib-jdk8-1.6.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21)

I had this issue because I was using a buggy kotlin version. Upgrade your kotlin version to 1.8.0-Beta. Follow this:

in your android/build.gradle, first add this in the ext section:

kotlinVersion = "1.8.0-Beta"

Now, go down to the dependencies section and add this:

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

Finally, make sure your JAVA_HOME points to a JDK11.

Doing these fixed my problem. Hope it helps!

2

Downgrade these deps to

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.5.1'
2

As I checked all ways, if there is a alpha version like this :

 implementation 'androidx.appcompat:appcompat:1.7.0-alpha02'

change it to

implementation 'androidx.appcompat:appcompat:1.6.1'

then Add class path in build.gradle()

 dependencies {
     
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
...
    }
Mori
  • 2,653
  • 18
  • 24
2

it is because of Compose Compiler Version and Kotlin Version incompability. when I posting this answer, most updated compatible versions were:

Compose Compiler Version: 1.4.6 Kotlin Version: 1.8.20

you can always find most updated compatible versions in this page: https://developer.android.com/jetpack/androidx/releases/compose-kotlin

you can set this versions in app level gradle file like this:

composeOptions {
    kotlinCompilerExtensionVersion = "1.4.3"
}

and

plugins {
    id 'org.jetbrains.kotlin.android' version '1.8.10'
}
ycannot
  • 1,807
  • 1
  • 9
  • 20
2

I had the same exact issue after accepting Android Studio's suggestion to update certain dependencies.

I looked at all the solutions here but none applied in my case because... I am not using kotlin in my code anywhere (i.e. my development is all in pure Java).

That led me to finding the following line in build.gradle:

    implementation 'androidx.core:core-ktx:1.10.1'

I just deleted it and all built perfectly as before the updates.

I have no idea why my build.gradle had a reference to androidx.core:core-ktx:1.9.0 in the first place (before the upgrade).

Introspective
  • 554
  • 2
  • 5
  • 13
  • 1
    Thank you so much, I had same problem with pure java, but because I updated `androidx.core:core` to 1.10.1 while keeping `fresco*` at 2.6.0. I had to revert `androidx.core:core` to 1.9.0 because I cannot use newer `fresco*` 3.0.0. – ZaFaR97 Jul 11 '23 at 05:33
  • 1
    @ZaFaR97 You are very welcome. What baffles me is that at some point I thought `androidx.core:core` was needed but now I see that it is not needed. I must have gone through some transitional stage, because my project was so old, that it had to undergo a lot of changes. – Introspective Jul 11 '23 at 10:14
  • I didn't know if it is needed in my project, I leave it till I can test it. – ZaFaR97 Jul 12 '23 at 11:06
1

for compose, use stable-version of material3

androidx.compose.material3:material3:1.0.1

and than

id 'org.jetbrains.kotlin.android' version '1.7.0' apply false

1

I noted the issue specifically after upgrading to these 2 libraries:

  • Material3 version 1.1.0-alpha05

  • Runtime-livedata version 1.4.0-beta01

This is are full dependencies:

implementation 'androidx.compose.runtime:runtime-livedata:1.4.0-beta01'
implementation 'androidx.compose.material3:material3:1.1.0-alpha04'

This is the error I was getting with Jdk-18.0.2

Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.20

The way around it for the time being is to downgrade these 2 libraries until the bug is corrected or updates are made.

implementation 'androidx.compose.material3:material3:1.1.0-alpha04'
implementation 'androidx.compose.runtime:runtime-livedata:1.4.0-alpha01'

Mind you there may be other dependencies that are affected and if you have recently upgraded the dependencies, you may need to rollback the updates and eliminate the library one by one to identify the troublesome one.

Tonnie
  • 4,865
  • 3
  • 34
  • 50
1

The accepted answer worked for me, but the Kotlin DSL syntax is:

kotlin("android").version("1.8.10").apply(false)
leo
  • 113
  • 3
  • 11
1

I first tried David Fraser's solution (constraints {...}) and it worked fine, but I found it strange and discarded it. Instead I changed the plugins as follows:

id 'org.jetbrains.kotlin.multiplatform' version '1.8.0' apply false // before 1.7.10

This also fixed the error.

guenter47
  • 457
  • 5
  • 13
1

Such problem occurred after upgrading jetpack compose from 1.3.0 to 1.4.0.

If you find this problem, please refer to the Kotlin Compose compatibility map in order to find matching Kotlin/Compose Compiler versions.

When you have found them, you need to make sure that:

  • Gradle plugin has the right version (in buildSrc: build.gradle.kts for KTX gradle or in regular Gradle the root build.gradle one: implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:<KOTLIN_VERSION>"))
  • Kotlin dependency has right version (if you use multi-module app and use Kotlin directly (implementation("org.jetbrains.kotlin:kotlin-stdlib:<KOTLIN_VERSION>"))
  • kotlinCompilerExtensionVersion = <COMPOSE_VERSION> is matching
Michał Dobi Dobrzański
  • 1,449
  • 1
  • 20
  • 19
1

On my Mac. I hadn't worked on my app in a while 6-12 months maybe. I was just checking in to make sure that Android Studio and the dependencies weren't too out of date. I received the same error as noted in the question.

  1. When I tried to change my ext.kotlin_version = "1.7.20" to 1.8.x, I received a warning about classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" being inconsistent with my Kotlin version.

  2. When I went into Tools->Kotlin->Configure Kotlin Updates and noticed that I couldn't get the the recent 1.8.20 version.

Tool Menu Kotlin Config Window

  1. Then went to see if I needed an Android Studio->Check for updates... enter image description here

  2. I in fact did need a major release update arctic fox to flamingo. After upgrading to Flamingo, I was able to update both locations of Kotlin Version to 1.8.20. Tools->Kotlin->Configure Kotlin Updates and ext.kotlin_version = "1.8.20"

  3. Did File->Invalidate Cache and reboot

  4. Did Build->Clean Project

  5. I updated whatever else needed to be updated with Flamingo

  6. Built and ran -> and ... BUILD SUCCESSFUL in 1m 6s 38 actionable tasks: 38 executed

Jacksonsox
  • 1,114
  • 15
  • 25
1

(gradle.properties)

org.gradle.jvmargs=-Xmx4608m
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true

and app (build.gradle)

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")
        }
    }
1

In my case, only changing the implementation 'androidx.core:core-ktx:1.10.1' to implementation 'androidx.core:core-ktx:1.7.0' worked for me.

1

I had the same problem after updating my Android Studio, for this to work, I had to downgrade my dependencies like this

Previous use:

dependencies {

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.6.0'
}

Changing to:

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
}

It worked for me.

banan3'14
  • 3,810
  • 3
  • 24
  • 47
1

I solved this problem and added this line in the build.gradle:

implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
Ahmed Ramadan
  • 181
  • 1
  • 4
1

The issue may be easely fixed by one line only:

dependencies {
.....
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.0'
}
Anatolii Shuba
  • 4,614
  • 1
  • 16
  • 17
1

Add the below line in your build.gradle(Module:app)

dependencies {

(...)

// Fix Duplicate class
implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))

}
Arbaz Alam
  • 1,172
  • 1
  • 14
  • 24
0

I had same issue while it was working 2 days ago...

I changed to

composeOptions {
        kotlinCompilerExtensionVersion '1.3.2'
}

and id 'org.jetbrains.kotlin.android' version '1.7.20' apply false in build.gradle (Project)

Then clean and rebuild project.

0

I had the same error in my project but mine was caused by different jetpack compose libraries having different versions and I put the specific version number "1.3.3" which caused other libraries that have later version numbers to not be found. I fix this by using compose BOM which provides the latest version to each library

0

Replace this

implementation("androidx.navigation:navigation-compose:2.6.0-alpha06")

With

implementation("androidx.navigation:navigation-compose:2.6.0-alpha04")
  • 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 Mar 12 '23 at 04:39
0

Replace

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"

With

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10"

And then If you used kotlin-android-extensions plugin, then remove this line. And use View binding to whole project.

Ahamadullah Saikat
  • 4,437
  • 42
  • 39
0

Duplicate id detected is because there is a duplicate id you defined. For example, you defined the id in project level build.gradle file and if you define the same id in your module-level build.gradle file then it is simply a duplicate id problem.

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
0

If you are upgrading to 1.8.0 then you should add

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
Ranajit Sawant
  • 181
  • 1
  • 6
0

By @ArdaKazancı answer, I did a similar change: (agp:7.4.2, gradle: 7.5)

implementation 'androidx.recyclerview:recyclerview:1.3.1'
implementation 'androidx.core:core:1.10.0'

to

implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.core:core:1.9.0' 
C.F.G
  • 817
  • 8
  • 16
0

This duplication happens because some of the libraries still use module org.jetbrains.kotlin:kotlin-stdlib-jdk8 or org.jetbrains.kotlin:kotlin-stdlib-jdk7 instead of the new module org.jetbrains.kotlin:kotlin-stdlib, therefore the class duplication accure.

To fix this we need to replace this module by ourselves:

modules {
        module('org.jetbrains.kotlin:kotlin-stdlib-jdk8')
                { replacedBy 'org.jetbrains.kotlin:kotlin-stdlib', 'kotlin fixes' }
        module('org.jetbrains.kotlin:kotlin-stdlib-jdk7')
                { replacedBy 'org.jetbrains.kotlin:kotlin-stdlib', 'kotlin fixes' }
    }
Mohamed Ben Romdhane
  • 1,005
  • 3
  • 11
  • 22
0

Change all lines in build.gradle to

 implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.annotation:annotation:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

I'd say downgrade the dependencies. I just do it and it works very well.

  • 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 Jul 11 '23 at 10:20