Get Started with the Navigation SDK for Android | Navigation SDK | Android Docs (original) (raw)

This guide describes the steps to install the latest version of the Mapbox Navigation SDK for Android, configure your Android app to use the SDK, and run a minimal example showing turn-by-turn navigation.

Prerequisites

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.

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.
  1. Locate the resource folder:
  1. Create a new resource file:
  1. In the new file, copy and paste the code snippet below.

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

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.

Step 4: Understand permissions

Real-time navigation requires access to the user's location, access to the network, and access to notify the user of updates. The SDK already includes all necessary permissions in its AndroidManifest.xml file, and these will be merged with any permissions you have in your app's manifest file via Manifest Merging.

You do not need to add any additional permissions to your app's manifest file, but you should make sure that your app requests the necessary permissions at runtime. The SDK provides a PermissionsManager class to help you manage permissions. You can check whether the user has granted location permission and request permissions if the user hasn't granted them yet using the PermissionsManager.

The permissions used by the SDK are listed below:

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.

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

Step 2: Add Nav SDK dependencies

Next, add the Nav SDK modules to your project to start using the Mapbox Navigation SDK for Android:

  1. In Android Studio, open your module-level build.gradle file.
  2. Add the Navigation SDK dependency under dependencies.
    • This will allow you to access base navigation services, like creating a route. Additional Navigation Services - Limiting dependencies in the SDK

There are other navigation modules you can add to your implementation, based on your app's requirements. See the list below:

Module Description
com.mapbox.navigationcore:copilot The Copilot is a component that collects detailed trace files of navigation sessions together with search analytics data.
com.mapbox.navigationcore:ui-maps The Maps component provides a set of classes that can enhance the navigation experience for your users, for example, Navigation Camera to simplify management of the map's camera object, or Route Line to render a route line on a map.
com.mapbox.navigationcore:navigation The Navigation module is the primary interface for engaging with the Navigation SDK.
com.mapbox.navigationcore:tripdata The Trip Data component provides convenient API to request maneuver instructions, route shields, speed limit, and trip progress data.
com.mapbox.navigationcore:ui-components The UI components module provides pre-built UI widgets.
com.mapbox.navigationcore:voice The Voice component allows users to access Voice API.
  1. Make sure that your project's minSdk is 21 or higher:

Part 3: Run a Navigation Example

To confirm that the Navigation SDK dependencies are correctly installed and your tokens are configured properly, create a turn-by-turn navigation with simulated user locations.

The code below displays a scaled-down navigation experience with a map, route line, and location puck.

  1. Add the sample code below to your MainActivty.kt file to run the simulation:
  2. Click File > Sync Project with Gradle Files
  3. Click the play button to build the app and run it on an emulator or device. After the map loads, you will see simulated navigation begin, with the camera following the user's location along the route.

You can experiment with the code to customize the navigation experience. For example, you can change the route's origin and destination points, or add custom logic to handle user location updates.

Troubleshooting

If your implementation is not working as expected, view the following troubleshooting solutions:

Black screen

If when you run simulator your screen displays black, here are some possible solutions:

Route does not appear

If your map appears but the route does not appear on the map, here are some possible solutions:

Maximum method count My application is crashing on start or won't build

General Tips

Make sure your always click Save All and Sync Project with Gradle Files when making changes to your application.

Next Steps

Congratulations! You've gotten your application up and running with the Mapbox Navigation SDK for Android.

What we've covered

Learn more

Explore other learning related to Mapbox Navigation SDK for Android:

Additional Developer Resources