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

Flutter项目集成Mapbox双插件出现重复类构建失败求助

解决Flutter中mapbox_maps_flutter与flutter_mapbox_navigation的重复类冲突问题

问题根源

mapbox_maps_flutter:^2.1.0依赖的com.mapbox.common:common:24.5.0已经内置了com.mapbox.common.module.okhttp下的所有核心类;而flutter_mapbox_navigation:^0.2.2依赖了旧版本的独立com.mapbox.common:okhttp:23.8.0模块,两者的类完全重复,导致Android构建阶段的checkDebugDuplicateClasses任务失败。

有效解决方案

方案1:全局排除重复的okhttp模块

在build.gradle(app)的android节点下添加配置,全局移除冲突的独立okhttp模块:

android {
    // 保留原有compileSdk、defaultConfig等配置...

    configurations.all {
        exclude group: 'com.mapbox.common', module: 'okhttp'
    }
}

原理:由于com.mapbox.common:24.5.0已经包含了okhttp相关类,直接排除所有依赖中的独立okhttp模块即可避免重复。

方案2:精准排除指定插件的冲突依赖

如果不想全局排除,可以只针对flutter_mapbox_navigation插件的传递依赖进行排除,修改build.gradle(app)的dependencies块:

dependencies {
    implementation platform("org.jetbrains.kotlin:kotlin-bom:1.8.0")
    
    implementation project(':flutter_mapbox_navigation') {
        exclude group: 'com.mapbox.common', module: 'okhttp'
    }
}

方案3:强制统一Mapbox Common版本

通过Gradle的强制版本规则,让所有依赖都使用同一版本的com.mapbox.common,避免版本差异导致的冲突:

dependencies {
    implementation platform("org.jetbrains.kotlin:kotlin-bom:1.8.0")
    
    implementation('com.mapbox.common:common:24.5.0') {
        force = true
    }
}

方案4:升级插件版本(推荐优先尝试)

flutter_mapbox_navigation:^0.2.2是较旧版本,尝试升级到最新兼容版本(如^0.11.0,需匹配你的Flutter SDK版本),新版本通常已修复依赖冲突,不再依赖独立的okhttp模块。

验证步骤

  1. 清理项目缓存与构建文件:
flutter clean
rm -rf android/.gradle android/build
flutter pub get
  1. 重新构建项目:
flutter build apk --debug

内容的提问来源于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.19 23:05:02