Skip to main content

Get Started with the Navigation SDK for Android UX Framework

This guide describes the steps to install the latest version of the Mapbox Navigation SDK for Android UX Framework, configure your Android app to use the framework, and initialize the navigation UX in an Android View.

Evaluation Terms
By downloading UX Framework with MapGPT you agree that you're downloading beta software licensed only on the following terms: https://www.mapbox.com/legal/nav-sdk-eval-terms.

Prerequisites

  • A Mapbox account: Sign up or log into a free account on Mapbox.
  • Android Studio: This guide includes details specific to the latest version of Android Studio.
  • Gradle: Make sure you have Gradle installed.

Part 1: Configure credentials

Step 1: Create a secret token

A secret access token is required to download the SDK dependencies in your Android project. This token is used by gradle to authenticate with the Mapbox maven server where the SDK packages are hosted.

To create a secret token, follow these steps:

  1. Go to your account's tokens page.
  2. Click the Create a token button.
  3. Name your token, for this example we've used InstallTokenAndroid.
  4. Scroll down to the Secret Scope section and check the Downloads:Read scope box.
  5. Click the Create token button at the bottom of the page to create your token.
  6. Enter your password to confirm the creation of your token.
  7. Now, you'll be returned to your account's tokens page, where you can copy your created token. Note, this token is a secret token, which means you will only have one opportunity to copy it, so save this token somewhere secure.
Protect secret access tokens

You should not expose secret access tokens in publicly-accessible source code where unauthorized users might find them. Instead, you should store them somewhere safe on your computer and take advantage of Gradle properties to make sure they're only added when your app is compiled.

Step 2: Configure your secret token

Next, add your secret token to your global gradle.properties file. The global gradle.properties file is located in your Gradle user home folder.

If you don't have a gradle.properties file, create one. Add your secret token to the gradle.properties file as shown below, replacing the placeholder YOUR_SECRET_MAPBOX_ACCESS_TOKEN with your secret token.

~/.gradle/gradle.properties
MAPBOX_DOWNLOADS_TOKEN=YOUR_SECRET_MAPBOX_ACCESS_TOKEN

Step 3: Configure your public token

Your app must have a public access token configured to associate its usage of Mapbox resources with your account.

Follow these steps to add a public access token from your Mapbox account as an Android string resource.

  1. Open your project folder or create a new project in Android Studio.
  • If creating a new project, we recommend using the Empty Activity project type.
  1. Locate the resource folder:
  • In the project explorer, open your resource folder located at app/res/values.
  1. Create a new resource file:
  • Left click on the resource folder
  • Select New > Values Resource File
  • Name the file mapbox_access_token.xml
  • Click the Ok button.
  1. In the new file, copy and paste the code snippet below.
  • Make sure you are signed in to docs.mapbox.com. This will insert your default public token into the code snippet (a long string that starts with pk.).
  • If you are not signed in, you will need to replace the placeholder YOUR_MAPBOX_ACCESS_TOKEN with a token from your account's tokens page.
app/res/values/mapbox_access_token.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="mapbox_access_token" translatable="false" tools:ignore="UnusedResources">YOUR_MAPBOX_ACCESS_TOKEN</string>
</resources>

Your public access token is now available for use in your Android project. You will access it via the string resource you created in your implementation code.

Advanced Topics: Best Practices, Rotating Tokens & Adding Tokens at Runtime
GUIDE
Access token best practices

Learn how to keep access tokens private in mobile apps.

Adding Tokens at Runtime

You can also implement tokens at runtime, but this requires you to have a separate server to store your tokens. This is helpful if you want to rotate your tokens or add additional security by storing your tokens outside of the APK, but is a much more complex method of implementation.

If you do choose to follow this method, we recommend calling MapboxOptions.accessToken = YOUR_PUBLIC_MAPBOX_ACCESS_TOKEN before inflating the MapView, otherwise the app will crash.

Rotating Tokens

For more information on access token rotation, consult the Access Tokens Information page.

Part 2: Add the dependency

Step 1: Add the Mapbox Maven repository

