Getting started with the Google Navigation SDK

In this blog post, we are getting hands on with the Google Navigation SDK (commonly known as the Nav SDK) by building a simple Android navigation app using starter code from the official Navigation SDK Code Lab. We’ll go over how to set up the Nav SDK, make sure your app has the right permissions and libraries, and get it to provide in-app driving directions to a preset destination. Future tutorials will show you how to let users set their destination address using a Place Autocomplete text box, customize the navigation interface with your branding, and adjust the position and visibility of key UI elements for better control.

Part 1: Google Navigation SDK standalone
Part 2: Getting started with the Google Navigation SDK (this article)
Part 3: Adding a Place Autocomplete text box to your Android app
Part 4: Creating a custom route on Google Maps for Android
Part 5: Styling the Navigation SDK with custom UI and branding
Part 6: Custom Google Maps markers on the navigation view
Part 7: Customize your vehicle icon with the Navigation SDK
Part 8: Using the Navigation SDK for multi waypoint navigation

About this Google Navigation SDK tutorial

This tutorial is intended for developers with general software development experience but who are not experts in Android mobile development or the Kotlin programming language. There are two ways to get the most out of this tutorial. If you already have a mobile app and want to use the Navigation SDK as a drop-in replacement for your existing in-app navigation system, go ahead and clone the nav_sdk_tutorial (coming soon!) repository, add your Google Maps API key to the local.defaults.properties and secrets.properties files and run the app in Android Studio. To understand the nuts and bolts of the Nav SDK, follow along line by line. If you get stuck at any point you can always refer to the commit history (coming soon) of nav_sdk_tutorial for guidance.

Afi Labs builds custom transportation and logistics management software. From route optimization, live tracking to proof of delivery and driver apps, we've got you covered!
👋 Say Hello!

Initial commit

To get hold of our starter code, clone the Codelab repo with git clone https://github.com/googlemaps-samples/codelab-navigation-101-android-kotlin.git.

Navigate to the /Starter directory and copy + paste its contents into a new folder called nav_sdk-afi_labs.

We'll be using this code base as the starting point for our navigation app. Download Android Studio and open the /nav_sdk-afi_labs folder. Run the app by pressing the ▶️ button on the top nav bar. You should see an empty app running in the simulator. This means that your computer has everything it needs to run an Android app locally.

Setup SDK: Add dependencies

The initial part of this tutorial will involve working with Gradle, an open-source build automation tool primarily used for building, testing, and deploying software written in Java and Kotlin. Gradle uses a domain-specific language (DSL) based on Kotlin, so don't be surprised if it looks unfamiliar, especially if you are used to working in Javascript.

Open the app/build.gradle.kts file and add the line:

/*** app/build.gradle.kts ***/
api("com.google.android.libraries.navigation:navigation:5.2.3")

to the dependencies object. This line adds a dependency on the com.google.android.libraries.navigation:navigation library (version 5.2.3) and ensures it is downloaded and included in your project during the build process.

The line above instructs Gradle to retrieve the Google Navigation SDK from Maven and include it automatically during the build process.

Setup SDK: Config Navigation SDK API key

Next, we are going to securely authenticate with the Google Maps server using the Navigation SDK enabled API key we created in the previous section. We will use the Secrets Gradle Plugin, which serves a similar purpose to a .env file in Node.js development by providing developers with a secure way to store API keys and other credentials while ensuring these sensitive details are not checked into version control, reducing the risk of accidental exposure.

First, open app/build.gradle.kts and the line below to the plugins object.

/*** app/build.gradle.kts ***/
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.1" apply false

In the same app/build.gradle.kts, add a secrets object with the following code snippet. This defines a configuration block for the the Secrets Gradle Plugin.

/*** app/build.gradle.kts ***/
secrets {
    // Optionally specify a different file name containing your secrets.
    // The plugin defaults to "local.properties"
    propertiesFileName = "secrets.properties"

    // A properties file containing default secret values. This file can be
    // checked in version control.
    defaultPropertiesFileName = "local.defaults.properties"
}

The plugin reads secrets from the specified secrets.properties file during the build process. This file is typically not committed to version control to keep sensitive information secure. If a secret is not found in secrets.properties, the plugin will fall back to the local.defaults.properties file for a default value.

Next, create a new file, secrets.properties, and add your Google Maps API key (you don't have to add it to the local.defaults.properties file if you are checking this into version control).

/*** secrets.properties ***/
MAPS_API_KEY=YOUR_API_KEY

Finally, save all your files and sync your project with gradle.

Setup permissions: Declare permissions the app will be using

In app/src/main/AndroidManifest.xml, add the following code:

/*** app/src/main/AndroidManifest.xml ***/
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

In Android mobile development, the AndroidManifest.xml file is serves as a blueprint for our app. It provides essential information to the Android system about your app, including its structure, behavior, and requirements. Here, we are specifying the types of location-related access our Android app requires e.g. we are allowing the app to access location services while running in a foreground service.

This will allow the Nav SDK to track our real time location and suggest a new route if we deviate from the current one.