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

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:

  1. 执行升级命令:
    npm install react-native-device-info@latest
    
    或指定稳定兼容版本(如v10.8.0):
    npm install react-native-device-info@10.8.0
    
  2. 将android/build.gradle中的googlePlayServicesVersion设置为20.0.0
  3. 清理gradle缓存并重新构建:
    ./gradlew clean
    ./gradlew assembleDebug
    

方案二:强制统一Play Services版本并替换废弃依赖

若暂时无法升级react-native-device-info,可通过gradle的依赖解析规则强制统一版本并替换废弃库:

  1. 在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'
                    }
                }
            }
        }
    }
    
  2. 将根build.gradle中的googlePlayServicesVersion改为20.0.0
  3. 清理缓存后重新构建项目

方案三:修正exclude规则并补充替代依赖

之前的exclude未生效是因为未精准定位到废弃依赖,可按以下方式调整:

  1. 在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'
    
  2. 设置googlePlayServicesVersion为20.0.0
  3. 执行./gradlew clean后重新构建

内容的提问来源于stack exchange,提问作者Mansoor Ameen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 16:46:12