Mapbox provides the Maps SDK dependencies via a private Maven repository. To download Mapbox dependencies, you must add the Maven repository's URL to your project.

  1. In Android Studio, under Gradle Scripts, open the settings.gradle file.
  2. Add a new maven {...} definition inside dependencyResolutionManagement.repositories.
settings.gradle
 
// Mapbox Maven repository
 
maven {
 
url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
 
authentication {
 
basic(BasicAuthentication)
 
}
 
credentials {
 
// Do not change the username below.
 
// This should always be `mapbox` (not your username).
 
username = "mapbox"
 
// Use the secret token you stored in gradle.properties as the password
 
password = providers.gradleProperty("MAPBOX_DOWNLOADS_TOKEN").get()
 
}
 
}

This configuration uses the secret access token you configured in Part 1 to authenticate with the Mapbox Maven repository.

While in beta, the Navigation SDK UX Framework requires additional maven configuration to download some snapshot dependencies. Add the following to your project-level settings.gradle file, after the main Mapbox Maven repository configuration:

settings.gradle.kts
...
maven {
url = uri("https://api.mapbox.com/downloads/v2/snapshots/maven")
credentials {
username = "mapbox"
password = providers.gradleProperty("MAPBOX_DOWNLOADS_TOKEN").get()
}
authentication {
create<BasicAuthentication>("basic")
}
}
...

Step 2: Add the UX Framework dependency

Next, add the Navigation SDK UX Framework module to your project.

  1. In Android Studio, open your module-level build.gradle file.

  2. Add the UX Framework module under dependencies.

build.gradle
dependencies {
...
implementation 'com.mapbox.navigationux:android:1.0.0-rc.2'
...
}

Part 3: Implement the UX Framework

Step 1: Initialize the Framework

You must initialize the UX Framework in your Application class. You can extend the Application class and override the onCreate method.

  1. Create a new file MyApplication.kt in the same directory as your other activities and add the following code:
MyApplication.kt
package com.example.navproject;

import android.app.Application;
import com.mapbox.dash.sdk.Dash


class MyApplication : Application() {

override fun onCreate() {
super.onCreate()
// Initialize the framework
Dash.init(
context = this,
accessToken = getString(R.string.mapbox_access_token)
)
}
}
  1. Update your AndroidManifest.xml file to use the MyApplication class as the application class:
AndroidManifest.xml
...
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...

Step 2: Add a navigation fragment

You must add a navigation fragment provided by the UX Framework to your application to tell it where to display the navigation UI. You can do this by adding a FragmentContainerView to your activity layout file, or by adding the fragment via the standard Android fragment manager.

In your main activity's xml layout file, add a FragmentContainerView with the android:name attribute set to com.mapbox.dash.sdk.DashNavigationFragment.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.mapbox.dash.sdk.DashNavigationFragment"
/>

Be sure that your main activity is referencing this layout file in its setContentView method:

MainActivity.kt
package com.example.navproject;

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Use the XML layout
}
}

Step 3: Run your app

Sync your project with Gradle and run your app on a device or emulator. You will see the navigation UI displayed in the FragmentContainerView or the activity layout.

Troubleshooting

Known Issues

When building an Android application, dependencies can bring along duplicate resource files or the same libraries. This can lead to conflicts when trying to compile the project.

Duplicate resource files in various libraries.

Many libraries include files like LICENSE, NOTICE, etc. If two or more libraries in your project contain the same files, it will cause a file duplication error when packaging the APK. To resolve this issue, you can exclude these files using the packagingOptions.

// build.gradle.kts
android {
packagingOptions {
resources {
excludes += setOf(
// To compile the current version of UX Framework you need to add only these three lines:
"META-INF/DEPENDENCIES",
"META-INF/INDEX.LIST",
"dash-sdk.properties",
)
}
}
}

Another dependency issue you may see related to the ListenableFuture library. That Guava's library might be included both directly and via other libraries. If this happens, you might experience build errors due to duplicate classes or incompatible versions. To address this issue, you can exclude conflicting libraries or versions using the configurations section.

// build.gradle.kts
configurations.all {
exclude(group = "com.google.guava", module = "listenablefuture")
}

Next Steps

With a minimal implementation working, you can now explore the Navigation SDK UX Framework's features and customize the navigation experience in your app.

Was this page helpful?