Navigation SDK for Android
Beta

Modularization

The Mapbox Navigation SDK gives flexible customization options to your project. The SDK's architecture follows a build-time modular design, which allows you to completely replace certain functionalities with custom implementations without increasing your project's binary-size.

The goal of modularization is to enable swapping SDK modules during compilation instead of exposing runtime configuration options and relying on third-party tools to remove unused code. The SDK streamlines independent units of code into modules where possible and uses interface contracts to connect the modules. Each module has a corresponding default implementation artifact that is shipped independently and referenced by the SDK. This means default implementations can be replaced with build system tools.

Architecture overview

The Navigation SDK's MapboxNavigation class depends on three interface contracts: Router, TripNotification, and Logger. The SDK composites the implementation of these contracts at runtime to receive or send the required information.

Router

The Navigation SDK's Router interface defines a contract to create a DirectionsRoute. This DirectionsRoute is eventually used to compute turn-by-turn instructions, visualize the route on the map, and enhance Location updates.

By default, the Navigation SDK has three router classes that extend the Router interface:

  1. Offboard Router: the default implementation of an online router that makes requests to the Mapbox Directions API. Offboard refers to the device having an internet connection.

    group: "com.mapbox.navigation", module: "router-offboard"
    
  2. Onboard Router: the default implementation of an offline router that generates an ideal route locally on the device. The route is created using the on-device routing tiles. This process can happen even when the device doesn't have an internet connection.

    group: "com.mapbox.navigation", module: "router-onboard"
    
  3. Hybrid Router: the main default implementation that wraps the Offboard and Onboard routers to offer the ideal routing experience in all cases.

    group: "com.mapbox.navigation", module: "router"
    

Each of the routers can be replaced independently. Replacing the Hybrid Router will automatically remove the Onboard and Offboard Routers references, which will require re-adding. This re-adding rule can be ignored if the custom Hybrid Router implementation doesn't depend on the other routers' default functionality.

TripNotification

TripNotification is the contract definition that fuels the Navigation SDK's notifications. These notifications are required by the Android-system foreground service that's created whenever a trip session is started. While the default Mapbox-provided implementation does allow for a certain amount of customization, completely replacing the implementation makes it possible to provide a completely custom Notification layout with personalized action buttons.

group: "com.mapbox.navigation", module: "notification"

Logger

By default, the Mapbox-provided implementation of the logger uses the android.util.Log class to log any info, warning, or error level log messages. Replacing the logger implementation allows for sending or processing this information by any third-party platform to provide more value and actionable debug data.

group: "com.mapbox.common", module: "logger"

Replacing a module

The Mapbox Navigation SDK comes bundled with runtime dependencies for all the default module implementations. You don't have to take any action to receive the SDK's default features. If your application requires additional levels of customization, you can remove any module listed from the build and replace it with a custom implementation in three steps.

For example, here's how to use Gradle to replace the logger module:

  1. Exclude the module's default implementation from the Mapbox Navigation SDK artifact's dependencies by referencing the group and artifact IDs:

    implementation("com.mapbox.navigation:core:1.0.0") {
        exclude group: "com.mapbox.common", module: "logger"
    }
    
  2. Add the Mapbox Annotation Processor dependency:

    compileOnly("com.mapbox.base:android-annotations:0.1.0")
    kapt("com.mapbox.base:android-annotations-processsor:0.1.0")
    
  3. Provide the custom logger implementation by creating and annotating a class that implements the contract:

    @MapboxModule(MapboxModuleType.CommonLogger)
    @Keep
    object MyLogger : Logger {
    ...
    }
    

Now, compiling the project will generate all the required classes and doesn't require any additional actions.

For more information and advanced usage examples, including testing and dependency injection, read the @MapboxModule annotation's documentation and visit the Mapbox Base Android repository.

Was this page helpful?