How to configure sentry environments according to Android project build type (production vs staging)?

Viewed 844

We are working on a Kotlin-based Android project where sentry is already up and running. We would like to make use of the sentry environment tags in order to be able to distinguish between production and staging issues.

What I already know is:

  1. It is possible to set a tag programmatically. However, this is not what we are after.
  2. It is possible to achieve via the sentry-cli. However, this is not what we are after either.
  3. It should be possible to use the sentry.properties file to configure the different environment tags. But I couldn't figure out: which keywords to use? Where in the project structure to place the different sentry.properties files?
  4. By looking at the javascript documentation, I suspect there should be a way to achieve it via the AndroidManifest.xml. Has anyone managed to get it working like this?
3 Answers

you could also use a placeholder from the build script like:

<meta-data android:name="io.sentry.environment" android:value="${environment}" />

then define your placeholder in the build script:

android {
    buildTypes {
        getByName("debug") {
            manifestPlaceholders = mapOf(
                "environment" to "debug"
            )
        }
        getByName("release") {
            manifestPlaceholders = mapOf(
                "environment" to "release"
            )
        }
    }
}

this code snippet is using kotlin kts file, so adapt it if using groovy.

We can achieve it by using configuration, Please check below code to add tags programatically:

Sentry.configureScope(scope -> {
      scope.setTag("Application Name", getString(R.string.app_name));
      scope.setTag("Variant", BuildConfig.BUILD_TYPE);
});

You will get these tags on sentry panel. ;)

enter image description here

You can set it via AndroidManifest.xml (I'm assuming you're using the SDK version 2.0 or later). Here's an example.

<meta-data android:name="io.sentry.environment" android:value="staging" />
Related