> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt

# Kotlin Multiplatform Error Tracking installation - Docs

Copy page

# Kotlin Multiplatform Error Tracking 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 = "<ph_project_token>",
                    host = "https://us.i.posthog.com",
                ),
                context = PostHogContext(this),
            )
        }
    }
    ```

    Register the class in your `AndroidManifest.xml`:

    android/src/main/AndroidManifest.xml

    PostHog AI

    ```xml
    <application android:name=".MyApplication" ...>
    ```

    ## 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 = "<ph_project_token>",
                    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 = "<ph_project_token>",
                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 = "<ph_project_token>",
                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

    PostHog AI

    ```kotlin
    import com.posthog.kmp.PostHog
    PostHog.capture(
        event = "button_clicked",
        properties = mapOf(
            "button_name" to "signup"
        )
    )
    ```

4.  4

    ## Enable exception autocapture in PostHog

    Required

    In your project's [Error Tracking settings](https://app.posthog.com/settings/project-error-tracking#exception-autocapture), 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.  5

    ## Set up exception autocapture

    Required

    **Version requirement**

    The error tracking configuration requires PostHog KMP version 0.4.0 or higher.

    Both the project setting from the previous step and `errorTracking.autoCapture` must be enabled. Replace the `PostHog.setup()` call from the configuration step with the appropriate example below. The optional `inAppIncludes` prefixes mark matching stack frames as in-app code on Android, iOS, and JVM. Kotlin/JS and Kotlin/Wasm ignore `inAppIncludes`.

    ## Tab

    Keep this call inside the `Application.onCreate()` method from the Android configuration example:

    MyApplication.kt

    PostHog AI

    ```kotlin
    import com.posthog.kmp.ErrorTrackingConfig
    import com.posthog.kmp.PostHog
    import com.posthog.kmp.PostHogConfig
    import com.posthog.kmp.PostHogContext
    PostHog.setup(
        config = PostHogConfig(
            apiKey = "<ph_project_token>",
            host = "https://us.i.posthog.com",
            errorTracking = ErrorTrackingConfig(
                autoCapture = true,
                inAppIncludes = listOf("com.example"),
            ),
        ),
        context = PostHogContext(this),
    )
    ```

    ## Tab

    On iOS, web, and JVM, keep using the no-argument `PostHogContext()` from the configuration example:

    Kotlin

    PostHog AI

    ```kotlin
    import com.posthog.kmp.ErrorTrackingConfig
    import com.posthog.kmp.PostHog
    import com.posthog.kmp.PostHogConfig
    import com.posthog.kmp.PostHogContext
    PostHog.setup(
        config = PostHogConfig(
            apiKey = "<ph_project_token>",
            host = "https://us.i.posthog.com",
            errorTracking = ErrorTrackingConfig(
                autoCapture = true,
                inAppIncludes = listOf("com.example"),
            ),
        ),
        context = PostHogContext(),
    )
    ```

    On 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.  6

    ## Manually capture exceptions

    Optional

    Call `PostHog.captureException` for handled exceptions or errors caught by your application code. You can include additional properties to help investigate the exception.

    Kotlin

    PostHog AI

    ```kotlin
    try {
        riskyOperation()
    } catch (error: Exception) {
        PostHog.captureException(
            throwable = error,
            additionalProperties = mapOf("context" to "checkout_flow"),
        )
    }
    ```

7.  ## Verify error tracking

    Recommended

    *Confirm events are being sent to PostHog*

    Before proceeding, let's make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.

    ![Activity feed with events](https://res.cloudinary.com/dmukukwp6/image/upload/SCR_20250729_ouxl_f788dd8cd2.png)![Activity feed with events](https://res.cloudinary.com/dmukukwp6/image/upload/SCR_20250729_owae_7c3490822c.png)

    [Check for exceptions in PostHog](https://app.posthog.com/activity/explore)

8.  7

    ## Upload debug symbols

    Optional

    If you ship minified or native code on a supported KMP target, upload that target's debug symbols so PostHog can generate accurate stack traces.

    Let's continue to the next section.

    [Upload debug symbols](/docs/error-tracking/upload-debug-symbols/kmp.md)

### Was this page useful?

HelpfulCould be better