Set up Android SDK

Install and initialize the LaunchDarkly Android SDK to start evaluating feature flags in your application.

Install the package

implementation 'com.launchdarkly:launchdarkly-android-client-sdk:5.+'

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

Initialize the SDK for feature management

Android SDK initialization
import com.launchdarkly.sdk.*
import com.launchdarkly.sdk.android.*;
import com.launchdarkly.sdk.android.LDConfig.Builder.AutoEnvAttributes
// This is your mobile key.
val ldConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
.mobileKey("YOUR_MOBILE_KEY")
.build()
// A "context" is a data object representing users, devices, organizations, and other entities.
val context = LDContext.create("EXAMPLE_CONTEXT_KEY")
// If you don't want to block execution while the SDK tries to get
// latest flags, move this code into an async IO task and await on its completion.
val client: LDClient = LDClient.init(this@BaseApplication, ldConfig, context, 5)

Initialize the SDK for feature management and Experimentation

If you want to use the LaunchDarkly Experimentation feature, include a track call in your initialization code:

Android SDK initialization for Experimentation
import com.launchdarkly.sdk.*
import com.launchdarkly.sdk.android.*
import com.launchdarkly.sdk.android.LDConfig.Builder.AutoEnvAttributes
// This is your mobile key.
val ldConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
.mobileKey("YOUR_MOBILE_KEY")
.build()
// A "context" is a data object representing users, devices, organizations, and other entities.
val context = LDContext.builder("EXAMPLE_CONTEXT_KEY")
.set("email", "EXAMPLE_EMAIL")
.build()
// If you don't want to block execution while the SDK tries to get
// latest flags, move this code into an async IO task and await on its completion.
val client: LDClient = LDClient.init(this@BaseApplication, ldConfig, context, 5)
// Call trackMetric when a metric action occurs in your app —
// a tap, a form submit, a screen view, a custom event, whatever your metric measures.
fun trackMetric(metricKey: String, data: LDValue = LDValue.ofNull()) {
LDClient.get().trackData(metricKey, data)
}

You can find your server-side SDK keys, mobile keys, and client-side ID on the SDK keys page under Settings.

To learn more, read Initialize the client in the Android SDK reference guide.