Manual Setup

Learn how to set up the SDK manually.

If you can't (or prefer not to) run the automatic setup, you can follow the instructions below to configure your application manually.

The easiest way to get started is to install the Sentry Android Gradle plugin to your app module's build.gradle file.

app/build.gradle
Copied
plugins {
  id "com.android.application"
  id "io.sentry.android.gradle" version "6.1.0"
}

Version 6.1.0 of the plugin will automatically add the Sentry Android SDK (version 8.34.0) to your app.

If you don't want the Sentry Gradle plugin to install the Sentry SDK automatically, you can define the dependency directly in the build.gradle file of your app module.

app/build.gradle
Copied
dependencies {
    implementation 'io.sentry:sentry-android:8.34.0'
}

Configuration is done via the application AndroidManifest.xml. Here's an example config which should get you started:

AndroidManifest.xml
Copied
<application>
  <!-- Required: set your sentry.io project identifier (DSN) -->
  <meta-data
    android:name="io.sentry.dsn"
    android:value="___PUBLIC_DSN___"
  />

  <!-- Add data like request headers, user ip address and device name, see https://docs.sentry.io/platforms/android/data-management/data-collected/ for more info -->
  <meta-data
    android:name="io.sentry.send-default-pii"
    android:value="true"
  />

  <!-- enable automatic traces for user interactions (clicks, swipes, scrolls) -->
  <meta-data
    android:name="io.sentry.traces.user-interaction.enable"
    android:value="true"
  />
  <!-- enable screenshot for crashes -->
  <meta-data
    android:name="io.sentry.attach-screenshot"
    android:value="true"
  />
  <!-- enable view hierarchy for crashes -->
  <meta-data
    android:name="io.sentry.attach-view-hierarchy"
    android:value="true"
  />

  <!-- enable the performance API by setting a sample-rate, adjust in production env -->
  <meta-data
    android:name="io.sentry.traces.sample-rate"
    android:value="1.0"
  />

  <!-- Enable UI profiling, adjust in production env. This is evaluated only once per session -->
  <meta-data
    android:name="io.sentry.traces.profiling.session-sample-rate"
    android:value="1.0"
  />
  <!-- Set profiling mode. For more info see https://docs.sentry.io/platforms/android/profiling/#enabling-ui-profiling -->
  <meta-data
    android:name="io.sentry.traces.profiling.lifecycle"
    android:value="trace"
  />
  <!-- Enable profiling on app start. The app start profile will be stopped automatically when the app start root span finishes -->
  <meta-data
    android:name="io.sentry.traces.profiling.start-on-app-start"
    android:value="true"
  />

  <!-- record session replays for 100% of errors and 10% of sessions -->
  <meta-data
    android:name="io.sentry.session-replay.on-error-sample-rate"
    android:value="1.0"
  />
  <meta-data
    android:name="io.sentry.session-replay.session-sample-rate"
    android:value="0.1"
  />

  <!-- If your application has strict PII requirements we recommend using the Canvas screenshot strategy. 
  See the Android Session Replay documentation for details: https://docs.sentry.io/platforms/android/session-replay/#screenshot-strategy -->
  <!-- <meta-data android:name="io.sentry.session-replay.screenshot-strategy" android:value="canvas" /> -->

  <!-- enable logs to be sent to Sentry. Use Sentry.logger() to capture logs or check the available integrations that capture logs automatically: https://docs.sentry.io/platforms/android/logs/#integrations -->
  <meta-data android:name="io.sentry.logs.enabled" android:value="true" />

  <!-- enable tombstone support for richer Native crashes context. See more at https://docs.sentry.io/platforms/android/configuration/tombstones/ -->
  <meta-data
    android:name="io.sentry.tombstone.enable"
    android:value="true"
  />
</application>

Under the hood, Sentry uses a ContentProvider to initialize the SDK based on the values provided above. This way, the SDK can capture important crashes and metrics right from the app start.

