> ## 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.

# Android SDK 4.x to 5.0 migration guide

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic explains the changes in the Android 5.0 release and how to migrate to that version.

**Version 5.0 includes breaking changes**. It removes the deprecated `LDUser`. To learn more, read [Understanding what was removed](#understanding-what-was-removed).

Version 5 also introduces the ability to have LaunchDarkly automatically provide data about the mobile environment where the application is running. This data makes it simpler to target your mobile customers based on application name or version, or on device characteristics including manufacturer, model, operating system, and so on.

When you configure the SDK, starting in version 5.0 you must indicate whether LaunchDarkly should or should not include this data.

This data is provided in two new context kinds. The data is added automatically to each evaluation context you provide.

Customers who are billed by MAU may be charged based on the `ld_device` context kind if it becomes the context kind with the highest volume of monthly activity in their account. This feature does not affect customers on other billing models. To learn more, read [Account usage metrics](/docs/home/account/metrics).

Before you migrate to version 5.0, we recommend updating to the latest 4.x version. If you update to the latest 4.x version, deprecation warnings appear in areas of your code that need to be changed for 5.0, for example, any use of `LDUser`. You can update these areas at your own pace while still using 4.x, rather than migrating everything simultaneously. To learn more about updating to the latest 4.x version, visit the [SDK's GitHub repository](https://github.com/launchdarkly/android-client-sdk/).

## Understanding changes to configuration options

Version 5.0 introduces a new, required configuration parameter. Use this to indicate whether LaunchDarkly should automatically provide data about the mobile environment where the application is running as part of each evaluation context. You must include this parameter when building your `LDConfig`.

Here's how:

<CodeGroup>
  ```java title="Android SDK v5.0 (Java)" lines wrap theme={null}
  LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
      .mobileKey("example-mobile-key")
      .build();
  ```

  ```kotlin title="Android SDK v5.0 (Kotlin)" lines wrap theme={null}
  val ldConfig: LDConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
      .mobileKey("example-mobile-key")
      .build()
  ```

  ```java title="Android SDK v4.x (Java)" lines wrap theme={null}
  LDConfig ldConfig = new LDConfig.Builder()
      .mobileKey("example-mobile-key")
      .build();
  ```

  ```kotlin title="Android SDK v4.x (Kotlin)" lines wrap theme={null}
  val ldConfig: LDConfig = LDConfig.Builder()
      .mobileKey("example-mobile-key")
      .build()
  ```
</CodeGroup>

To disable this option, pass in `AutoEnvAttributes.Disabled` instead.

To learn more, read [`LDConfig.Builder`](https://launchdarkly.github.io/android-client-sdk/com/launchdarkly/sdk/android/LDConfig.Builder.html).

## Understanding the automatically added environment attributes

If you use `AutoEnvAttributes.Enabled` in the SDK client configuration, the SDK automatically adds two additional contexts to each context that you evaluate. You can use attributes from these contexts in your targeting rules.

<Tip>
  **Working with multi-contexts**

  Because the SDK automatically adds contexts, every evaluation context is now a set of multiple contexts, which is called a multi-context. This means to you can create targeting rules in your feature flags based on data from multiple contexts at once. To learn more, read [Multi-contexts](/docs/home/flags/multi-contexts).
</Tip>

These automatically added contexts include the following attributes:

<table>
  <thead>
    <tr>
      <th>Context kind</th>
      <th>Context attribute</th>
      <th>Description</th>
      <th>Example</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td><code>ld\_application</code></td>
      <td><code>key</code></td>
      <td>Unique for this context kind. Automatically generated by the SDK.</td>
      <td>*randomly generated*</td>
    </tr>

    <tr>
      <td />

      <td><code>id</code></td>
      <td>Unique identifier of the application.</td>
      <td><code>com.launchdarkly.example</code></td>
    </tr>

    <tr>
      <td />

      <td><code>locale</code></td>
      <td>Locale of the device, in [IETF BCP 47 Language Tag](https://www.ietf.org/rfc/bcp/bcp47.txt) format.</td>
      <td><code>en-US</code></td>
    </tr>

    <tr>
      <td />

      <td><code>name</code></td>
      <td>Human-friendly name of the application.</td>
      <td><code>Example Mobile App</code></td>
    </tr>

    <tr>
      <td />

      <td><code>version</code></td>
      <td>Version of the application used for update comparison.</td>
      <td><code>5</code></td>
    </tr>

    <tr>
      <td />

      <td><code>versionName</code></td>
      <td>Human-friendly name of the version. May or may not be a semantic version.</td>
      <td><code>5</code></td>
    </tr>

    <tr>
      <td />

      <td><code>envAttributesVersion</code></td>
      <td>Version of the environment attributes schema being used. This may change in later versions of the SDK.</td>
      <td><code>1.0</code></td>
    </tr>

    <tr>
      <td><code>ld\_device</code></td>
      <td><code>key</code></td>
      <td>Unique for this context kind. Automatically generated by the SDK.</td>
      <td>*randomly generated*</td>
    </tr>

    <tr>
      <td />

      <td><code>manufacturer</code></td>
      <td>Manufacturer of the device.</td>
      <td><code>Google</code></td>
    </tr>

    <tr>
      <td />

      <td><code>model</code></td>
      <td>Model of the device.</td>
      <td><code>Pixel 6 Pro</code></td>
    </tr>

    <tr>
      <td />

      <td><code>os</code></td>
      <td>Operating system of the device. Includes properties for <code>family</code>, <code>name</code>, and <code>version</code>.</td>
      <td><ul><li><code>family</code>: <code>Android</code></li><li><code>version</code>: <code>13</code></li><li><code>name</code>: <code>Android33</code></li></ul></td>
    </tr>

    <tr>
      <td />

      <td><code>envAttributesVersion</code></td>
      <td>Version of the environment attributes schema being used. This may change in later versions of the SDK.</td>
      <td><code>1.0</code></td>
    </tr>
  </tbody>
</table>

If the SDK cannot determine the information for a particular attribute, it will not include that attribute. For example, if the SDK cannot determine the device's model, it will not include the `model` attribute.

### Automatic environment attributes and application metadata configuration

If you are already setting [application metadata](/docs/sdk/features/app-config) as part of your SDK configuration, you can still use this new [environment attributes](/docs/sdk/features/environment-attributes) feature. Any application metadata that you set will override the automatically collected environment attributes.

Specifically, `ld_application` context attributes are set based on the following priority order:

1. If you set values for application id, name, version, or version name using [application metadata configuration](/docs/sdk/features/app-config) in the SDK, then the `ld_application` context will reflect those values.
2. Otherwise, if you have set values for the application id, name, version, or version name within your mobile app, the `ld_application` context will reflect those values.
3. Otherwise, the `ld_application` context will use the LaunchDarkly SDK name and version.

## Understanding changes to application information

In previous versions, you could use the `LDConfig` builder's `.applicationInfo` method to set the application id and application version. Starting with version 5, you can also set the application name and version name.

Here's how:

<CodeGroup>
  ```java title="Android SDK v5.0 (Java)" lines wrap theme={null}
  LDConfig config = new LDConfig.Builder(AutoEnvAttributes.Enabled)
      .applicationInfo(
          Components.applicationInfo()
              .applicationId("authentication-service")
              .applicationName("Authentication-Service")
              .applicationVersion("1.0.0")
              .applicationVersionName("v1")
      )
      .build();
  ```

  ```kotlin title="Android SDK v5.0 (Kotlin)" lines wrap theme={null}
  val ldConfig: LDConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
      .applicationInfo(
          Components.applicationInfo()
              .applicationId("authentication-service")
              .applicationName("Authentication-Service")
              .applicationVersion("1.0.0")
              .applicationVersionName("v1")
      )
      .build()
  ```
</CodeGroup>

To learn more, read [Application metadata configuration](/docs/sdk/features/app-config#android).

## Understanding what was removed

**Version 5.0 removes the deprecated `LDUser`**. Version 4 of the Android SDK replaced users with contexts. Starting in version 5, the deprecated `LDUser` is removed.

Here's how to construct a basic context, as compared with constructing a user:

<CodeGroup>
  ```java title="Android SDK v3.x, user with key" lines wrap theme={null}
  LDUser user = new LDUser("example-user-key");
  ```

  ```java title="Android SDK v4+, context with key" lines wrap theme={null}
  LDContext context = LDContext.create("example-context-key");
  ```
</CodeGroup>

To learn more about replacing users with contexts, read the [Android SDK 3.x to 4.0 migration guide](/docs/sdk/client-side/android/migration-3-to-4) and [Best practices for upgrading users to contexts](/docs/guides/flags/upgrading-contexts).
