Skip to main content

Documentation Index

Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Overview

This topic explains how to support multiple environments in LaunchDarkly mobile SDKs.

Client-side SDKs

Some LaunchDarkly client-side mobile SDKs support having multiple LDClient instances tied to separate mobile keys. This allows the SDK to evaluate flags from multiple environments. The SDK can evaluate flags from different environments whether they are all in the same LaunchDarkly project or different LaunchDarkly projects. However, other LaunchDarkly client-side mobile SDKs, including Android, iOS, and React Native, do not support multiple LDClient instances. Instead, you can configure the one SDK instance to connect to multiple environments. This feature is available in the following client-side SDKs:

Android

All LDClient instances evaluate against the same LDContext. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.Here’s how:
      Map<String, String> otherKeys = new HashMap<String, String>();
      otherKeys.put("platform", "platform-mobile-key-456def");
      otherKeys.put("core", "core-mobile-key-789ghi");

      LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
          .mobileKey("example-mobile-key")
          .secondaryMobileKeys(otherKeys)
          .build();

      LDContext context = LDContext.builder("example-context-key")
          .set("email", "sandy@example.com")
          .build();

      LDClient.init(this.getApplication(), ldConfig, context);
To access the secondary mobile key instances, use the getForMobileKey method on LDClient. This method takes the identifier name assigned to your environment key in the secondaryMobileKeys map and returns the associated LDClient instance. Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.Here’s how:
      LDClient coreInstance = LDClient.getForMobileKey("core");
      // Variation determines whether or not a flag is enabled for a specific context
      coreInstance.boolVariation("core-flag", false);
      // allFlags produces a map of feature flag keys to their values
      coreInstance.allFlags();
      // track records actions end users take in your app
      coreInstance.track("example-metric-key", data);
As all the client instances use the same LDClient object, some calls affect all instances.These methods include:
      LDClient coreInstance = LDClient.getForMobileKey("core");

      // Calls affect all LDClient Instances
      coreInstance.identify(/*Context Object*/);
      coreInstance.flush();
      coreInstance.setOffline();
      coreInstance.setOnline();
      coreInstance.close();
To learn more, read LDClient.

iOS

All LDClient instances evaluate against the same LDContext. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.
      let context = try LDContextBuilder(key: "example-context-key").build().get()
      var config = LDConfig(mobileKey: "example-mobile-key", autoEnvAttributes: .enabled)
      // The SDK throws error strings if you add duplicate keys or put the primary key or name in secondaryMobileKeys.
      try! config.setSecondaryMobileKeys(["platform": "platform-example-mobile-key", "core": "core-example-mobile-key"])
      LDClient.start(config: config, context: context)
To access the secondary mobile key instances, use the LDClient.get static method on LDClient. This method takes the identifier name assigned to your environment key in the secondaryMobileKeys map and returns the associated LDClient instance. Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.Here’s how:
      let coreInstance = LDClient.get(environment: "core")
      // Variation determines whether or not a flag is enabled for a specific context
      let coreFlagValue = coreInstance?.boolVariation(forKey: "core-example-flag-key", defaultValue: false)
      // allFlags produces a map of feature flag keys to their values
      let allFlags: [String: LDValue]? = coreInstance?.allFlags
      // track records actions end users take in your app
      try coreInstance?.track(key: "track-example-event-key", data: data)
As all the client instances use the same LDClient object, some SDK functionality affects all instances.These methods include:
      coreInstance.identify(/*Context Object*/)
      coreInstance.flush()
      coreInstance.setOnline(/*true or false*/)
      coreInstance.close()
To learn more, read LDClient.

React Native

Starting with version 10 of the React Native SDK, if you need to support multiple environments in the same application, you can create multiple clients. We do not recommend using multiple environments within one application, because it quickly becomes complex and difficult to manage. If your setup requires it, here’s how:
      // not recommended: support multiple environments by creating multiple clients
      const client1 = new ReactNativeLDClient('example-mobile-key', AutoEnvAttributes.Enabled);
      const client2 = new ReactNativeLDClient('mobile-key-456def', AutoEnvAttributes.Enabled);
In previous versions of the React Native SDK, all LDClient instances evaluated against the same LDContext. The mobile keys for additional environments were specified, along with identifying names, in a map passed to your LDConfig object.