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

    EDIT ON GITHUB

    Android SDK reference

    Read time: 2 minutes
    Last edited: Sep 27, 2022

    Overview

    This topic documents how to get started with the Android SDK, and links to reference information on all of the supported features.

    SDK quick links

    LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

    ResourceLocation
    SDK API documentationSDK API docs
    GitHub repositoryandroid-client-sdk
    Sample applicationhello-android
    Published moduleMaven
    SDK version compatibility

    The LaunchDarkly Android SDK is compatible with Android SDK versions 21 and higher (Android 5.0, Lollipop).

    Getting started

    After you complete the Getting started process, follow these instructions to start using the LaunchDarkly SDK in your Android application.

    First, declare a dependency on the LaunchDarkly Android SDK:

    implementation 'com.launchdarkly:launchdarkly-android-client-sdk:3.1.1'

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

    Using ProGuard or R8

    If you use ProGuard or R8, the aar artifact should automatically include the configuration for the Android SDK. 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:

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

    After you install the SDK, create a single, shared instance of LDClient. To create a client instance, you need your environment's mobile key. This authorizes your application to connect to a particular environment within LaunchDarkly. Your mobile key is available in the Projects tab of your Account settings page.

    Never embed a server-side SDK key into a client-side application

    Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.

    LDClient must be a singleton

    It's important to make LDClient a singleton. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

    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:

    LDConfig ldConfig = new LDConfig.Builder()
    .mobileKey("YOUR_MOBILE_KEY")
    .build();
    LDUser user = new LDUser.Builder("user key")
    .email("fake@example.com")
    .build();
    LDClient 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 allows you to use the client immediately. The app stores flags from the previous launch on the device and retrieves them 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 SDK can evaluate those flags. 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:

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

    You can use client to check which variation a particular user will receive for a feature flag.

    Here's how:

    boolean showFeature = client.boolVariation(flagKey, true);
    if (showFeature) {
    // Application code to show the feature
    }
    else {
    // The code to run if the feature is off
    }

    Shutting down

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

    Data collection

    The data collected by the Android SDK persists until the number of cached users exceeds a limit. When you call identify, the number of cached users increments. Eventually, the number of cached users exceeds maxCachedUsers. When that happens, user data in excess of maxCachedUsers is deleted, starting with the oldest user first.

    Supported features

    This SDK supports the following features: