Feature flagging with launchdarkly in Android

Malcolm Maima
6 min readOct 24, 2022

--

Introduction

What is feature flagging? Feature flagging is a technique that allows you to turn features on and off without having to redeploy your application. This is useful when you want to test a feature with a small group of users before releasing it to everyone. It is also useful when you want to turn off a feature that is causing problems. This technique is also known as feature flipping, feature toggling, and release toggling.

Gone are the days when you have to do an emergency patch to maybe silence a problematic feature or silence a buggy feature. Now some may argue that a feature flag is just a fancy if else statement. To some extent it is, but it is also important to realize that feature flags provide you with more control and measurable metrics that can be used in scenarios such as A/B testing. Say you want to turn off a specific feature for users in a specific region. You can do that with a feature flag, or even turn off/on a specific feature for users that meet a certain criterion.

In this article we’re going to look at how to use LaunchDarkly to implement feature flags in your android application.

What is LaunchDarkly?

LaunchDarkly is a feature flagging and management platform. It allows you to manage feature flags and target specific users. It also provides you with metrics on how your feature flags are performing. LaunchDarkly provides SDKs for a variety of languages and frameworks. You can find the full list of SDKs [here].

Setting up LaunchDarkly

To get started with LaunchDarkly, you need to create an account [here].

Once you’ve created an account, you can create a new project. You can name your project whatever you want. For this article, I named my project Android Feature Flags.

Once you’ve created your project, you can create your first feature flag. To do this, click on the create flag button (found in Feature Flags tab).

You can name your feature flag whatever you want. For this article, I named my feature flag show_new_onboarding. You can also set a description for your feature flag. This is useful if you want to provide more context to your team members. You can also set a default value for your feature flag. This is useful if you want to enable or disable your feature flag by default. For this article, I set the default value to `false`.

Also remember to select SDKs using mobile key. You’re all set on the LaunchDarkly dashboard. Now let’s move on to the next step.

Setting up your Android project

To get started, you need to add the LaunchDarkly SDK to your project. To do this, add the following dependency to your app level `build.gradle` file.

Next, you need to initialize the LaunchDarkly SDK. To do this, create a new class called `LaunchDarklyManager`. This class will be responsible for initializing the LaunchDarkly SDK and providing a way to access the LaunchDarkly client. Add the following code to your `LaunchDarklyManager` class.

In the code above, we’re creating a singleton class called `LaunchDarklyManager`. This class is responsible for initializing the LaunchDarkly SDK and providing a way to access the LaunchDarkly client.

We’re also creating a companion object that will be used to access the singleton instance of the `LaunchDarklyManager` class. We’re also creating a method called `init` that will be used to initialize the LaunchDarkly SDK.

We’re also creating a method called `getLDClient` that will be used to access the LaunchDarkly client.

Next, you need to initialize the LaunchDarkly SDK in your `Application` class. To do this, add the following code to your `onCreate` method.

Next, you need to create a new class called `FeatureFlag`. This class will be responsible for providing a way to access your feature flags. Add the following code to your `FeatureFlag` class.

In the code above, we’re creating a companion object that will be used to access our feature flags.

We’re also creating a method called `showNewOnboarding` that will be used to access our `show_new_onboarding` feature flag. We’re also using the `boolVariation` method to access the value of our feature flag.

The `boolVariation` method takes two parameters. The first parameter is the name of the feature flag. The second parameter is the default value of the feature flag. In this case, we’re setting the default value to `false`.

Since we’re able to get the value of our feature flag, we can now decide to show or hide our new onboarding screen based on the value of our feature flag. To do this, add the following code to your `MainActivity` class.

In the code above, we’re checking the value of our `show_new_onboarding` feature flag. If the value of our feature flag is `true`, we’re starting the `NewOnboardingActivity`. If the value of our feature flag is `false`, we’re starting the `OldOnboardingActivity`.

Targeting specific users

Now that we’ve seen how to use feature flags in our android application, let’s look at how we can target specific users. To do this, we’re going to use the `user` object. The `user` object is used to target specific users.

To create a `user` object, add the following code to your `LaunchDarklyManager` class.

In the code above, we’re creating a `user` object and setting the `firstName`, `lastName`, `email` and `groups` attributes. We’re also setting the `user_key` attribute.

The `user_key` attribute is used to uniquely identify a user. Once we’ve created the `user` object, we’re identifying the user using the `identify` method.

Now that we’ve identified the user, we can target the user using the `user` object that we created and passed to the `identify` method.

Now if you run your application, the user data will be sent to LaunchDarkly. You can then target the user using the `user` object.

To do this, go to your LaunchDarkly dashboard and click on the `Users` tab. You should see the user data that you sent to LaunchDarkly. You can then target the user using the `user` object.

You can also go to the `Flags` tab and click on the `show_new_onboarding` feature flag. You should see the user data that you sent to LaunchDarkly. You can then target the user using the `user` object.

By toggling the button ON/OFF launchdarkly sdk is able to pick up flag changes on the client app.

Conclusion

In this tutorial, we’ve seen how to use feature flags in our android application. We’ve also seen how to target specific users using the `user` object. Launchdarkly provides a sample android application that you can use to test your feature flags. You can find the sample android application [here].

The SDK documentation can also be found [here]

There are a lot of other features that LaunchDarkly provides, but we’ll be covering them in future tutorials. If you have any questions, feel free to leave a comment below.

--

--

Malcolm Maima

Android Engineer @ Equity Bank — I code stuff…I eat a lot too, coffee makes me sleepy Love cartoons and robots. http://linktr.ee/malcolmmaima