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

Jetpack Compose+ViewModel+LiveData的Gradle配置与重复类问题排查

解决Jetpack Compose项目Kotlin标准库重复类问题及依赖版本排查方法

问题背景

配置了结合ViewModel和LiveData的Jetpack Compose项目,构建时出现Kotlin标准库重复类错误,推测由依赖版本不兼容导致,需排查过期依赖并修复。

构建错误信息

Caused by: java.lang.RuntimeException: 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.6.21
(org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21)

项目Gradle配置(app模块)

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

android {
    namespace 'com.example.exercisecompose'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.exercisecompose"
        minSdk 21
        targetSdk 32
        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.1.1'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"

    implementation "androidx.compose.runtime:runtime-livedata:1.5.3"
}

MainActivity代码

class MainActivity : ComponentActivity() {
    private val viewmodel: TimerViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ExerciseComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    //Greeting("Android")
                    // Row 1 is a textview displaying time
                    // Row 2 consists of 3 buttons equally spaced
                    Row(
                        // Modifier.fillMaxSize() is usually passed into a Composable
                        modifier = Modifier.fillMaxSize()
                    ) {
                        Text(
                            viewmodel.timer.observeAsState().value?.time.toString()
                        )
                    }
                    Row(modifier = Modifier.fillMaxSize()) {
                        Button(
                            modifier = Modifier.padding(vertical = 24.dp),
                            onClick = {
                                viewmodel.startTimer()
                            }
                        ) {
                            Text("Start")
                        }

                        Button(
                            modifier = Modifier.padding(vertical = 24.dp),
                            onClick = {
                                viewmodel.pauseTimer()
                            }
                        ) {
                            Text("Pause")
                        }

                        Button(
                            modifier = Modifier.padding(vertical = 24.dp),
                            onClick = {
                                viewmodel.stopTimer()
                            }
                        ) {
                            Text("Stop")
                        }
                    }

                }
            }
        }
    }
}

解决步骤及依赖排查方法

一、修复当前重复类错误

该错误是因为项目同时引入了不同版本的Kotlin标准库:kotlin-stdlib:1.8.21(项目Kotlin插件默认依赖)和kotlin-stdlib-jdk8:1.6.21(来自runtime-livedata:1.5.3的传递依赖),版本冲突导致重复类。

  1. 统一Compose相关依赖版本
    将runtime-livedata的版本改为与compose_ui_version一致(1.1.1),替换原依赖行:

    implementation "androidx.compose.runtime:runtime-livedata:$compose_ui_version"
    
  2. 统一Kotlin版本(可选但推荐)
    在项目根目录的build.gradle中统一配置Kotlin版本,确保插件与依赖版本匹配:

    buildscript {
        ext {
            kotlin_version = "1.8.21"
            compose_ui_version = "1.1.1"
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
  3. 排除冲突的传递依赖(若仍报错)
    如果同步后还存在冲突,直接排除runtime-livedata中的低版本Kotlin依赖:

    implementation("androidx.compose.runtime:runtime-livedata:$compose_ui_version") {
        exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
    }
    

二、排查过期/冲突依赖的通用方法

  1. 使用Gradle依赖分析命令

    • 查看完整依赖树:执行终端命令 ./gradlew app:dependencies(Windows为gradlew.bat app:dependencies),搜索冲突的依赖包(如kotlin-stdlib-jdk8),找到它的引入来源
    • 精准定位依赖:执行 ./gradlew app:dependencyInsight --dependency kotlin-stdlib-jdk8 --configuration implementation,直接查看该依赖的所有版本和引入路径
  2. 利用Android Studio内置工具
    打开File > Project Structure > Dependencies,界面中会标注有更新可用的依赖,点击即可查看最新版本

  3. 维护统一版本管理
    在根build.gradle的ext块中定义所有核心依赖的版本号,避免分散配置导致版本不一致,例如:

    ext {
        kotlin_version = "1.8.21"
        compose_ui_version = "1.1.1"
        core_ktx_version = "1.10.1"
        lifecycle_version = "2.6.2"
        activity_compose_version = "1.7.2"
    }
    

    之后所有依赖都引用这些变量,便于统一升级。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 21:45:09