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

从GMS Places迁移到Places-AppCompat时的重复类问题求助

解决Places与Places-AppCompat的重复类依赖冲突问题

这个问题的根源很明确:你同时引入了places核心库和places-compat兼容库,而后者内部包含了和核心库重复的AutocompleteActivity及相关内部类,再加上你写的exclude规则格式错误,自然就触发了重复类编译错误。

给你两个可行的解决方案,按需选择:

方案1:仅保留Places-Compat兼容库(推荐)

如果你的项目是为了兼容旧版Places API的写法,完全不需要同时引入核心库,直接移除com.google.android.libraries.places:places:2.0.0即可,只保留兼容库:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
    implementation 'com.google.firebase:firebase-database:19.2.0'
    implementation 'com.google.firebase:firebase-auth:19.1.0'
    // 移除核心places库,仅保留兼容版本
    implementation 'com.google.android.libraries.places:places-compat:2.0.0'
    implementation 'com.firebase:geofire-android:3.0.0'
    implementation 'com.getkeepsafe.taptargetview:taptargetview:1.10.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
apply plugin: 'com.google.gms.google-services'

方案2:同时保留核心库和兼容库,正确排除重复类

如果你确实需要同时使用新版核心库的API和兼容层,那得修正你的exclude规则——你之前的写法格式错误,重复类实际上来自places-widget模块,所以要排除这个模块:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
    implementation 'com.google.firebase:firebase-database:19.2.0'
    implementation 'com.google.firebase:firebase-auth:19.1.0'
    implementation 'com.google.android.libraries.places:places:2.0.0'
    // 正确排除compat中与核心库重复的widget模块
    implementation ('com.google.android.libraries.places:places-compat:2.0.0'){
        exclude group: 'com.google.android.libraries.places', module: 'places-widget'
    }
    implementation 'com.firebase:geofire-android:3.0.0'
    implementation 'com.getkeepsafe.taptargetview:taptargetview:1.10.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
apply plugin: 'com.google.gms.google-services'

两种方案都能解决你遇到的重复类错误,优先推荐方案1,因为兼容库已经包含了你需要的旧API适配,没必要同时引入两个库增加依赖复杂度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:40:14