Android error tracking installation

Planned features

We currently don't support demangling stacktraces of minified builds (isMinifyEnabled=true) or source code context associated with an exception.

These features will be added in a future release.

  1. Install PostHog Android SDK

    Required
    SDK version requirement

    A minimum SDK version of 3.24.0 is required, but we recommend keeping up to date with the latest version to ensure you have all of error tracking's features.

    The best way to install the PostHog Android library is with a build system like Gradle. This ensures you can easily upgrade to the latest versions.

    All you need to do is add the posthog-android module to your App's build.gradle or build.gradle.kts:

    app/build.gradle
    dependencies {
    implementation("com.posthog:posthog-android:3.+")
    }

    Configuration

    The best place to initialize the client is in your Application subclass.

    Kotlin
    import android.app.Application
    import com.posthog.android.PostHogAndroid
    import com.posthog.android.PostHogAndroidConfig
    class SampleApp : Application() {
    companion object {
    const val POSTHOG_API_KEY = "<ph_project_api_key>"
    // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com'
    const val POSTHOG_HOST = "https://us.i.posthog.com"
    }
    override fun onCreate() {
    super.onCreate()
    val config = PostHogAndroidConfig(
    apiKey = POSTHOG_API_KEY,
    host = POSTHOG_HOST
    )
    PostHogAndroid.setup(this, config)
    }
    }
  2. 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.

  3. 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.

  4. Verify error tracking

    Checkpoint
    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

Community questions

Was this page useful?

Questions about this page? or post a community question.