Subchapter 50.4
references/sdk/snippets/android-client-sdk.mdMarkdown4 KBView on GitHub
Install the dependency: Install the SDK (opens in a new tab) documents two Gradle styles—the same Maven coordinate, different syntax. Pick the one that matches your app module:
// Gradle Groovy — typically app/build.gradle
implementation 'com.launchdarkly:launchdarkly-android-client-sdk:5.+'// Gradle Kotlin DSL — typically app/build.gradle.kts
implementation("com.launchdarkly:launchdarkly-android-client-sdk:5.+")Import the SDK: Use the imports that match your language (Import the SDK (opens in a new tab)). Observability is optional (separate launchdarkly-observability-android dependency; requires Android client SDK v5.9+).
Kotlin:
import com.launchdarkly.sdk.*
import com.launchdarkly.sdk.android.*
// Optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
// import com.launchdarkly.observability.plugin.Observability
// import com.launchdarkly.sdk.android.integrations.PluginJava:
import com.launchdarkly.sdk.*;
import com.launchdarkly.sdk.android.*;
// Optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
// import com.launchdarkly.observability.plugin.Observability;
// import com.launchdarkly.sdk.android.integrations.Plugin;The init snippet below matches the onboarding initialization example (opens in a new tab): use a placeholder mobile key first, then wire a real value from build configuration (see optional Gradle section). BuildConfig.LAUNCHDARKLY_MOBILE_KEY is not defined unless you add buildConfigField yourself—do not paste it without the Gradle setup.
Includes: Copy-paste onboarding sample below (Kotlin). Place inside your Application subclass (for example onCreate()); this refers to that Application instance.
import com.launchdarkly.sdk.*
import com.launchdarkly.sdk.android.*
// Optional observability plugin, requires LaunchDarkly Android Client SDK v5.9+
// import com.launchdarkly.observability.plugin.Observability
// import com.launchdarkly.sdk.android.integrations.Plugin
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, ldConfig, context, 5)Replace "YOUR_MOBILE_KEY" with your Mobile key from LaunchDarkly Project settings > Environments (see Apply: environment configuration). Never commit real keys in source.
Optional — BuildConfig + Gradle (Kotlin DSL): Enable BuildConfig and pass the key from a non-committed gradle.properties or local.properties value:
// app/build.gradle.kts — excerpt
android {
buildFeatures {
buildConfig = true
}
defaultConfig {
val key = (project.findProperty("LAUNCHDARKLY_MOBILE_KEY") as String?)?.trim().orEmpty()
buildConfigField("String", "LAUNCHDARKLY_MOBILE_KEY", "\"$key\"")
}
}Then change .mobileKey("YOUR_MOBILE_KEY") to .mobileKey(BuildConfig.LAUNCHDARKLY_MOBILE_KEY) and set LAUNCHDARKLY_MOBILE_KEY=… where Gradle can read it (for example ~/.gradle/gradle.properties or a CI secret), not in a committed file.