How to set up Feature Flags in Android (Kotlin)
To implement feature flags in an Android app using the Statsig SDK, follow these steps, including creating a basic "Hello World" Android app, integrating Statsig, and managing feature flags in the Statsig console.
Step 1: Create a Basic Android "Hello World" App
-
Install Android Studio:
- Download and install Android Studio from the official website. Launch it and follow the setup wizard to install any necessary SDK packages.
-
Start a New Android Studio Project:
- Open Android Studio and choose to start a new Android project.
- Select the "Empty Activity" template and click "Next".
- Enter your Application name, for example, "HelloWorldApp", and choose a save location.
- Make sure to select Kotlin as the language and the minimum API level supported by your app. API level 21 (Android 5.0 Lollipop) is a good starting point for broad compatibility.
- Click "Finish" to create your project.
-
Modify
MainActivity.kt
:- Open
MainActivity.kt
and ensure it contains code similar to the following for a basic "Hello World" setup:package com.example.helloworldapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.textView)
textView.text = "Hello, World!"
}
}
- Open
-
Update the Layout:
- Ensure
res/layout/activity_main.xml
is updated to include a TextView with@+id/textView
.
- Ensure
Step 2: Integrating Statsig for Feature Flags
-
Add Statsig SDK Dependency:
- Open your app's
build.gradle
(Module: app) file, and add the Statsig SDK dependency to thedependencies
block:implementation 'com.statsig:android-sdk:1.+'
- Open your app's
-
Initialize Statsig in Your Application:
- Modify
MainActivity.kt
to initialize Statsig and check a feature flag:import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.statsig.androidsdk.Statsig
import com.statsig.androidsdk.StatsigOptions
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize Statsig
Statsig.initialize(
this.applicationContext,
"client-sdk-key",
options = StatsigOptions(initTimeoutMs = 6000)
) {
// Check if a feature flag is enabled
val isFeatureEnabled = Statsig.checkGate("example_feature_flag")
runOnUiThread {
val textView: TextView = findViewById(R.id.textView)
if (isFeatureEnabled) {
textView.text = "Feature Enabled!"
} else {
textView.text = "Hello, World!"
}
}
}
}
override fun onDestroy() {
super.onDestroy()
Statsig.shutdown()
}
} - Replace
"client-sdk-key"
with your actual Statsig Client SDK Key. - Replace
"example_feature_flag"
with the name of the feature flag you will create.
- Modify
Step 3: Creating Feature Flags in the Statsig Console
-
Sign Up/Login to Statsig:
- Visit Statsig's website and create an account or log in.
-
Create a New Project:
- In the Statsig console, create a new project for your application.
-
Navigate to Feature Flags:
- Access the "Feature Flags" section from the dashboard.
-
Create a New Feature Flag:
- Click "Create Feature Flag".
- Enter a name for your feature flag (e.g.,
example_feature_flag
). - Configure targeting rules as needed.
- Save your feature flag.
-
Toggle Your Feature Flag:
- You can enable or disable your feature flag from the dashboard and configure it as needed.
This guide outlines creating a simple Android app, integrating Statsig for feature management, and setting up feature flags in the Statsig console. Ensure your client-sdk-key
is the client (not server) key meant for client-side use, and always manage feature flags responsibly to provide the best user experience.