> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Multiple environments

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

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](#android)
* [iOS](#ios)
* [React Native](#react-native)

### Android

<Accordion title="Expand Android code sample">
  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:

  <CodeGroup>
    ```java title="Android SDK v5.x (Java)" lines wrap theme={null}
    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);
    ```

    ```kotlin title="Android SDK v5.x (Kotlin)" lines wrap theme={null}
    val otherKeys: MutableMap<String, String> = HashMap()
    otherKeys.put("platform", "platform-example-mobile-key")
    otherKeys.put("core", "core-example-mobile-key")

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

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

    LDClient.init(application, ldConfig, context)
    ```
  </CodeGroup>

  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:

  <CodeGroup>
    ```java title="Java" lines wrap theme={null}
    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();
    // trackData records actions end users take in your app
    coreInstance.trackData("example-metric-key", data);
    ```

    ```kotlin title="Kotlin" lines wrap theme={null}
    val coreInstance: LDClient = 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()
    // trackData records actions end users take in your app
    coreInstance.trackData("example-metric-key", data)
    ```
  </CodeGroup>

  As all the client instances use the same LDClient object, some calls affect all instances.

  These methods include:

  <CodeGroup>
    ```java title="Android SDK v4.0+ (Java)" lines wrap theme={null}
    LDClient coreInstance = LDClient.getForMobileKey("core");

    // Calls affect all LDClient Instances
    coreInstance.identify(context);
    coreInstance.flush();
    coreInstance.setOffline();
    coreInstance.setOnline();
    coreInstance.close();
    ```

    ```kotlin title="Android SDK v4.0+ (Kotlin)" lines wrap theme={null}
    val coreInstance: LDClient = LDClient.getForMobileKey("core")

    // Calls affect all LDClient Instances
    coreInstance.identify(context)
    coreInstance.flush()
    coreInstance.setOffline()
    coreInstance.setOnline()
    coreInstance.close()
    ```
  </CodeGroup>

  To learn more, read [`LDClient`](https://launchdarkly.github.io/android-client-sdk/com/launchdarkly/sdk/android/LDClient.html).
</Accordion>

### iOS

<Accordion title="Expand iOS code sample">
  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.

  <CodeGroup>
    ```swift title="iOS SDK v9.x (Swift)" lines wrap theme={null}
    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)
    ```

    ```objectivec title="iOS SDK v9.x (Objective-C)" lines wrap theme={null}
    LDContext *context = [[LDContextBuilder alloc] initWithKey:@"example-context-key"].build.success;
    LDConfig *config = [[LDConfig alloc] initWithMobileKey:@"example-mobile-key" autoEnvAttributes:AutoEnvAttributesEnabled];
    NSDictionary *otherMobileKeys = @{@"platform": @"platform-example-mobile-key", @"core": @"core-example-mobile-key"};
    NSError *err = nil;
    [config setSecondaryMobileKeys:otherMobileKeys error:&err];
    [LDClient startWithConfiguration:config context:context completion:nil];
    ```
  </CodeGroup>

  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:

  <CodeGroup>
    ```swift title="Swift" lines wrap theme={null}
    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)
    ```

    ```objectivec title="Objective-C" lines wrap theme={null}
    LDClient *coreInstance = [LDClient getWithEnvironment:@"core"];
    // Variation determines whether or not a flag is enabled for a specific context
    BOOL coreFlagValue = [coreInstance boolVariationForKey:@"core-example-flag-key" defaultValue:NO];
    // allFlags produces a map of feature flag keys to their values
    NSDictionary<NSString*, LDValue*> * allFlags = [coreInstance allFlags];
    // track records actions end users take in your app
    [coreInstance trackWithKey:@"MY_TRACK_EVENT_NAME" data:data];
    ```
  </CodeGroup>

  As all the client instances use the same `LDClient` object, some SDK functionality affects all instances.

  These methods include:

  <CodeGroup>
    ```swift title="Swift" lines wrap theme={null}
    coreInstance.identify(context: context)
    coreInstance.flush()
    coreInstance.setOnline(true)
    coreInstance.close()
    ```

    ```objectivec title="Objective-C" lines wrap theme={null}
    [coreInstance identifyWithContext:/*Context Object*/];
    [coreInstance flush];
    [coreInstance setOnline:/*true false*/];
    [coreInstance close];
    ```
  </CodeGroup>

  To learn more, read [`LDClient`](https://launchdarkly.github.io/ios-client-sdk/Classes/LDClient.html).
</Accordion>

### React Native

<Accordion title="Expand React Native code sample">
  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:

  <CodeGroup>
    ```ts title="React Native SDK, v10" lines wrap theme={null}
    // 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);
    ```
  </CodeGroup>

  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.
</Accordion>
