Android error tracking installation

  1. Install the dependency

    Required

    Add the PostHog Android SDK to your build.gradle dependencies:

    build.gradle
    dependencies {
    implementation("com.posthog:posthog-android:3.+")
    }
  2. Configure PostHog

    Required

    Initialize PostHog in your Application class:

    SampleApp.kt
    class SampleApp : Application() {
    companion object {
    const val POSTHOG_API_KEY = "<ph_project_api_key>"
    const val POSTHOG_HOST = "https://us.i.posthog.com"
    }
    override fun onCreate() {
    super.onCreate()
    // Create a PostHog Config with the given API key and host
    val config = PostHogAndroidConfig(
    apiKey = POSTHOG_API_KEY,
    host = POSTHOG_HOST
    )
    // Setup PostHog with the given Context and Config
    PostHogAndroid.setup(this, config)
    }
    }
  3. Send events

    Recommended

    Once installed, PostHog will automatically start capturing events. You can also manually send events to test your integration:

    Kotlin
    import com.posthog.PostHog
    PostHog.capture(
    event = "button_clicked",
    properties = mapOf(
    "button_name" to "signup"
    )
    )
  4. Set up exception autocapture

    Recommended
    Client-side configuration only

    This configuration is client-side only. Support for remote configuration in the error tracking settings will be added in a future release.

    You can autocapture exceptions by setting the errorTrackingConfig.autoCapture argument to true when initializing the PostHog SDK.

    Kotlin
    import com.posthog.android.PostHogAndroidConfig
    val config = PostHogAndroidConfig(
    apiKey = POSTHOG_API_KEY,
    host = POSTHOG_HOST
    ).apply {
    ...
    errorTrackingConfig.autoCapture = true
    }
    ...

    When enabled, this automatically captures $exception events when errors are thrown by wrapping the Thread.UncaughtExceptionHandler listener.

    Planned features

    We currently don't support source code context associated with an exception.

    These features will be added in a future release.

  5. Manually capture exceptions

    Optional

    It is also possible to manually capture exceptions using the captureException method:

    Kotlin
    PostHog.captureException(
    exception,
    properties = additionalProperties
    )

    This is helpful if you've built your own error handling logic or want to capture exceptions that are handled by your application code.

  6. 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
    Check for exceptions in PostHog
  7. Upload source maps

    Required

    Great, you're capturing exceptions! If you serve minified bundles, the next step is to upload source maps to generate accurate stack traces.

    Let's continue to the next section.

    Upload source maps

Community questions

Was this page useful?

Questions about this page? or post a community question.