• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL

EDIT ON GITHUB

Android SDK reference

Read time: 2 minutes
Last edited: Jul 28, 2021
Supported Android SDK versions

This library is compatible with Android SDK API versions 21 and up (Android 5.0, Lollipop).

This reference guide documents all of the methods available in the Android SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source. To learn more, view the source on GitHub or the generated Javadocs. Additionally you can clone and run a sample application that uses this SDK.

Getting started

Building on top of our Getting Started 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:

1implementation 'com.launchdarkly:launchdarkly-android-client-sdk:3.0.0'

The SDK uses AndroidX from Jetpack. For projects not currently using AndroidX, a migration guide is available here.

ProGuard / R8

If you use ProGuard or R8, the configuration for the Android SDK should be automatically included from the aar artifact. If this is not the case for your build, include the Proguard configuration lines from consumer-proguard-rules.pro into your proguard file.

Next, import the LaunchDarkly client in your application code:

1import com.launchdarkly.sdk.*;
2import com.launchdarkly.sdk.android.*;

Once the SDK is installed, create a single, shared instance of LDClient. Specify your mobile key here so that your application is authorized to connect to LaunchDarkly and retrieve flag values for your application and environment.

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

Here is how to create the client:

1LDConfig ldConfig = new LDConfig.Builder()
2 .mobileKey("YOUR_MOBILE_KEY")
3 .build();
4
5LDUser user = new LDUser.Builder("user key")
6 .email("fake@example.com")
7 .build();
8
9LDClient ldClient = LDClient.init(this.getApplication(), ldConfig, user, 5);
Use a mobile key

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 a best practice. The preferred method is shown below. It allows 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 still connects in the background and continually updates itself with the latest flags.

Here is the preferred method:

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

Using ldClient, you can check which variation a particular user receives for a given feature flag:

1boolean showFeature = ldClient.boolVariation(flagKey, true);
2if (showFeature) {
3 // application code to show the feature
4}
5else {
6 // the code to run if the feature is off
7}

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

Data collection

To learn more about data collection within this SDK and implications on submissions to the Apple App Store, read Apple App Store data collection policy.

Supported features

This SDK supports the following features: