> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Kotlin Multiplatform Feature Flags installation 1. 1 ## Install the dependency Required Add the PostHog KMP SDK to your shared module's `commonMain` source set: shared/build.gradle.kts ```kotlin 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](https://central.sonatype.com/artifact/com.posthog/posthog-kmp) if you would rather upgrade deliberately. Kotlin/Wasm support requires `0.2.0` or higher. 2. 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. ## Tab 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 ```kotlin 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 = "", host = "https://us.i.posthog.com", ), context = PostHogContext(this), ) } } ``` Register the class in your `AndroidManifest.xml`: android/src/main/AndroidManifest.xml ```xml ``` ## Tab On iOS, use the no-argument `PostHogContext()`: MainViewController.kt ```kotlin import com.posthog.kmp.PostHog import com.posthog.kmp.PostHogConfig import com.posthog.kmp.PostHogContext fun MainViewController() = ComposeUIViewController { LaunchedEffect(Unit) { PostHog.setup( config = PostHogConfig( apiKey = "", host = "https://us.i.posthog.com", ), context = PostHogContext(), ) } App() } ``` ## Tab On the web (Kotlin/JS and Kotlin/Wasm), use the no-argument `PostHogContext()`: main.kt ```kotlin import com.posthog.kmp.PostHog import com.posthog.kmp.PostHogConfig import com.posthog.kmp.PostHogContext fun main() { PostHog.setup( config = PostHogConfig( apiKey = "", host = "https://us.i.posthog.com", ), context = PostHogContext(), ) } ``` ## Tab On the JVM – for example, a Compose Multiplatform desktop app – use the no-argument `PostHogContext()`: main.kt ```kotlin import com.posthog.kmp.PostHog import com.posthog.kmp.PostHogConfig import com.posthog.kmp.PostHogContext fun main() = application { PostHog.setup( config = PostHogConfig( apiKey = "", host = "https://us.i.posthog.com", ), context = PostHogContext(), ) // ... } ``` 3. 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 ```kotlin import com.posthog.kmp.PostHog PostHog.capture( event = "button_clicked", properties = mapOf( "button_name" to "signup" ) ) ``` 4. 4 ## Evaluate boolean feature flags Required Flags are preloaded on setup, so this call is synchronous: Kotlin ```kotlin if (PostHog.isFeatureEnabled("flag-key")) { // Do something differently for this user // Optional: fetch the payload val payload = PostHog.getFeatureFlagResult("flag-key")?.getPayloadAs>() } ``` 5. 5 ## Evaluate multivariate feature flags Optional For multivariate flags, check which variant the user has been assigned: Kotlin ```kotlin when (PostHog.getFeatureFlag("flag-key")) { "control" -> showOriginalPricing() "variant-key" -> showNewPricing() } ``` 6. 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 ```kotlin PostHog.reloadFeatureFlags { // flags are now up to date } ``` 7. 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. 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. | Resource | Description | | --- | --- | | [Creating a feature flag](/docs/feature-flags/creating-feature-flags.md) | How to create a feature flag in PostHog | | [Adding feature flag code](/docs/feature-flags/adding-feature-flag-code.md) | How to check flags in your code for all platforms | | [Framework-specific guides](/docs/feature-flags/tutorials.md#framework-guides) | Setup guides for React Native, Next.js, Flutter, and other frameworks | | [How to do a phased rollout](/tutorials/phased-rollout.md) | Gradually roll out features to minimize risk | | [More tutorials](/docs/feature-flags/tutorials.md) | Other real-world examples and use cases | ### Still have questions? Ask PostHog AI ### Was this page useful? HelpfulCould be better