Skip to main content

Modularization

The Navigation 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 and visualize the route on the map.

By default, the Navigation SDK provides a hybrid router implementation that:

  1. Requests the route from the Mapbox Directions API if the connectivity is available.

  2. Generates the route locally on the device if the SDK had a chance to cache the routing resources. This is a fallback if there's no connectivity.

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

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:2.19.0") {
    exclude group: "com.mapbox.common", module: "logger"
    }
  2. Add the Mapbox Annotation Processor dependency:

    compileOnly("com.mapbox.base:annotations:0.8.0")
    kapt("com.mapbox.base:annotations-processor:0.8.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?