Kotlin Multiplatform Error Tracking installation
- 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
Enable exception autocapture in PostHog
RequiredIn your project's Error Tracking settings, enable Enable exception autocapture. This project setting acts as a server-side kill switch, so the SDK does not automatically capture exceptions when it is disabled.
- 5
Set up exception autocapture
RequiredVersion requirementThe error tracking configuration requires PostHog KMP version 0.4.0 or higher.
Both the project setting from the previous step and
errorTracking.autoCapturemust be enabled. Replace thePostHog.setup()call from the configuration step with the appropriate example below. The optionalinAppIncludesprefixes mark matching stack frames as in-app code on Android, iOS, and JVM. Kotlin/JS and Kotlin/Wasm ignoreinAppIncludes.Keep this call inside the
Application.onCreate()method from the Android configuration example:MyApplication.ktOn iOS, web, and JVM, keep using the no-argument
PostHogContext()from the configuration example:KotlinOn Android, iOS, and JVM, this captures unhandled exceptions. On Kotlin/JS and Kotlin/Wasm, it captures unhandled errors and unhandled promise rejections. It does not capture browser console errors.
- 6
Manually capture exceptions
OptionalCall
PostHog.captureExceptionfor handled exceptions or errors caught by your application code. You can include additional properties to help investigate the exception.Kotlin - 7

