Manual Initialization

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 into 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" />
</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. So, we recommend calling SentryAndroid.init in the instance of the Application class right after the application is created.

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.SentryLevel;
import io.sentry.android.core.SentryAndroid;
import android.app.Application;

public class SentryApplication extends Application {
  public void onCreate() {
    super.onCreate();

    SentryAndroid.init(this, options -> {
      options.setDsn("https://examplePublicKey@o0.ingest.sentry.io/0");
      // 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.setBeforeSend((event, hint) -> {
        if (SentryLevel.DEBUG.equals(event.getLevel()))
          return null;
        else
          return event;
      });
    });
  }
}