LaunchDarkly Developer Documentation

Get started in under 30 minutes.
LaunchDarkly provides feature flags as a service for Java · Python · Ruby · Go · Node.js · PHP · .NET. Control feature launches -- who sees what and when -- without multiple code deploys. Easy dashboard for phased rollouts, targeting and segmenting.
Need more help? write us at

Get Started    Guides

Android SDK Reference

Supported Android SDK Versions

This library is compatible with Android SDK versions 16 and up (4.1 Jelly Bean)

This reference guide documents all of the methods available in our Android SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source-- head to our Android SDK GitHub repository or our JavaDocs to look under the hood.

Getting started

Building on top of our Quickstart guide, the following steps will get you started with using the LaunchDarkly SDK in your Android application.

To get started, declare a dependency on the LaunchDarkly Android SDK.

implementation 'com.launchdarkly:launchdarkly-android-client:2.7.0'


If you're using ProGuard add these lines to your config:

-keep class** { *; }
-keep class org.apache.http.** { *; }
-dontwarn okio.**
-dontwarn okhttp3.**
-dontwarn org.apache.http.**
-dontwarn org.slf4j.**
-dontwarn java.nio.file.*
-dontwarn javax.annotation.**
-dontwarn sun.misc.Unsafe
-dontwarn java.lang.ClassValue
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

Once the SDK is installed, you'll want to create a single, shared instance of LDClient. You should specify your mobile key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

The following example shows the simplest way to create the client. It will block for up to 5 seconds until the latest feature flags have been retrieved from LaunchDarkly.

LDConfig ldConfig = new LDConfig.Builder()

user = new LDUser.Builder("user key")

ldClient = LDClient.init(this.getApplication(), ldConfig, user, 5);

Use a mobile key

Be sure to use a mobile key from your Environments page. Never embed a server-side SDK key into an embedded or desktop application.

However, calling blocking code from the main thread in an Android app is not considered a best practice. The preferred method is shown below. It will allow you to use the client immediately. Flags from the previous launch of the app are stored on the device and retrieved for immediate use. The client will still connect in the background and continually update itself with the latest flags.

LDClient ldClient = LDClient.init(this.getApplication(), ldConfig, user, 0);

Using ldClient, you can check which variation a particular user should receive for a given feature flag.

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

Lastly, when your application is about to terminate, shut down ldClient. This ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events. This is something you only need to do once.


Customizing your client

You can also pass other custom parameters to the client via the configuration object:

LDConfig ldConfig = new LDConfig.Builder()

Here, we've customized the client connect and flush interval parameters. For the full set of config options check out the JavaDoc and the Github Readme


Feature flag targeting and rollouts are all determined by the user you pass to your client. In our Android SDK, we use a builder pattern to make it easy to construct users. Here's an example:

user = new LDUser.Builder("aa0ceb")
   .custom("group", "microsoft")

Let's walk through this snippet. The first argument to the builder is the user's key-- in this case we've used the hash "aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an e-mail address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like firstName, email, and the custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

In addition to built-in attributes like names and e-mail addresses, you can pass us any of your own user data by passing custom attributes, like the groups attribute in the example above.

A note on types

Most of our built-in attributes (like names and e-mail addresses) expect string values. Custom attributes values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers.

If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way. The Android SDK is strongly typed, so be aware of this distinction.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us-- organizations, groups, account plans-- anything you pass to us becomes available instantly on our dashboard.

Private user attributes

You can optionally configure the Android SDK to treat some or all user attributes as private user attributes. Private user attributes can be used for targeting purposes, but are removed from the user data sent back to LaunchDarkly.

In the Android SDK you can define private attributes for the entire LaunchDarkly client. When creating the LDConfig object, you can call the setPrivateAttributes method, which which takes in a set of custom or built-in attributes as a parameter. If any user has a custom or built-in attribute named in this list, it will be removed before the user is sent to LaunchDarkly.

LDConfig ldConfig = new LDConfig.Builder()
  	.setPrivateAttributes(new ArrayList<String>(){{add("email");}})

You can also mark attributes as private when building the user object itself by passing a list of attribute names to the user build method. For example:

user = new LDUser.Builder("aa0ceb")
   .build(new ArrayList<String>(){{add("email");}});

When this user is sent back to LaunchDarkly, the email attribute will be removed.

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

user = new LDUser.Builder("user key")

You can leave the key parameter in the Builder null or make it an empty string and the client will automatically set it to a LaunchDarkly-specific device-unique string that is consistent between app restarts and device reboots.

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. You also can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. This is actually a good thing-- it keeps anonymous users from polluting your Users page!


