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

Flutter集成MapBox逐向导航遇多类错误求助

Flutter集成MapBox逐向导航崩溃问题解决方案

问题概述

在Flutter应用中集成MapBox逐向导航时,执行导航功能出现java.lang.NoSuchMethodException、IllegalArgumentException(因Targeting S+未指定PendingIntent的FLAG_IMMUTABLE/FLAG_MUTABLE)等错误,最终导致应用崩溃。相关配置及报错日志如下:

报错日志

E/MapboxModuleProvider(30785): Module loading failed
E/MapboxModuleProvider(30785): java.lang.NoSuchMethodException: com.mapbox.navigation.trip.notification.internal.MapboxTripNotification.<init> []
E/MapboxModuleProvider(30785):  at java.lang.Class.getConstructor0(Class.java:2363)
...[省略重复报错内容]
E/libc++abi(30785): terminating with uncaught exception of type jni::PendingJavaException
F/libc    (30785): Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 30785 (tematica.coride), pid 30785 (tematica.coride)

pubspec.yaml配置

name: codrive_flutter
description: Codrive with flutter framework
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.5.0
environment:
  sdk: ">=2.16.0 <3.0.0"
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  flutter_mapbox_navigation: ^0.0.25
  shared_preferences: ^2.0.13
  flutter_native_splash: ^2.0.4
  location: ^4.4.0
  google_fonts: ^2.3.1
  flutter_dotenv: ^5.0.2
  dio: ^4.0.4
  mapbox_gl: ^0.15.0
  intl: ^0.17.0
  flutter_rating_bar: ^4.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^1.0.0
  flutter_launcher_icons: ^0.9.2
flutter_intl:
  enabled: true 

build.gradle配置

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 31

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "it.sistematica.coride"
        minSdkVersion 21
        targetSdkVersion 31
        multiDexEnabled true
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation 'com.android.support:multidex:1.0.3'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

configurations {
    all*.exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-android-telemetry'
}

解决建议

  • 升级MapBox相关依赖版本
    当前使用的flutter_mapbox_navigation: ^0.0.25版本过旧,与mapbox_gl: ^0.15.0存在依赖冲突风险。建议替换为兼容的新版本,例如:

    flutter_mapbox_navigation: ^0.1.7 # 或使用最新稳定版
    mapbox_gl: ^0.16.0 # 确保版本相互兼容
    

    执行flutter pub get完成依赖更新。

  • 修复Android 12+的PendingIntent问题
    由于targetSdkVersion设置为31(Android 12+),所有PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE。可在build.gradle的dependencies块中添加指定版本的MapBox导航UI依赖,确保其已适配该要求:

    implementation 'com.mapbox.navigation:ui:2.12.0'
    
  • 解决NoSuchMethodException错误
    该错误源于依赖版本不匹配导致的构造方法缺失,升级依赖后通常可解决。若问题仍存在:

    • 执行flutter clean清理项目缓存,再重新执行flutter pub get并构建项目。
    • 移除build.gradle中排除mapbox-android-telemetry的配置,部分MapBox导航版本依赖该模块:
      // 注释或删除此配置块
      // configurations {
      //     all*.exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-android-telemetry'
      // }
      
  • 适配Android 12+权限配置
    在AndroidManifest.xml中添加Android 12+所需的通知权限:

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 21:55:24