Set up Android SDK

Install the package

1implementation '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
1import com.launchdarkly.sdk.*
2import com.launchdarkly.sdk.android.*;
3import com.launchdarkly.sdk.android.LDConfig.Builder.AutoEnvAttributes
4
5// This is your mobile key.
6val ldConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
7 .mobileKey("YOUR_MOBILE_KEY")
8 .build()
9
10// A "context" is a data object representing users, devices, organizations, and other entities.
11val context = LDContext.create("EXAMPLE_CONTEXT_KEY")
12
13// If you don't want to block execution while the SDK tries to get
14// latest flags, move this code into an async IO task and await on its completion.
15val 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
1import com.launchdarkly.sdk.*
2import com.launchdarkly.sdk.android.*
3import com.launchdarkly.sdk.android.LDConfig.Builder.AutoEnvAttributes
4
5// This is your mobile key.
6val ldConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
7 .mobileKey("YOUR_MOBILE_KEY")
8 .build()
9
10// A "context" is a data object representing users, devices, organizations, and other entities.
11val context = LDContext.create("EXAMPLE_CONTEXT_KEY")
12
13// If you don't want to block execution while the SDK tries to get
14// latest flags, move this code into an async IO task and await on its completion.
15val client: LDClient = LDClient.init(this@BaseApplication, ldConfig, context, 5)
16
17// Call trackMetric when a metric action occurs in your app —
18// a tap, a form submit, a screen view, a custom event, whatever your metric measures.
19fun trackMetric(metricKey: String, data: LDValue = LDValue.ofNull()) {
20 LDClient.get().trackData(metricKey, data)
21}

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.