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

# Android SDK reference

> This topic documents how to get started with the Android SDK, and links to reference information on all of the supported features.

> For the complete documentation index, see [llms.txt](/llms.txt). For the full documentation snapshot, see [llms-full.txt](/llms-full.txt).

<Note title="Recent major versions">
  **Version 5 of the Android SDK introduces optional automatic collection of environment attributes**. To learn more about upgrading, read [Android SDK 4.x to 5.0 migration guide](/sdk/client-side/android/migration-4-to-5).
</Note>

## Overview

This topic documents how to get started with the Android SDK, and links to reference information on all of the supported features.

<Note title="SDK quick links">
  LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
</Note>

| Resource | Location |
| - | - |
| SDK API documentation | [SDK API docs](https://launchdarkly.github.io/android-client-sdk/) |
| Supported SDK Versions | [Android SDK](../concepts/supported-versions#android-sdk) |
| GitHub repository | [android-client-sdk](https://github.com/launchdarkly/android-client-sdk) |
| Sample application | [Android](https://github.com/launchdarkly/hello-android) |
| Published module | [Maven](https://mvnrepository.com/artifact/com.launchdarkly/launchdarkly-android-client-sdk) |

<Note title="SDK version compatibility">
  The LaunchDarkly Android SDK is compatible with Android SDK versions 21 and higher (Android 5.0, Lollipop).
</Note>

## Get started

After you complete the [Get started](/home/getting-started) process, follow these instructions to start using the LaunchDarkly SDK in your Android application:

* [Install the SDK](#install-the-sdk)
* [Import the SDK](#import-the-sdk)
* [Initialize the client](#initialize-the-client)
* [Evaluate a flag](#evaluate-a-flag)

### Install the SDK

First, declare a dependency on the LaunchDarkly Android SDK.

We recommend making the LaunchDarkly [observability plugin](/sdk/observability) available as well. This plugin collects and sends observability data to LaunchDarkly. This means you can review error monitoring and logs from within the LaunchDarkly UI. It requires the Android SDK version 5.9 or later.

Here's how:

<CodeGroup>
  ```java title="Gradle Groovy" theme={null}
      implementation 'com.launchdarkly:launchdarkly-android-client-sdk:5.+'

      // optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
      implementation 'com.launchdarkly:launchdarkly-observability-android:0.5.0'
  ```

  ```kotlin title="Gradle Kotlin" theme={null}
      implementation("com.launchdarkly:launchdarkly-android-client-sdk:5.+")

      // optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
      implementation("com.launchdarkly:launchdarkly-observability-android:0.5.0")
  ```
</CodeGroup>

The SDK uses [AndroidX from Jetpack](https://developer.android.com/jetpack/androidx). If your project does not use AndroidX, read [Android's migration guide](https://developer.android.com/jetpack/androidx/migrate).

<Note title="Using ProGuard or R8">
  If you use ProGuard or R8, the `aar` artifact should automatically include the configuration for the Android SDK. If this is not the case for your build, include the Proguard configuration lines from [consumer-proguard-rules.pro](https://github.com/launchdarkly/android-client-sdk/blob/master/launchdarkly-android-client-sdk/consumer-proguard-rules.pro) into your proguard file.
</Note>

### Import the SDK

Next, import the LaunchDarkly client in your application code:

<CodeGroup>
  ```java title="Java" theme={null}
      import com.launchdarkly.sdk.*;
      import com.launchdarkly.sdk.android.*;

      // optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
      import com.launchdarkly.observability.plugin.Observability
      import com.launchdarkly.sdk.android.integrations.Plugin
  ```

  ```kotlin title="Kotlin" theme={null}
      import com.launchdarkly.sdk.*
      import com.launchdarkly.sdk.android.*

      // optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
      import com.launchdarkly.observability.plugin.Observability
      import com.launchdarkly.sdk.android.integrations.Plugin
  ```
</CodeGroup>

### Initialize the client

After you install the SDK, create a single, shared instance of `LDClient`. To create a client instance, you need your environment's mobile key and the context for which you want to evaluate flags. This authorizes your application to connect to a particular environment within LaunchDarkly.

<Warning title="Android SDK credentials">
  The Android SDK uses a mobile key. Keys are specific to each project and environment. They are available from **Project settings**, on the **Environments** list. To learn more about key types, read [Keys](/sdk/concepts/client-side-server-side#keys).

  Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.
</Warning>

We recommend calling the client initialization method with a timeout of zero seconds. This allows you to use the client immediately. The app stores flags from the previous launch on the device and retrieves them for immediate use. The client connects in the background and continually updates itself with the latest flags.

Here's how to create the client:

<CodeGroup>
  ```java title="Android SDK v5.x (Java)" theme={null}
      LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
          .mobileKey("example-mobile-key")
          // optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
          .plugins(Components.plugins().setPlugins(
            Collections.singletonList<Plugin>(Observability(this.getApplication()))
          ))
          // other options
          .build();

      // You'll need this context later, but you can ignore it for now.
      LDContext context = LDContext.create("example-context-key");

      LDClient client = LDClient.init(this.getApplication(), ldConfig, context, 0);
  ```

  ```kotlin title="Android SDK v5.x (Kotlin)" theme={null}
      val ldConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
          .mobileKey("example-mobile-key")
          // optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
          .plugins(Components.plugins().setPlugins(
            listOf(Observability(this@BaseApplication))
          ))
          // other options
          .build()

      // You'll need this context later, but you can ignore it for now.
      val context = LDContext.create("example-context-key")

      val client: LDClient = LDClient.init(this@BaseApplication, ldConfig, context, 0)
  ```

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

      // You'll need this context later, but you can ignore it for now.
      LDContext context = LDContext.create("example-context-key");

      LDClient client = LDClient.init(this.getApplication(), ldConfig, context, 0);
  ```

  ```kotlin title="Android SDK v4.x (Kotlin)" theme={null}
      val ldConfig = LDConfig.Builder()
          .mobileKey("example-mobile-key")
          .build()

      // You'll need this context later, but you can ignore it for now.
      val context = LDContext.create("example-context-key")

      val client: LDClient = LDClient.init(this@BaseApplication, ldConfig, context, 0)
  ```
</CodeGroup>

If you need to, you can block for a short period of time until the SDK retrieves the latest feature flags from LaunchDarkly by using a non-zero value for the timeout parameter in the initialization call. However, calling blocking code from the main thread in an Android app is not a best practice, and we do not recommend it. Blocking until the SDK retrieves the latest feature flags from LaunchDarkly will cause your app never to load if there is a connectivity problem. If you must use a non-zero timeout parameter, we recommend setting it for five seconds or fewer.

To learn more about the specific configuration options available in this SDK, read [`LDConfig.Builder`](https://launchdarkly.github.io/android-client-sdk/com/launchdarkly/sdk/android/LDConfig.Builder.html).

<Warning title="LDClient must be a singleton">
  It's important to make `LDClient` a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

  If you have multiple LaunchDarkly projects, you should use the multiple environments feature. To learn more, read [Multiple environments](/sdk/features/multiple-environments#android).
</Warning>

### Evaluate a flag

After you create the `client`, you can use it to check which variation a particular context will receive for a feature flag.

Here's how:

<CodeGroup>
  ```java title="Java" theme={null}
      boolean showFeature = client.boolVariation(flagKey, true);
      if (showFeature) {
          // Application code to show the feature
      }
      else {
          // The code to run if the feature is off
      }
  ```

  ```kotlin title="Kotlin" theme={null}
      val showFeature: Boolean = client.boolVariation(flagKey, true)
      if (showFeature) {
          // Application code to show the feature
      }
      else {
          // The code to run if the feature is off
      }
  ```
</CodeGroup>

<Warning title="Making feature flags available to this SDK">
  You must make feature flags available to mobile SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the context will receive the fallback value for that flag.

  To make a flag available to this SDK, check the **SDKs using Mobile key** checkbox during flag creation, or toggle on the option in the flag's right sidebar. To make all of a project's flags available to this SDK by default, check the **SDKs using Mobile key** checkbox on your project's [Flag settings page](/home/account/edit-project).
</Warning>

## Shut down the client

Shut down the client when your application terminates. To learn more, read [Shutting down](/sdk/features/shutdown#android).

## Data collection

The data collected by the Android SDK persists until the number of cached contexts exceeds a limit. When you call `identify`, the number of cached contexts increments. Eventually, the number of cached contexts exceeds `maxCachedContexts`. When that happens, the SDK deletes context data in excess of `maxCachedContext`, starting with the oldest context first.

## Supported features

This SDK supports the following features:

* [Anonymous contexts and users](/sdk/features/anonymous#android)
* [Automatic environment attributes](/sdk/features/environment-attributes#android)
* [Configuration](/sdk/features/config#android), including
  * [Application metadata configuration](/sdk/features/app-config#android)
  * [Service endpoint configuration](/sdk/features/service-endpoint-configuration/client-side#android)
* [Context configuration](/sdk/features/context-config/client-side#android)
* [Evaluating flags](/sdk/features/evaluating#android)
* [Flag evaluation reasons](/sdk/features/evaluation-reasons/client-side#android)
* [Flushing events](/sdk/features/flush#android)
* [Getting all flags](/sdk/features/all-flags#android)
* [Hooks](/sdk/features/hooks#android)
* [Identifying and changing contexts](/sdk/features/identify#android)
* [Logging configuration](/sdk/features/logging#android)
* [Monitoring SDK status](/sdk/features/monitoring#android)
* [Multiple environments](/sdk/features/multiple-environments#android)
* [Observability](/sdk/observability/android)
* [Offline mode](/sdk/features/offline-mode#android)
* [Private attributes](/sdk/features/private-attributes/client-side#android)
* [Relay Proxy configuration, using proxy mode](/sdk/features/relay-proxy-configuration/proxy-mode#android)
* [Sending custom events](/sdk/features/events#android)
* [Shutting down](/sdk/features/shutdown#android)
* [Subscribing to flag changes](/sdk/features/flag-changes#android)
