我在运行我的Android项目时遇到了“重复类”错误。

huangapple go评论51阅读模式
英文:

I get a Duplicate Class error when running my Android project

问题

I am getting a Duplicate class error when running my Android project. It is giving the following error:

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

My build.gradle code is this:

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
    }
    
    apply plugin:'kotlin-parcelize'
    
    android {
        namespace 'com.example.quitanda'
        compileSdk 33
    
        defaultConfig {
            applicationId "com.example.quitanda"
            minSdk 21
            targetSdk 33
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            vectorDrawables {
                useSupportLibrary true
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_11
            targetCompatibility JavaVersion.VERSION_11
        }
        kotlinOptions {
            jvmTarget = '11'
        }
        buildFeatures {
            compose true
        }
        composeOptions {
            kotlinCompilerExtensionVersion '1.3.2'
        }
        packagingOptions {
            resources {
                excludes += '/META-INF/{AL2.0,LGPL2.1}'
            }
        }
    }
    
    dependencies {
    
        implementation 'androidx.core:core-ktx:1.8.0'
        implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
        implementation 'androidx.activity:activity-compose:1.5.1'
        implementation platform('androidx.compose:compose-bom:2022.10.00')
        implementation 'androidx.compose.ui:ui'
        implementation 'androidx.compose.ui:ui-graphics'
        implementation 'androidx.compose.ui:ui-tooling-preview'
        implementation 'androidx.compose.material3:material3'
        testImplementation 'junit:junit:4.13.2'
        androidTestImplementation 'androidx.test.ext:junit:1.1.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
        androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
        androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
        debugImplementation 'androidx.compose.ui:ui-tooling'
        debugImplementation 'androidx.compose.ui:ui-test-manifest'
    
        // Minha implementação
        implementation 'androidx.appcompat:appcompat:1.6.1'
        //2.6.0-alpha09
        implementation "androidx.navigation:navigation-compose:2.6.0-beta01"
        //def moshiVersion = "1.10.0"
        //implementation "com.squareup.moshi:moshi:$moshiVersion"
    
        implementation "com.squareup.retrofit2:retrofit:2.9.0"
        implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
        implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
        implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0-RC1'
        //1.0.0-alpha18
        implementation "androidx.paging:paging-compose:1.0.0-alpha19"
        //implementation "com.google.accompanist:accompanist-swiperefresh:0.14.0"
        implementation "com.google.accompanist:accompanist-pager:0.21.3-beta"
        implementation "io.coil-kt:coil-compose:2.2.2"
        //1.4.2
        implementation "androidx.compose.material:material-icons-extended:1.4.3"
        implementation "androidx.datastore:datastore:1.0.0"
        implementation "androidx.datastore:datastore-preferences:1.0.0"
        implementation 'com.google.android.gms:play-services-location:21.0.1'
        implementation 'com.google.maps.android:maps-compose:2.11.0'
        implementation 'com.google.android.gms:play-services-maps:18.1.0'
    }

I tried excluding the duplicate classes using the code below:

    apply plugin: 'org.jetbrains.kotlin'
    configurations {
        all {
            exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk8"

        }
    }

I added the above code, but then I got this error:

Plugin with id 'org.jetbrains.kotlin' not found

When I remove the apply it gives the error:

Duplicate class found
Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations$ReflectSdkVersion found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.DirectoryEntriesReader found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.ExperimentalPathApi found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org

<details>
<summary>英文:</summary>

I am getting a `Duplicate class` error when running my Android project. It is giving the following error: 

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


My build.gradle code is this:
plugins {
    id &#39;com.android.application&#39;
    id &#39;org.jetbrains.kotlin.android&#39;
}

apply plugin:&#39;kotlin-parcelize&#39;

android {
    namespace &#39;com.example.quitanda&#39;
    compileSdk 33

    defaultConfig {
        applicationId &quot;com.example.quitanda&quot;
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName &quot;1.0&quot;

        testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = &#39;11&#39;
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion &#39;1.3.2&#39;
    }
    packagingOptions {
        resources {
            excludes += &#39;/META-INF/{AL2.0,LGPL2.1}&#39;
        }
    }
}

dependencies {

    implementation &#39;androidx.core:core-ktx:1.8.0&#39;
    implementation &#39;androidx.lifecycle:lifecycle-runtime-ktx:2.3.1&#39;
    implementation &#39;androidx.activity:activity-compose:1.5.1&#39;
    implementation platform(&#39;androidx.compose:compose-bom:2022.10.00&#39;)
    implementation &#39;androidx.compose.ui:ui&#39;
    implementation &#39;androidx.compose.ui:ui-graphics&#39;
    implementation &#39;androidx.compose.ui:ui-tooling-preview&#39;
    implementation &#39;androidx.compose.material3:material3&#39;
    testImplementation &#39;junit:junit:4.13.2&#39;
    androidTestImplementation &#39;androidx.test.ext:junit:1.1.5&#39;
    androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.5.1&#39;
    androidTestImplementation platform(&#39;androidx.compose:compose-bom:2022.10.00&#39;)
    androidTestImplementation &#39;androidx.compose.ui:ui-test-junit4&#39;
    debugImplementation &#39;androidx.compose.ui:ui-tooling&#39;
    debugImplementation &#39;androidx.compose.ui:ui-test-manifest&#39;

    // Minha implementa&#231;&#227;o
    implementation &#39;androidx.appcompat:appcompat:1.6.1&#39;
    //2.6.0-alpha09
    implementation &quot;androidx.navigation:navigation-compose:2.6.0-beta01&quot;
    //def moshiVersion = &quot;1.10.0&quot;
    //implementation &quot;com.squareup.moshi:moshi:$moshiVersion&quot;

    implementation &quot;com.squareup.retrofit2:retrofit:2.9.0&quot;
    implementation &#39;com.squareup.retrofit2:converter-gson:2.9.0&#39;
    implementation &#39;com.squareup.retrofit2:converter-scalars:2.9.0&#39;
    implementation &#39;com.squareup.okhttp3:logging-interceptor:4.10.0-RC1&#39;
    //1.0.0-alpha18
    implementation &quot;androidx.paging:paging-compose:1.0.0-alpha19&quot;
    //implementation &quot;com.google.accompanist:accompanist-swiperefresh:0.14.0&quot;
    implementation &quot;com.google.accompanist:accompanist-pager:0.21.3-beta&quot;
    implementation &quot;io.coil-kt:coil-compose:2.2.2&quot;
    //1.4.2
    implementation &quot;androidx.compose.material:material-icons-extended:1.4.3&quot;
    implementation &quot;androidx.datastore:datastore:1.0.0&quot;
    implementation &quot;androidx.datastore:datastore-preferences:1.0.0&quot;
    implementation &#39;com.google.android.gms:play-services-location:21.0.1&#39;
    implementation &#39;com.google.maps.android:maps-compose:2.11.0&#39;
    implementation &#39;com.google.android.gms:play-services-maps:18.1.0&#39;
}

I tried excluding the duplicate classes using the code below:
apply plugin: &#39;org.jetbrains.kotlin&#39;
configurations {
    all {
        exclude group: &quot;org.jetbrains.kotlin&quot;, module: &quot;kotlin-stdlib-jdk8&quot;

    }
}

I added the above code, but then I got this error: 

Plugin with id 'org.jetbrains.kotlin' not found


When I remove the apply it gives the error: 
```Duplicate class found
Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations$ReflectSdkVersion found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.DirectoryEntriesReader found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.ExperimentalPathApi found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.FileVisitorBuilder found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.FileVisitorBuilderImpl found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.FileVisitorImpl found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.LinkFollowing found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathNode found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathRelativizer found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathTreeWalk found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathTreeWalk$bfsIterator$1 found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathTreeWalk$dfsIterator$1 found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathTreeWalkKt found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathWalkOption found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathsKt found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathsKt__PathReadWriteKt found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.io.path.PathsKt__PathUtilsKt found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)
Duplicate class kotlin.jdk7.AutoCloseableKt found in modules kotlin-stdlib-1.8.21 (org.jetbrains.kotlin:kotlin-stdlib:1.8.21) and kotlin-stdlib-jdk7-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20)

[SOLVED]

My solution was to add below command to build.gradle

dependencies {
     constraints {
         implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0&quot;) {
             because(&quot;kotlin-stdlib-jdk7 is now a part of kotlin-stdlib&quot;)
         }
         implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0&quot;) {
             because(&quot;kotlin-stdlib-jdk8 is now a part of kotlin-stdlib&quot;)
         }
     }
}

答案1

得分: 2

将以下内容添加到我的 build.gradle(模块:app)的末尾解决了此问题:

dependencies {
    constraints {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
            because("kotlin-stdlib-jdk7 现在是 kotlin-stdlib 的一部分")
        }
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
            because("kotlin-stdlib-jdk8 现在是 kotlin-stdlib 的一部分")
        }
    }
}
英文:

Adding this at the end to my build.graddle (Module:app) solved it:

dependencies {
 constraints {
     implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0&quot;) {
         because(&quot;kotlin-stdlib-jdk7 is now a part of kotlin-stdlib&quot;)
     }
     implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0&quot;) {
         because(&quot;kotlin-stdlib-jdk8 is now a part of kotlin-stdlib&quot;)
     }
 }

答案2

得分: 0

错误意味着在您添加的两个或多个依赖中使用了相同的类。Android Studio 不知道在运行应用程序时要使用哪个类。因此,会引发错误。在您的情况下,类 kotlin.collections.jdk8.CollectionsJDK8Kt 在两个不同的 Kotlin 版本中被使用。要解决这个问题:

apply plugin: 'com.package.domain'

android {
    ...
    buildTypes {
        ...
    }
    configurations {
        all { 
            exclude group: "com.package.domain", module: "YOUR_MODULE_NAME"
           
        }
    }
}

用冲突类的相应值替换 com.package.domainYOUR_MODULE_NAME

在您的情况下,group 将为 org.jetbrains.kotlin.android,module 将为 kotlin-stdlib-jdk8。这将排除其中一个 Kotlin 版本。

英文:

The error means that the same class used in two or more dependencies that you have added. Android Studio doesn’t know which class to use when running the app. So, the error is thrown. In your case, the class kotlin.collections.jdk8.CollectionsJDK8Kt is used in two different Kotlin versions. To solve it:

apply plugin: &#39;com.package.domain&#39;

android {
    ...
    buildTypes {
        ...
    }
    configurations {
        all { 
            exclude group: &quot;com.package.domain&quot;, module: &quot;YOUR_MODULE_NAME&quot;
           
        }
    }
}

Replace com.package.domain and YOUR_MODULE_NAME with the respective values of the conflicting classes.

In your case, the group would be org.jetbrains.kotlin.android and module would be kotlin-stdlib-jdk8. This will exclude one of the Kotlin versions.

huangapple
  • 本文由 发表于 2023年5月7日 10:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定