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

Flutter使用flutter_mapbox_navigation打包APK报错:找不到mapbox_access_token

解决flutter_mapbox_navigation打包APK时的资源缺失错误

问题现象

使用flutter_mapbox_navigation实现导航功能,Debug模式下Android和iOS运行正常,但打包Release APK时失败,报错信息如下:

FAILURE: Build failed with an exception。具体错误为执行':flutter_mapbox_navigation:verifyReleaseResources'任务失败,Android资源链接错误,提示找不到资源string/mapbox_access_token(即eopeter.flutter_mapbox_navigation:string/mapbox_access_token)。

环境信息

  • Flutter (Channel stable, 3.7.11, on macOS 13.1 22C65 darwin-arm64, locale en-GB)
  • Android toolchain - develop for Android devices (Android SDK version 33.0.1)
  • Xcode - develop for iOS and macOS (Xcode 14.3)
  • Chrome - develop for the web
  • Android Studio (version 2021.3)
  • IntelliJ IDEA Community Edition (version 2022.3.2)
  • Connected device (2 available)
  • HTTP Host Availability

当前android/build.gradle配置

buildscript {
    ext.kotlin_version = '1.8.0'
    repositories {
        google()
        mavenCentral()
        maven {
            url 'https://api.mapbox.com/downloads/v2/releases/maven'
            authentication {
                basic(BasicAuthentication)
            }
            credentials {
                username = "mapbox"
                password = project.properties['MY SecretKey'] ?: ""
            }
        }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.15'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.2'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url 'https://api.mapbox.com/downloads/v2/releases/maven'
            authentication {
                basic(BasicAuthentication)
            }
            credentials {
                username = "mapbox"
                password = project.properties['MY SecretKey'] ?: ""
            }
        }
        configurations.all {
            resolutionStrategy {
                force "com.google.android.gms:play-services-location:21.0.1"
            }
        }
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

解决方案

1. 确保Release模式能读取到mapbox_access_token

  • 检查android/app/src/main/res/values/strings.xml,确认是否包含:
    <string name="mapbox_access_token">你的Mapbox访问令牌</string>
    
    若仅在debug目录的strings.xml中有该配置,需复制到main目录或release目录下的对应文件中,保证Release构建时能获取到资源。

2. 通过Gradle传递令牌到资源文件

在android/app/build.gradle的android块中,为Release构建添加资源配置:

android {
    // 其他配置...
    buildTypes {
        release {
            // 其他Release配置...
            // 添加以下两行,将Gradle中的令牌注入到资源
            buildConfigField "String", "MAPBOX_ACCESS_TOKEN", project.properties['MAPBOX_ACCESS_TOKEN'] ?: ""
            resValue "string", "mapbox_access_token", project.properties['MAPBOX_ACCESS_TOKEN'] ?: ""
        }
    }
}

同时在项目根目录的gradle.properties中添加(注意不要提交到版本控制系统):

MAPBOX_ACCESS_TOKEN=你的Mapbox访问令牌

3. 验证插件配置与版本

  • 检查pubspec.yaml中flutter_mapbox_navigation的版本,尝试更新到适配Flutter 3.7.11的最新稳定版,避免版本兼容问题。
  • 确认AndroidManifest.xml中已添加Mapbox所需的权限(如位置权限),但核心问题仍聚焦于令牌资源的可访问性。

4. 清理构建缓存后重新打包

执行以下命令清理缓存并构建Release APK:

flutter clean
flutter build apk --release

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 10:20:29