> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Kotlin Multiplatform Experiments installation - Docs Copy page # Kotlin Multiplatform Experiments installation - Docs 1. 1 ## Install the dependency Required Add the PostHog KMP SDK to your shared module's `commonMain` source set: shared/build.gradle.kts PostHog AI ```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 PostHog AI ```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 PostHog AI ```xml ``` ## Tab On iOS, use the no-argument `PostHogContext()`: MainViewController.kt PostHog AI ```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 PostHog AI ```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 PostHog AI ```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 ## Implement your experiment Required Experiments run on top of our feature flags. You can define which version of your code runs based on the return value of the feature flag: ```kotlin if (PostHog.getFeatureFlag("your-experiment-feature-flag") == "test") { // do something } else { // It's a good idea to let control variant always be the default behaviour, // so if something goes wrong with flag evaluation, you don't break your app. } ``` 4. 4 ## Run your experiment Required Once you've implemented the feature flag in your code, you'll enable it for a target audience by creating a new experiment in the PostHog dashboard. 5. 5 ## Next steps Recommended Now that you're running experiments, continue with the resources below to learn what else Experiments enables within the PostHog platform. | Resource | Description | | --- | --- | | [Creating an experiment](/docs/experiments/creating-an-experiment.md) | How to create an experiment in PostHog | | [Adding experiment code](/docs/experiments/adding-experiment-code.md) | How to implement experiments for all platforms | | [Statistical significance](/docs/experiments/statistics-bayesian.md) | Understanding when results are meaningful | | [Experiment insights](/docs/experiments/analyzing-results.md) | How to analyze your experiment data | | [More tutorials](/docs/experiments/tutorials.md) | Other real-world examples and use cases | ### Still have questions? Ask PostHog AI ### Was this page useful? HelpfulCould be better