Skip to content

launchdarkly/android-client-sdk

Repository files navigation

LaunchDarkly Android SDK

This library is compatible with Android SDK versions 16 and up (4.1 Jelly Bean)

How to use: Check out the included example app, or follow things here:

  1. Declare this dependency:

    compile 'com.launchdarkly:launchdarkly-android-client:2.6.0'
    
  2. In your application configure and initialize the client:

    LDConfig ldConfig = new LDConfig.Builder()
    	.setMobileKey("YOUR_MOBILE_KEY")
    	.build();
    
    LDUser user = new LDUser.Builder("user key")
       .email("fake@example.com")
       .build();
    
    // NOTE: This method blocks for up to 5 seconds. See Javadoc or http://docs.launchdarkly.com/docs/android-sdk-reference
    // for nonblocking options.
    LDClient ldClient = LDClient.init(this.getApplication(), ldConfig, user, 5);
    
  3. Evaluation example:

    variationResult = ldClient.stringVariation(flagKey, "fallback");
    
  4. Updating the User:

    LDUser updatedUser = new LDUser.Builder(user)
       .email("fake2@example.com")
       .build();
    
    ldClient.identify(user);
    

ProGuard Config

If you're using ProGuard add these lines to your config:

-keep class com.launchdarkly.android.** { *; }
-keep class org.apache.http.** { *; }
-dontwarn okio.**
-dontwarn okhttp3.**
-dontwarn org.apache.http.**
-dontwarn org.slf4j.**
-dontwarn com.google.common.**
-dontwarn java.nio.file.*
-dontwarn javax.annotation.**
-dontwarn sun.misc.Unsafe
-dontwarn java.lang.ClassValue
-dontwarn com.google.j2objc.annotations.Weak
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

Feature Flag Updating

The LaunchDarkly Android SDK defaults to what we have found to be the best combination of low latency updates and minimal battery drain:

  1. When the app is foregrounded a Server-Sent Events streaming connection is made to LaunchDarkly. This streaming connection stays open as long as your app is in the foreground and is connected to the internet.
  2. When the app is backgrounded, the stream connection is terminated and the SDK will poll (with caching) for flag updates every 15 minutes.
  3. When the app is foregrounded, we fetch the latest flags and reconnect to the stream.
  4. In either the foreground or background, we don't try to update unless your device has internet connectivity.

This configuration means that you will get near real-time updates for your feature flag values when the app is in the foreground.

Other Options

If you prefer other options, here they are:

  1. Streaming can be disabled in favor of polling updates. To disable streaming call .setStream(false) on the LDConfig.Builder object.
  2. The default polling interval is 5 minutes. To change it call .setPollingIntervalMillis() on the LDConfig.Builder object.
  3. Background polling can be disabled (the app will only receive updates when the app is in the foreground). To disable background updating call .setDisableBackgroundUpdating(true) on the LDConfig.Builder object.
  4. The background polling interval can be adjusted (with the same minimum of 60 seconds). To change it call .setBackgroundPollingIntervalMillis() on the LDConfig.Builder object.

Example config with streaming disabled and custom polling intervals:

 LDConfig config = new LDConfig.Builder()
                .setStream(false)
                .setPollingIntervalMillis(600_000) // 10 minutes
                .setBackgroundPollingIntervalMillis(3_600_000) // 1 hour
                .build();

Known Issues/Features not yet implemented:

  • Make Android linter happy

Learn more

Check out our documentation for in-depth instructions on configuring and using LaunchDarkly. You can also head straight to the complete reference guide for this SDK or our Javadocs.

Testing

Much of the behavior we want to assert is around complicated device state changes such as app backgrounding, loss of internet connection. These are problematic to test in a programmatic way, so we rely on a combination of automated emulator tests and manual tests.

If, when running tests, the Android Studio build starts throwing countDebugDexMethods and countReleaseDexMethods errors using the run configuration dropdown, then switch to the command-line and exclude the two DEX methods causing the trouble.

./gradlew -x :launchdarkly-android-client:countDebugDexMethods -x :launchdarkly-android-client:countReleaseDexMethods -x :launchdarkly-android-client:signArchives --stacktrace clean build test cAT

About LaunchDarkly

  • LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
    • Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
    • Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
    • Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
    • Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline.
  • LaunchDarkly provides feature flag SDKs for
  • Explore LaunchDarkly