Initialize the SDK manually when you need to provide additional configuration to the SDK that cannot be specified in the manifest.

To initialize the SDK manually, disable the auto-initialization. You can do so by adding the following line to your manifest:

AndroidManifest.xml
Copied
<application>
    <meta-data android:name="io.sentry.auto-init" android:value="false" />
</application>

Or, to completely remove the merging of the ContentProvider:

AndroidManifest.xml
Copied
<application>
    <provider
    android:name="io.sentry.android.core.SentryInitProvider"
    android:authorities="${applicationId}.SentryInitProvider"
    tools:node="remove"
  />

    <provider
    android:name="io.sentry.android.core.SentryPerformanceProvider"
    android:authorities="${applicationId}.SentryPerformanceProvider"
    tools:node="remove"
  />
</application>

The next step is to initialize the SDK directly in your code.

The SDK can catch errors and crashes only after you've initialized it. For that reason, we recommend calling SentryAndroid.init in your Application class as soon as the application is created. If you don't already have a custom Application class, check out the official docs on how to create one.

Configuration options will be loaded from the manifest so that you don't need to have the static properties in your code. In the init method, you can provide a callback that will modify the configuration and also register new options.

Copied
import io.sentry.ScreenshotStrategyType;
import io.sentry.SentryLevel;
import io.sentry.ProfileLifecycle;
import io.sentry.android.core.SentryAndroid;
import android.app.Application;
import io.sentry.SentryOptions

class MyApplication : Application() {
  override fun onCreate() {
    super.onCreate()

    SentryAndroid.init(this) { options ->
      // Required: set your sentry.io project identifier (DSN)
      options.dsn = "___PUBLIC_DSN___"
      // Add data like request headers, user ip address and device name, see https://docs.sentry.io/platforms/android/data-management/data-collected/ for more info
      options.isSendDefaultPii = true
      // enable automatic traces for user interactions (clicks, swipes, scrolls)
      options.isEnableUserInteractionTracing = true
      // enable screenshot for crashes
      options.isAttachScreenshot = true
      // enable view hierarchy for crashes
      options.isAttachViewHierarchy = true
      // enable the performance API by setting a sample-rate, adjust in production env
      options.tracesSampleRate = 1.0
      // enable UI profiling, adjust in production env. This is evaluated only once per session
      options.profileSessionSampleRate = 1.0
      // set profiling mode. For more info see https://docs.sentry.io/platforms/android/profiling/#enabling-ui-profiling
      options.profileLifecycle = ProfileLifecycle.TRACE
      // enable profiling on app start. The app start profile will be stopped automatically when the app start root span finishes
      options.isStartProfilerOnAppStart = true
      // record session replays for 100% of errors and 10% of sessions
      options.sessionReplay.sessionSampleRate = 0.1
      options.sessionReplay.onErrorSampleRate = 1.0

      // If your application has strict PII requirements we recommend using the Canvas screenshot strategy.
      // See the Android Session Replay documentation for details: https://docs.sentry.io/platforms/android/session-replay/#screenshot-strategy
      // options.sessionReplay.screenshotStrategy = ScreenshotStrategyType.CANVAS

      // enable logs to be sent to Sentry. Use Sentry.logger() to capture logs or check the available integrations that capture logs automatically: https://docs.sentry.io/platforms/android/logs/#integrations
      options.logs.isEnabled = true;

      // enable tombstone support for richer Native crashes context. See more at https://docs.sentry.io/platforms/android/configuration/tombstones/
      options.isTombstoneEnabled = true;

      // Add a callback that will be used before the event is sent to Sentry.
      // With this callback, you can modify the event or, when returning null, also discard the event.
      options.beforeSend =
        SentryOptions.BeforeSendCallback { event, hint ->
          if (SentryLevel.DEBUG == event.level) {
            null
          } else {
            event
          }
        }
    }
  }
}

Additional options can be found on our dedicated options page.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").