react-native-geolocation-service调用报错及版本冲突问题求助
解决React Native中Play Services版本冲突导致的构建失败与定位崩溃问题
问题根源
react-native-device-info的旧版本依赖已被Google废弃的play-services-iid库,该库在Play Services 17.0.0之后被移除并合并到play-services-identity中。而react-native-maps和react-native-geolocation-service需要更高版本的Play Services,导致版本冲突,引发构建失败或定位功能崩溃。
方案一:升级react-native-device-info到兼容版本
旧版react-native-device-info依赖废弃的play-services-iid,升级到v10.0.0及以上版本即可解决该依赖问题,同时兼容更高版本的Play Services:
- 执行升级命令:
或指定稳定兼容版本(如v10.8.0):npm install react-native-device-info@latestnpm install react-native-device-info@10.8.0 - 将
android/build.gradle中的googlePlayServicesVersion设置为20.0.0 - 清理gradle缓存并重新构建:
./gradlew clean ./gradlew assembleDebug
方案二:强制统一Play Services版本并替换废弃依赖
若暂时无法升级react-native-device-info,可通过gradle的依赖解析规则强制统一版本并替换废弃库:
- 在
android/build.gradle的allprojects块中添加以下配置:allprojects { repositories { // 保持原有仓库配置 } configurations.all { resolutionStrategy { // 强制所有Play Services核心库使用统一版本 force "com.google.android.gms:play-services-base:20.0.0" force "com.google.android.gms:play-services-location:20.0.0" // 将废弃的play-services-iid替换为play-services-identity eachDependency { details -> if (details.requested.group == 'com.google.android.gms' && details.requested.name == 'play-services-iid') { details.useTarget group: 'com.google.android.gms', name: 'play-services-identity', version: '18.0.0' } } } } } - 将根
build.gradle中的googlePlayServicesVersion改为20.0.0 - 清理缓存后重新构建项目
方案三:修正exclude规则并补充替代依赖
之前的exclude未生效是因为未精准定位到废弃依赖,可按以下方式调整:
- 在
android/app/build.gradle的dependencies块中修改react-native-device-info的依赖配置:implementation(project(':react-native-device-info')) { exclude group: 'com.google.android.gms', module: 'play-services-iid' } // 补充替代的identity库 implementation 'com.google.android.gms:play-services-identity:18.0.0' - 设置
googlePlayServicesVersion为20.0.0 - 执行
./gradlew clean后重新构建
内容的提问来源于stack exchange,提问作者Mansoor Ameen
相关产品推荐
相关产品推荐