The variation method determines whether a flag is enabled or not for a specific user. In Android, there is a variation method for each type (e.g. boolVariation, stringVariation):

variationResult = ldClient.boolVariation(flagKey, false);

variation calls take the feature flag key and a fallback value.

The fallback value will only be returned if an error is encountered-- for example, if the feature flag key doesn't exist or the user doesn't have a key specified.

The variation call will automatically create a user in LaunchDarkly if a user with that user key doesn't exist already. There's no need to create users ahead of time (but if you do need to, take a look at Identify).

Handling flag values on initial application launch

When LDClient is initialized for the first time at app launch, users will receive the feature flag fallback values until an initial connection to LaunchDarkly is completed for the first time. Take a look at the section above on various ways to initialize the client.

All flags

Creating users

Note that unlike variation and identify calls, allFlags does not send events to LaunchDarkly. Thus, users are not created or updated in the LaunchDarkly dashboard.

The allFlags method produces a map of feature flag keys to their values for a specific user.



The track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. You can also attach custom JSON data to your event by passing an extra JsonElement parameter to track. Here's a simple example:

ldClient.track("your-goal-key", data)

Offline mode

In some situations, you might want to stop making remote calls to LaunchDarkly and switch to the last known values for your feature flags. offline mode lets you do this easily.

LDConfig ldConfig = new LDConfig.Builder()

ldClient = LDClient.init(this.getApplication(), ldConfig, user);

// Or to switch an already-instantiated client to offline mode:

Airplane/Flight Mode

If a user's device is in airplane/flight mode or if they are not connected to a network, LaunchDarkly will use the latest stored flag settings in memory. If there are no previously stored flag settings, then the fallback values will be used.


Internally, the LaunchDarkly SDK keeps an event buffer for track calls. These are flushed periodically in a background thread. In some situations (for example, if you're testing out the SDK in a simulator), you may want to manually call flush to process events immediately.


Note that the flush interval is configurable-- if you need to change the interval, you can do so via the configuration.

Changing the User Context

If your app is used by multiple users on a single device, you may want to change users and have separate flag settings for each user. To achieve this, the SDK will store the last 5 user contexts on a single device, with support for switching between different user contexts.

You can use the identify method to switch user contexts:

LDUser updatedUser = new LDUser.Builder(user)


The identify() call will load any saved flag values for the new user and immediately trigger an update of the latest flags from LaunchDarkly.

Identify() returns a ListenableFuture. If you want to be sure subsequent code is using the latest values from the server, you can register a listener with the future.

Real-Time Updates

LaunchDarkly manages all flags for a user context in real-time by polling flags based on a real-time event stream. When a flag is modified via the LaunchDarkly dashboard, the flag values for the current user will update almost immediately.

To accomplish real-time updates, LaunchDarkly broadcasts an event stream that is listened to by the Android SDK. Whenever an event is performed on the dashboard, the Android SDK is notified of the updated flag settings in real-time.

To perform real-time updates in your app, your app will need to register listeners for each flag you'd like to watch:

String flagKey = "yourFlagKey";

FeatureFlagChangeListener listener = new FeatureFlagChangeListener() {
  public void onFeatureFlagChange(String flagKey) {
  	bool newValue = newLDClient.get().boolVariation(flagKey, false);

LDClient.get().registerFeatureFlagListener(flagKey, listener);

Similarly you can unregister listeners to disable them:

LDClient.get().unregisterFeatureFlagListener(flagKey, listener);

Multiple Environments

LaunchDarkly's Android SDK supports having multiple LDClient instances tied to separate mobile keys. This allows evaluating flags from multiple environments.

All LDClient instances will evaluate against the same LDUser. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.

Map<String, String> otherKeys = new HashMap<String, String>();
otherKeys.put("platform", "PLATFORM_MOBILE_KEY");
otherKeys.put("core", "CORE_MOBILE_KEY");
LDConfig ldConfig = new LDConfig.Builder()

LDUser user = new LDUser.Builder("user key")
LDClient.init(this.getApplication(), ldConfig, user);

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.

LDClient coreInstance = LDClient.getForMobileKey("core");
coreInstance.boolVariation("core-flag", false);

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

LDClient coreInstance = LDClient.getForMobileKey("core");

// Calls affect all LDClient Instances
coreInstance.identify(/*User Object*/);

Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.

Background Fetch

When the app is backgrounded, the Android SDK does not receive real-time events. However, a battery-conscious Android Alarm is set to poll for updates once every 15 minutes. This periodic polling only happens after the app has been run once since device boot.


The Android SDK makes heavy use of the built-in Android logging capabilities. You won't see much at the Info level, but turn on Debug logging for very descriptive output.

Android SDK Reference

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.