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

Flutter中Mapbox SDK与location包共存触发IncompatibleClassChangeError问题

解决Mapbox与Location包共存时的java.lang.IncompatibleClassChangeError错误

问题根源

该错误由Google Play Services定位库版本冲突导致:mapbox_gl和location包依赖了不同版本的com.google.android.gms:play-services-location,其中一个版本将FusedLocationProviderClient定义为接口,另一个定义为类,运行时出现类型不兼容。

解决方案

1. 强制统一Play Services定位库版本

在项目的android/build.gradle文件中添加版本强制配置,确保所有依赖使用同一版本的定位库:

allprojects {
    repositories {
        // 保留原有仓库配置
    }
    // 新增版本强制策略
    configurations.all {
        resolutionStrategy {
            force 'com.google.android.gms:play-services-location:21.0.1'
        }
    }
}

版本号可根据Firebase依赖的兼容版本调整,21.0.1为稳定兼容版本

2. 彻底清理并重建项目

执行以下命令清除缓存并重新构建:

flutter clean
flutter pub get
cd android && ./gradlew clean && cd ..
flutter run

3. 调整依赖版本(可选)

  • 尝试升级mapbox_gl至最新稳定版,在pubspec.yaml中更新版本号
  • 或降级location包至^6.0.0,该版本与mapbox_gl:^0.15.0兼容性经过验证

4. 替换定位实现(备选方案)

放弃单独的location包,直接使用Mapbox内置的定位功能:

MapboxMap(
  // 其他地图配置
  myLocationEnabled: true,
  myLocationTrackingMode: MyLocationTrackingMode.Tracking,
)

此方式可避免引入额外定位依赖,从根源减少冲突概率


内容的提问来源于stack exchange,提问作者Naqeeb Awan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 01:52:37