Initialize the Consumer SDK

Before starting these procedures, make sure you have enabled the appropriate services and the Consumer SDK as described in earlier sections.

To initialize the Consumer SDK, follow these steps:

  1. Get the ConsumerAPI instance
  2. Initialize Maps SDK to request for preferred renderer

Get the ConsumerApi instance

To use the Consumer SDK, your app needs to initialize the singleton ConsumerApi asynchronously. The initialization method takes the AuthTokenFactory class to generate new JWT tokens for the user when necessary.

The providerId is the Project ID of your Google Cloud Project. For more information about creating a Fleet Engine project, see Create your Fleet Engine project in the Fleet Engine guide.

Your app should implement the AuthTokenFactory as described in Consumer SDK Authentication.

Java

Task<ConsumerApi> consumerApiTask = ConsumerApi.initialize(
    this, "myProviderId", authTokenFactory);

consumerApiTask.addOnSuccessListener(
  consumerApi -> this.consumerApi = consumerApi);

Kotlin

val consumerApiTask =
  ConsumerApi.initialize(this, "myProviderId", authTokenFactory)

consumerApiTask?.addOnSuccessListener { consumerApi: ConsumerApi ->
  this@YourActivity.consumerApi = consumerApi
}

Maps SDK and maps renderers

The Consumer SDK v2.0.0 and later support the Maps SDK for Android v18.1.0 and later. The following table summarizes the default renderer by Maps SDK version and the supportability of both renderers. If possible, use the latest renderer. If you must use the legacy renderer, explicitly specify it using MapsInitializer.initialize().

Maps SDK version Supports the latest renderer Supports the legacy renderer Default renderer
V18.1.0 and below Yes Yes Legacy*
V18.2.0 Yes Yes Latest

* With the rollout of new Maps Renderer, the Latest renderer will be the default.

If you must use a preferred renderer, run all UI-rendering operations after OnMapsSdkInitializedCallback returns a result. UI-rendering operations include the following operations:

  • Inflating a view that contains GoogleMapView or ConsumerMapView.

  • Placing markers on ConsumerMapView.

If you don't run these operations after receiving the OnMapsSdkInitializedCallback result, the Maps SDK doesn't allocate your preferred renderer and the map view is instead rendered by the default renderer.

Initialize Maps SDK before initializing the Consumer SDK

  1. In your Application or start-up Activity class, call MapsInitializer.initialize()

  2. Wait for the renderer request result before initializing the Consumer SDK.

See the following examples for details.

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  initViews();

  MapsInitializer.initialize(getApplicationContext(), Renderer.LATEST,
      new OnMapsSdkInitializedCallback() {
        @Override
        public void onMapsSdkInitialized(Renderer renderer) {
          switch (renderer) {
            case LATEST:
              Log.i("maps_renderer", "LATEST renderer");
              break;
            case LEGACY:
              Log.i("maps_renderer", "LEGACY renderer");
              break;
          }

          initializeConsumerSdk();
        }
      });
}

Kotlin

fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.main)
  initViews()

  MapsInitializer.initialize(
    getApplicationContext(), Renderer.LATEST,
    object : OnMapsSdkInitializedCallback() {
      fun onMapsSdkInitialized(renderer: Renderer?) {
        when (renderer) {
          LATEST -> Log.i("maps_renderer", "LATEST renderer")
          LEGACY -> Log.i("maps_renderer", "LEGACY renderer")
        }
        initializeConsumerSdk()
      }
    })
  }

Notes on SSL/TLS

Internally, the Consumer SDK implementation uses SSL/TLS to communicate securely with the Fleet Engine service. Android API versions 23 or earlier may require a SecurityProvider patch to communicate with the server. For more information about working with SSL in Android, see Security GMS Provider. The article also contains code samples for patching the security provider.

What's next

Set up a map