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

Jetpack Compose项目kotlin-stdlib重复类错误求助

问题描述

使用Jetpack Compose和Kotlin开发项目时,添加新依赖后同步并运行应用出现以下错误:

执行任务':app:checkDebugDuplicateClasses'失败,提示在kotlin-stdlib-1.8.0(org.jetbrains.kotlin:kotlin-stdlib:1.8.0)和kotlin-stdlib-jdk8-1.7.10(org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10)模块中发现重复类kotlin.collections.jdk8.CollectionsJDK8Kt。

项目相关代码如下:

模块级build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'
    id 'com.google.gms.google-services'

}

android {
    namespace 'com.haristudio.pdi_app'
    compileSdk 33

    defaultConfig {
        applicationId "com.haristudio.pdi_app"
        minSdk 23
        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_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.2.0'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-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'
    implementation "com.google.accompanist:accompanist-systemuicontroller:0.27.0"

    // Jetpack Compose
    def compose_version = "1.2.0"
    def kotlin_coroutines_version = "1.6.4"
    def dagger_version = "2.44.2"
    def compose_latest = "1.3.3"

    implementation 'androidx.activity:activity-compose:1.6.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_latest"
    implementation 'androidx.compose.material:material:1.3.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_latest"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_latest"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_latest"
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation "androidx.navigation:navigation-compose:2.5.3"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
    implementation 'androidx.compose.material:material-icons-extended:1.3.1'
    implementation "androidx.constraintlayout:constraintlayout-compose:1.0.1"
    implementation "androidx.compose.runtime:runtime-livedata:1.3.3"
    implementation "androidx.compose.foundation:foundation:1.4.0-beta01"
    implementation "androidx.compose.runtime:runtime-rxjava2:1.4.0-beta01"
    //coil for images
    implementation "io.coil-kt:coil-compose:2.2.2"

    // exoPlayer
    implementation 'com.google.android.exoplayer:exoplayer:2.18.2'

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"

    // Dagger Hilt
    implementation "com.google.dagger:hilt-android:$dagger_version"
    kapt "com.google.dagger:hilt-compiler:$dagger_version"

    // Firebase
    implementation platform('com.google.firebase:firebase-bom:31.2.0')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation "com.google.firebase:firebase-auth:21.1.0"
    implementation 'com.google.firebase:firebase-database:20.1.0'
}

项目级build.gradle

buildscript {
    ext {
        compose_ui_version = '1.2.0'
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.42"

    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.4.0' apply false
    id 'com.android.library' version '7.4.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
    id 'com.google.dagger.hilt.android' version '2.42' apply false
    id 'com.google.gms.google-services' version '4.3.14' apply false

}

AuthViewModel

@HiltViewModel
class AuthViewModel @Inject constructor(
    private val repository : AuthRepository,
) : ViewModel() {
    private val _loginFlow = MutableStateFlow<Resource<FirebaseUser>?>(null)
    private val _signUpFlow = MutableStateFlow<Resource<FirebaseUser>?>(null)

    val loginFlow: StateFlow<Resource<FirebaseUser>?> = _loginFlow
    val signUpFlow: StateFlow<Resource<FirebaseUser>?> = _signUpFlow

    private val currentUser : FirebaseUser?
    get() = repository.currentUser

    init {
        if(currentUser != null){
            _loginFlow.value = Resource.Success(currentUser!!)
        }

    }

    fun login(email : String, password : String) = viewModelScope.launch {
        _loginFlow.value = Resource.Loading
       val result = repository.login(email,password)
        _loginFlow.value = result
    }
    fun signUp(
        username : String,
        email : String,
        password : String,
        numberPhone : String,
        image : String = "null",
        navController: NavController
    ) = viewModelScope.launch {
        _signUpFlow.value = Resource.Loading
        val result = repository.signUp(username,email,password,image,numberPhone)
        _signUpFlow.value = result
        if (result is Resource.Success) {
            navController.navigate("addNewTeam_screen")
        }
    }
    fun logout(){
        repository.logout()
        _loginFlow.value = null
        _signUpFlow.value = null
    }
}

AuthRepositoryImpl

class AuthRepositoryImpl @Inject constructor(
    private val firebaseAuth: FirebaseAuth
) : AuthRepository {
    override val currentUser: FirebaseUser?
        get() = firebaseAuth.currentUser

    override suspend fun login(
        email: String,
        password: String
    ): Resource<FirebaseUser>  {
        return try {
            val result = firebaseAuth.signInWithEmailAndPassword(email,password).await()
            Resource.Success(result.user!!)
        }catch (e : Exception){
            Resource.Failure(exception = e)
        }
    }

    override suspend fun signUp(
        username: String,
        email: String,
        password: String,
        image: String,
        numberPhone: String
    ): Resource<FirebaseUser> {
        return try {
            val result = firebaseAuth.createUserWithEmailAndPassword(email, password).await()
            result?.
            user?.
            updateProfile(
                UserProfileChangeRequest
                    .Builder()
                    .setDisplayName(username)
                    .build()
            )?.await()
            Resource.Success(result.user!!)
        }catch (e : Exception){
            Resource.Failure(exception = e)
        }
    }

    override fun logout() {
        firebaseAuth.signOut()
    }
}
解决方案

1. 统一Kotlin Gradle插件版本

项目当前使用Kotlin 1.7.0插件,但部分依赖(如Jetpack Compose 1.3.3)默认依赖Kotlin 1.8.0标准库,导致版本冲突。修改项目级build.gradle中的Kotlin插件版本为1.8.0:

buildscript {
    ext {
        compose_ui_version = '1.2.0'
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0" // 升级到1.8.0
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.42"
    }
}

plugins {
    id 'com.android.application' version '7.4.0' apply false
    id 'com.android.library' version '7.4.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false // 同步升级版本
    id 'com.google.dagger.hilt.android' version '2.42' apply false
    id 'com.google.gms.google-services' version '4.3.14' apply false
}

2. 统一Compose版本(推荐)

项目中Compose依赖混用了1.2.0和1.3.3版本,建议统一为同一稳定版,避免潜在冲突。修改模块级build.gradle中的版本变量:

// Jetpack Compose
def compose_version = "1.3.3" // 统一为1.3.3
def kotlin_coroutines_version = "1.6.4"
def dagger_version = "2.44.2"

随后替换所有引用$compose_version的依赖项,确保版本一致。

3. 强制统一标准库版本(兜底方案)

如果仍存在冲突,可在模块级build.gradle的dependencies块上方添加配置,强制所有依赖使用指定版本的Kotlin标准库:

configurations.all {
    resolutionStrategy {
        force "org.jetbrains.kotlin:kotlin-stdlib:1.8.0"
        force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0"
    }
}

4. 清理缓存并同步

  • 点击Android Studio右上角的Sync Project with Gradle Files重新同步项目
  • 选择File > Invalidate Caches... > Invalidate and Restart重启IDE,清理旧缓存
原因分析

冲突根源是项目配置的Kotlin插件版本(1.7.0)与部分依赖默认引入的Kotlin标准库版本(1.8.0)不一致,导致同一类文件在两个版本中重复存在。统一Kotlin版本后即可消除该冲突。

内容的提问来源于stack exchange,提问作者Muhammad yaumil Ramadhani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 21:15:35