You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose添加依赖时出现Duplicate class错误求助

解决Jetpack Compose第三方库引发的Duplicate Class编译错误

核心排查与修复步骤

  • 精准定位冲突依赖
    执行Gradle命令生成完整依赖树,快速找到重复类的归属:

    ./gradlew app:dependencies
    

    若要更精准筛选,直接指定重复类所属的包/库:

    ./gradlew app:dependencyInsight --dependency <重复类对应的依赖组/模块>
    

    例如重复类是androidx.lifecycle.ViewModel,则用--dependency androidx.lifecycle:lifecycle-viewmodel

  • 移除第三方库中的冲突依赖
    找到冲突的传递依赖后,在模块的build.gradle中通过exclude语法剔除重复部分:

    implementation("第三方库完整坐标") {
        exclude(group = "冲突依赖的Group", module = "冲突依赖的Module")
    }
    
  • 强制统一依赖版本
    若多个依赖引入了同一库的不同版本,在项目根目录的build.gradle中强制锁定版本:

    configurations.all {
        resolutionStrategy {
            force "androidx.lifecycle:lifecycle-viewmodel:2.6.2" // 替换为项目使用的稳定版本
        }
    }
    

额外验证点

  • 检查间接依赖:部分第三方库会隐式引入lifecycle、coroutines等常用库,依赖树会清晰展示传递路径
  • 清理构建缓存:执行./gradlew clean后重新编译,避免缓存残留导致的异常

错误信息示例(供参考):

Duplicate class androidx.lifecycle.ViewModel found in modules lifecycle-viewmodel-2.4.0-runtime.jar (androidx.lifecycle:lifecycle-viewmodel:2.4.0) and lifecycle-viewmodel-2.6.2-runtime.jar (androidx.lifecycle:lifecycle-viewmodel:2.6.2)

模块build.gradle配置示例(供参考):

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.composeapp'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.composeapp"
        minSdk 24
        targetSdk 34
        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_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.5.3'
    }
    packaging {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.activity:activity-compose:1.8.2'
    implementation platform('androidx.compose:compose-bom:2023.10.01')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    // 引入的第三方库
    implementation 'com.example:thirdparty:1.0.0'

    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:2023.10.01')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'
}

内容的提问来源于stack exchange,提问作者Pyae Hein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 08:47:30