Kotlin Multiplatform Feature Flags installation
Install with the wizard
Run this command in your project directory to add PostHog to your Kotlin Multiplatform project. The wizard detects your framework, installs the SDK, and sets up your first events.
Or set it up manually by following the rest of this guide.
- 1
Install the dependency
RequiredAdd the PostHog KMP SDK to your shared module's
commonMainsource set:shared/build.gradle.kts0.+resolves to the latest0.xrelease. 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 requires0.2.0or higher. - 2
Configure PostHog
RequiredCall
PostHog.setup()once, early in your app's lifecycle. The config is shared across every target – only thePostHogContextdiffers per platform.Android needs the
Applicationinstance, so set PostHog up from yourApplicationclass – not from an Activity, whoseonCreatere-runs on every recreation (for example, on rotation):MyApplication.ktRegister the class in your
AndroidManifest.xml:android/src/main/AndroidManifest.xmlOn iOS, use the no-argument
PostHogContext():MainViewController.ktOn the web (Kotlin/JS and Kotlin/Wasm), use the no-argument
PostHogContext():main.ktOn the JVM – for example, a Compose Multiplatform desktop app – use the no-argument
PostHogContext():main.kt - 3
Send events
RecommendedOnce installed, PostHog automatically captures app lifecycle events on Android and iOS. Send an event manually from shared code to test your integration:
Kotlin - 4
Evaluate boolean feature flags
RequiredFlags are preloaded on setup, so this call is synchronous:
Kotlin - 5
Evaluate multivariate feature flags
OptionalFor multivariate flags, check which variant the user has been assigned:
Kotlin - 6
Reload flags after identifying
OptionalFlags 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 - 7
Running experiments
OptionalExperiments 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
RecommendedNow 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 How to create a feature flag in PostHog Adding feature flag code How to check flags in your code for all platforms Framework-specific guides Setup guides for React Native, Next.js, Flutter, and other frameworks How to do a phased rollout Gradually roll out features to minimize risk More tutorials Other real-world examples and use cases





