No results for ""
  • Home
  • API docs


Android SDK reference

Read time: 6 minutes
Last edited: Apr 03, 2024
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.

Version 4 of the Android SDK replaces users with contexts. A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more about upgrading, read Android SDK 3.x to 4.0 migration guide and Best practices for upgrading users to contexts.

Code samples on this page are from the three most recent SDK versions where they differ.


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

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:

SDK API documentationSDK API docs
GitHub repositoryandroid-client-sdk
Sample applicationAndroid
Published moduleMaven
SDK version compatibility

The LaunchDarkly Android SDK is compatible with Android SDK versions 21 and higher (Android 5.0, Lollipop).

Get started

After you complete the Getting started process, follow these instructions to start using the LaunchDarkly SDK in your Android application.

Install the SDK

First, declare a dependency on the LaunchDarkly Android SDK:

implementation 'com.launchdarkly:launchdarkly-android-client-sdk:5.0.0'

The SDK uses AndroidX from Jetpack. If your project does not use AndroidX, read Android's migration guide.

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 into your proguard file.

Next, import the LaunchDarkly client in your application code:

import com.launchdarkly.sdk.*;
The Android SDK uses a mobile key

The Android SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.

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. This authorizes your application to connect to a particular environment within LaunchDarkly.

Never embed a server-side SDK key into a client-side application

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.

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.

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:

LDClient client = LDClient.init(this.getApplication(), ldConfig, context, 0);
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 on the flag's Settings tab. To make all of a project's flags available to this SDK by default, check the SDKs using Mobile key checkbox in your project Settings.

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 this method. If you do use a timeout parameter, do not set it for more than five seconds.

Always include a timeout parameter

Do not configure your SDK to initialize without a timeout parameter. Doing so will cause your app never to load if there is a connectivity problem. We recommend setting a timeout for no more than 0-5 seconds.

In the v4 example, the context key is "context-key-123abc". In the v3 example, the user key is "user-key-123abc".

Here is how to create the client:

LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
LDContext context = LDContext.create("context-key-123abc");
LDClient client = LDClient.init(this.getApplication(), ldConfig, context, 0);

To learn more about the specific configuration options available in this SDK, read LDConfig.Builder.

Evaluate a context

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:

boolean showFeature = client.boolVariation(flagKey, true);
if (showFeature) {
// Application code to show the feature
else {
// The code to run if the feature is off

Shut down the client

Shut down the client when your application terminates. To learn more, read Shutting down.

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: