• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

Android SDK reference

Read time: 2 minutes
Last edited: Dec 04, 2021

Overview

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. LaunchDarkly's SDKs are open source. The source is available in GitHub or the generated Javadocs. You can also try this SDK out by cloning and running a sample application that uses this SDK.

Getting started

After you complete the Getting Started process, follow these instructions to start 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.1.1'

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. This authorizes your application to connect to LaunchDarkly and retrieve flag values for your application and environment. To create a client instance, you need your environment's mobile key, available in the Projects tab of your account settings page. Be sure to use a mobile key. Never embed a server-side SDK key into a mobile application.

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 client = LDClient.init(this.getApplication(), ldConfig, user, 5);

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.

Making feature flags available to this SDK

You must make feature flags available to mobile SDKs before the flags can be evaluated. If an SDK tries to evaluate a feature flag that is not available, the user will receive the default 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.

Here is the preferred method:

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

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

1boolean showFeature = client.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: