Android Studio连接Firebase时Kotlin重复类错误的解决方法
解决Kotlin标准库重复类的Gradle构建错误
错误原因
你遇到的Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt错误,是因为项目中同时引入了不同版本的Kotlin标准库:kotlin-stdlib:1.8.10和kotlin-stdlib-jdk8:1.7.20,二者包含重复的类定义,导致构建冲突。
具体修复步骤
移除重复的插件引用
你的Gradle脚本末尾重复应用了com.android.application和com.google.gms.google-services插件,顶部的plugins块已经声明过,直接删掉末尾这两行:apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services'统一Kotlin标准库版本
在android块之后、dependencies块之前添加配置,强制所有依赖使用同一版本的Kotlin标准库:configurations.all { resolutionStrategy { force 'org.jetbrains.kotlin:kotlin-stdlib:1.8.10' force 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10' } }清理冗余Firebase依赖
你同时添加了firebase-analytics和firebase-analytics-ktx,后者是前者的Kotlin扩展版本,保留firebase-analytics-ktx即可,删掉重复的implementation 'com.google.firebase:firebase-analytics'。
修改后的完整Gradle代码
plugins { id 'com.android.application' id 'kotlin-android' id 'com.google.gms.google-services' } android { namespace 'com.example.dealBreaker' compileSdk 33 defaultConfig { applicationId "com.example.dealBreaker" minSdk 26 targetSdk 33 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 } kotlinOptions { jvmTarget = '1.8' } } // 强制统一Kotlin标准库版本,解决冲突 configurations.all { resolutionStrategy { force 'org.jetbrains.kotlin:kotlin-stdlib:1.8.10' force 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10' } } dependencies { implementation 'androidx.core:core-ktx:1.10.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.8.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation platform('com.google.firebase:firebase-bom:32.0.0') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'com.google.firebase:firebase-config' implementation 'com.google.firebase:firebase-crashlytics' }
内容的提问来源于stack exchange,提问作者skye
相关产品推荐
相关产品推荐

