Kotlin Multiplatform Feature Flags installation

  1. Install the dependency

    Required

    Add the PostHog KMP SDK to your shared module's commonMain source set:

    shared/build.gradle.kts
    kotlin {
    sourceSets {
    commonMain.dependencies {
    implementation("com.posthog:posthog-kmp:0.+")
    }
    }
    }

    0.+ resolves to the latest 0.x release. The SDK is a pre-release, so the API can change between minor versions – pin an exact version from Maven Central if you would rather upgrade deliberately. Kotlin/Wasm support requires 0.2.0 or higher.

  2. Configure PostHog

    Required

    Call PostHog.setup() once, early in your app's lifecycle. The config is shared across every target – only the PostHogContext differs per platform.

    Android needs the Application instance, so set PostHog up from your Application class – not from an Activity, whose onCreate re-runs on every recreation (for example, on rotation):

    MyApplication.kt
    import android.app.Application
    import com.posthog.kmp.PostHog
    import com.posthog.kmp.PostHogConfig
    import com.posthog.kmp.PostHogContext
    class MyApplication : Application() {
    override fun onCreate() {
    super.onCreate()
    PostHog.setup(
    config = PostHogConfig(
    apiKey = "<ph_project_token>",
    host = "https://us.i.posthog.com",
    ),
    context = PostHogContext(this),
    )
    }
    }

    Register the class in your AndroidManifest.xml:

    android/src/main/AndroidManifest.xml
    <application android:name=".MyApplication" ...>
  3. Send events

    Recommended

    Once installed, PostHog automatically captures app lifecycle events on Android and iOS. Send an event manually from shared code to test your integration:

    Kotlin
    import com.posthog.kmp.PostHog
    PostHog.capture(
    event = "button_clicked",
    properties = mapOf(
    "button_name" to "signup"
    )
    )
  4. Evaluate boolean feature flags

    Required

    Flags are preloaded on setup, so this call is synchronous:

    Kotlin
    if (PostHog.isFeatureEnabled("flag-key")) {
    // Do something differently for this user
    // Optional: fetch the payload
    val payload = PostHog.getFeatureFlagResult("flag-key")?.getPayloadAs<Map<String, Any>>()
    }
  5. Evaluate multivariate feature flags

    Optional

    For multivariate flags, check which variant the user has been assigned:

    Kotlin
    when (PostHog.getFeatureFlag("flag-key")) {
    "control" -> showOriginalPricing()
    "variant-key" -> showNewPricing()
    }
  6. Reload flags after identifying

    Optional

    Flags are fetched on setup and cached. If a user's properties change – for example, after identify – reload them so they reflect the latest state:

    Kotlin
    PostHog.reloadFeatureFlags {
    // flags are now up to date
    }
  7. Running experiments

    Optional

    Experiments run on top of our feature flags. Once you've implemented the flag in your code, you run an experiment by creating a new experiment in the PostHog dashboard.

  8. Next steps

    Recommended

    Now that you're evaluating flags, continue with the resources below to learn what else Feature Flags enables within the PostHog platform.

    ResourceDescription
    Creating a feature flagHow to create a feature flag in PostHog
    Adding feature flag codeHow to check flags in your code for all platforms
    Framework-specific guidesSetup guides for React Native, Next.js, Flutter, and other frameworks
    How to do a phased rolloutGradually roll out features to minimize risk
    More tutorialsOther real-world examples and use cases

Still have questions?

Was this page useful?