Android Studio Jetpack Compose项目出现androidx.appcompat重复类冲突
问题
使用Android Studio、Kotlin 1.7.10、Jetpack Compose开发,添加com.google.android.libraries.places:places:2.6.0依赖后,编译报错,提示大量androidx.appcompat相关类在appcompat-1.0.0和appcompat-resources-1.4.1模块中重复。
错误示例
Duplicate class androidx.appcompat.content.res.AppCompatResources found in modules appcompat-1.0.0-runtime (androidx.appcompat:appcompat:1.0.0) and appcompat-resources-1.4.1-runtime (androidx.appcompat:appcompat-resources:1.4.1)
Duplicate class androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat found in modules appcompat-1.0.0-runtime (androidx.appcompat:appcompat:1.0.0) and appcompat-resources-1.4.1-runtime (androidx.appcompat:appcompat-resources:1.4.1)
(其余重复类错误信息省略)
已尝试无效操作
尝试排除places依赖中的AppCompat模块,但语法错误导致未生效:
implementation ('com.google.android.libraries.places:places:2.6.0') {exclude(group: 'androidx.appcompat', module: 'appcompat:1.0.0) }
项目完整依赖
dependencies { implementation ('androidx.core:core-ktx:1.8.0') implementation("androidx.compose.ui:ui:1.2.1") implementation("androidx.compose.material:material:1.2.1") implementation "androidx.compose.ui:ui-tooling-preview:1.2.1" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.activity:activity-compose:1.5.1' implementation ('com.google.android.gms:play-services-location:20.0.0') implementation ('com.google.android.libraries.places:places:2.6.0') testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.2.1" debugImplementation "androidx.compose.ui:ui-tooling:1.2.1" debugImplementation "androidx.compose.ui:ui-test-manifest:1.2.1" implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' implementation 'com.squareup.moshi:moshi-kotlin:1.13.0' implementation("io.coil-kt:coil-compose:2.1.0") implementation 'com.google.code.gson:gson:2.9.0' implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version" implementation "androidx.room:room-runtime:2.4.3" //2.4.3 implementation "androidx.room:room-ktx:2.4.3" //2.4.3 implementation "androidx.compose.runtime:runtime-livedata:1.2.1" // 1.2.1 annotationProcessor "androidx.room:room-compiler:2.4.3" // 2.4.2 -> 2.4.3 kapt "androidx.room:room-compiler:2.4.3" // 2.4.2 -> 2.4.3 implementation 'com.google.maps.android:maps-compose:2.5.3' //1.2.0 -> 2.5.3 implementation 'com.google.android.gms:play-services-maps:18.1.0' //18.0.2 -> 18.1.0 implementation("androidx.room:room-ktx:2.4.3") implementation "androidx.navigation:navigation-compose:2.5.1" androidTestImplementation "androidx.navigation:navigation-testing:2.5.1" implementation 'com.sealwu.jsontokotlin:library:3.7.4' }
解决方案
1. 修正依赖排除语法
你之前的exclude写法错误,模块名不能带版本号,正确写法如下:
implementation ('com.google.android.libraries.places:places:2.6.0') { exclude group: 'androidx.appcompat', module: 'appcompat' }
2. 强制统一AppCompat版本
如果排除依赖后仍有冲突,在项目根目录的build.gradle中添加版本强制规则,让所有依赖使用同一版本的AppCompat:
allprojects { configurations.all { resolutionStrategy { force 'androidx.appcompat:appcompat:1.4.1' force 'androidx.appcompat:appcompat-resources:1.4.1' } } }
或者直接在模块级build.gradle的dependencies中声明高版本AppCompat,Gradle会自动优先使用该版本:
implementation 'androidx.appcompat:appcompat:1.4.1'
3. 排查传递依赖
执行以下命令生成完整依赖树,确认哪些依赖引入了旧版本AppCompat,再针对性排除:
./gradlew app:dependencies
验证
修改后,执行Clean Project -> Rebuild Project,检查编译是否通过。
内容的提问来源于stack exchange,提问作者prtyshp

