Expo Location模块getCurrentPositionAsync调用异常求助
错误信息
[Error: Encountered an exception while calling native method: Exception occurred while executing exported method getCurrentPositionAsync on module ExpoLocation: Found interface com.google.android.gms.location.FusedLocationProviderClient, but class was expected (declaration of 'com.google.android.gms.location.FusedLocationProviderClient' appears in /data/app/~~3vyWg7s9RwJ99lUbw2dcmg==/com.thotsuya.medlaapp-xjcjrh20AevFazVYU2oXvg==/base.apk!classes6.dex)]
环境信息
- expo-location 版本:
~15.1.1
相关代码
useEffect(() => { if (isEnabled) { (async () => { try { setLoading(true) let {status} = await Location.requestForegroundPermissionsAsync(); if (status !== 'granted') { setErrorMsg('Permission to access location was denied'); return; } let loc = await Location.getCurrentPositionAsync({}); setLocation(loc); await setCurrentProfileStatus(loc?.coords.latitude, loc?.coords.longitude, ProfileStatus.Online) await fetchNearbyPeople(loc?.coords.latitude, loc?.coords.longitude) mapRef.current.animateCamera({ center: { latitude: loc?.coords.latitude, longitude: loc?.coords.longitude, }, }) setLoading(false) } catch (e) { console.log(e) } finally { setLoading(false) } })(); } else { //Update the user's status to offline (async () => { try { await setCurrentProfileStatus(location?.coords.latitude, location?.coords.longitude, ProfileStatus.Offline) } catch (e) { console.log(e) } })(); } }, [isEnabled]);
解决建议
清理构建缓存
运行以下命令清理Android构建缓存和Expo相关缓存:# Managed Workflow npx expo prebuild --clean # Bare Workflow npx expo run:android --clean也可以手动删除项目根目录下的
.android/build文件夹,以及node_modules/.cache目录。强制统一Google Play Services版本
这个错误通常是Google Play Services(GMS)依赖版本冲突导致的,在app.json中指定统一的GMS版本:"expo": { "android": { "googleServicesVersion": "18.0.0" } }指定版本后重新构建项目。
检查依赖冲突
运行npx expo doctor检测项目依赖问题,根据输出提示修复冲突的GMS相关依赖。如果是Bare Workflow,可通过npx gradlew android:dependencies查看依赖树,定位冲突的GMS版本并强制统一。调整expo-location版本
尝试降级或升级expo-location到兼容版本,解决依赖适配问题:# 降级到15.0.1 npm install expo-location@15.0.1 --save # 或升级到15.2.0 npm install expo-location@15.2.0 --save重新生成Native构建文件
对于Managed Workflow,运行npx expo prebuild重新生成Android/iOS原生文件;Bare Workflow则运行npx gradlew clean assembleDebug重新编译Android项目。
内容的提问来源于stack exchange,提问作者Jarliev Pérez

