Flutter使用flutter_mapbox_navigation打包APK报错:找不到mapbox_access_token
问题现象
使用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
相关产品推荐
相关产品推荐

