Kotlin/Android Integration Guide

Kotlin Feature Flags

Feature flags for Android and Kotlin — coroutines, Compose, and safe rollouts

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

implementation("com.squareup.okhttp3:okhttp:4.12.0")

3. Evaluate flags

import kotlinx.coroutines.*
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

object FlagBit {
    private val client = OkHttpClient()
    private val cache = mutableMapOf<String, Boolean>()
    private val sdkKey = BuildConfig.FLAGBIT_SDK_KEY

    suspend fun isEnabled(flagKey: String, context: Map<String, String> = emptyMap()): Boolean {
        val cacheKey = "$flagKey:${context.hashCode()}"
        cache[cacheKey]?.let { return it }

        return withContext(Dispatchers.IO) {
            try {
                val body = JSONObject().apply {
                    put("flag_key", flagKey)
                    put("context", JSONObject(context))
                }.toString()

                val request = Request.Builder()
                    .url("https://flagbit.anethoth.com/api/v1/evaluate")
                    .addHeader("X-SDK-Key", sdkKey)
                    .post(body.toRequestBody("application/json".toMediaType()))
                    .build()

                val response = client.newCall(request).execute()
                val value = JSONObject(response.body!!.string()).getBoolean("value")
                cache[cacheKey] = value
                value
            } catch (e: Exception) {
                false
            }
        }
    }
}

// Jetpack Compose usage
@Composable
fun HomeScreen() {
    var showNewLayout by remember { mutableStateOf(false) }

    LaunchedEffect(Unit) {
        showNewLayout = FlagBit.isEnabled("new-home",
            context = mapOf("user_id" to userId, "platform" to "android"))
    }

    if (showNewLayout) {
        NewHomeLayout()
    } else {
        ClassicHomeLayout()
    }
}

// ViewModel pattern
class DashboardViewModel : ViewModel() {
    private val _flags = MutableStateFlow<Map<String, Boolean>>(emptyMap())
    val flags: StateFlow<Map<String, Boolean>> = _flags

    init {
        viewModelScope.launch {
            val newDash = FlagBit.isEnabled("new-dashboard")
            val darkMode = FlagBit.isEnabled("force-dark-mode")
            _flags.value = mapOf("new-dashboard" to newDash, "force-dark-mode" to darkMode)
        }
    }

    fun isEnabled(key: String) = flags.value[key] ?: false
}

Use Cases for Kotlin/Android

Play Store Independence

Toggle features remotely without publishing a new APK — changes are instant.

Compose UI Variants

A/B test different Jetpack Compose layouts and measure engagement differences.

Device Targeting

Enable features only on tablets, or only on Android 14+, using context-based rules.

Gradual Rollouts

Roll out to 1% of Android users, monitor ANR rates, then increase gradually.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Kotlin/Android app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

Cache flag values in SharedPreferences or DataStore. Load cached values first, refresh when online.

Yes — use LaunchedEffect for async evaluation and mutableStateOf for reactive UI updates.

Yes — evaluate flags in viewModelScope.launch and expose results via StateFlow.

Use Ktor client instead of OkHttp for KMP compatibility. The FlagBit API works from any HTTP client.

Start using feature flags in Kotlin/Android

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key