升级React Native至0.74.3后RNFusedLocation.getCurrentPosition调用失败
React Native 0.74.3升级后react-native-geolocation-service调用报错解决方法
问题背景
- React Native版本:从0.64.4升级至^0.74.3
- 定位依赖库:
react-native-geolocation-service@^5.3.1 - Google Play Services Location版本:21.3.0
- Gradle核心配置:
googlePlayServicesLocationVersion = "21.3.0" implementation 'com.google.android.gms:play-services-location:21.3.0' - 触发的错误信息:
Could not invoke RNFusedLocation.getCurrentPosition null Found interface com.google.android.gms.location.SettingsClient, but class was expected (declaration of 'com.google.android.gms.location.SettingsClient' appears in /data/app/...) - 已尝试无效操作:调整Google Play Services版本(含升级至21.3.0、降级至17.0.0)
核心原因
Google Play Services Location从20.0.0版本开始,将SettingsClient从类改为了接口,而你使用的react-native-geolocation-service@5.3.1版本仍按旧的类逻辑调用,导致版本兼容冲突。
解决方案
1. 升级定位库到兼容版本
该库的最新版本已适配Google Play Services的接口变更,执行升级命令:
npm install react-native-geolocation-service@latest # 或使用yarn yarn add react-native-geolocation-service@latest
2. 统一Gradle依赖版本
在项目根目录的android/build.gradle中,统一配置Google Play Services版本,避免依赖冲突:
buildscript { ext { googlePlayServicesLocationVersion = "21.3.0" } }
同时在android/app/build.gradle中引用该统一变量:
implementation "com.google.android.gms:play-services-location:$googlePlayServicesLocationVersion"
3. 清理缓存并重新构建
清除Gradle缓存,避免旧依赖残留:
cd android ./gradlew clean ./gradlew assembleDebug # 回到项目根目录重新运行 npx react-native run-android
4. 验证权限配置
确保AndroidManifest.xml中已添加必要定位权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 如需后台定位需添加 --> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
同时在代码中完成动态权限申请(RN 0.60+要求)。
补充:强制统一依赖版本
若升级后仍存在冲突,可在android/build.gradle的allprojects块中添加强制版本规则:
allprojects { configurations.all { resolutionStrategy { force "com.google.android.gms:play-services-location:$googlePlayServicesLocationVersion" } } }
内容的提问来源于stack exchange,提问作者rafik
相关产品推荐
相关产品推荐

