---
title: "Crash-Proofing Your Code: Setting Up Sentry on Android"
description: "Discover how to set up Sentry for the Android platform."
url: "https://cleevio.com/blog/sentry-guide-part-two"
date: "2024-04-04"
author: "Tomáš Dvořáček"
lang: "en"
---

# Crash-Proofing Your Code: Setting Up Sentry on Android
https://cleevio.com/blog/sentry-guide-part-two

Tomáš Dvořáček, Senior Android Developer · 2024-04-04

Discover how to set up Sentry for the Android platform.

Welcome back! As promised, we are here with the second part of the [Crash-proofing series.](https://cleevio.com/blog/sentry-guide-part-one) This time, we will show you how to set up Sentry for the Android platform and some nice-to-have stuff as a bonus.

Let's begin!

#### Create sentry.properties file inside your Android project’s root folder

```swift
# TODO: update this configuration for your project
defaults.project=<project_id>
defaults.org=<organisation_slug>
defaults.url=https://sentry.<organisation_slug>.io/
auth.token=<auth_token>
```

### Initial setup in Sentry Admin

These are a few things you need to do before implementing Sentry Android SDK.

- Create a new project in Sentry. Give this project some appropriate name like **your-project-android**
- Then go to Settings → Projects → Your new Project → General Settings

- You should see the name of your project. This is your **project_id**. In **sentry.properties**, replace **<project_id>** with it
- Then go to Organization settings → General Settings, copy your **organisation_slug** and replace **<organisation_slug>** inside **sentry.properties**

- Then go to User and tokens and create new Token. You can add every permission to it for a start but its better to check every [permission](https://docs.sentry.io/api/permissions/) and decide for yourself what is needed.

- Then copy your new auth token and replace **<auth_token>** with it inside sentry.properties.Sentry can upload ProGuard mapping files for you and needs this auth token. Ignore this if you don’t care about ProGuard
- Your **sentry.properties** should be filled now.
- Then go to Settings → Projects → Your new Project → Client Key (DSN)
- Get your DSN link and save it somewhere for now.

- Now you can return to your Android Project

### Adding sentry to project

A full tutorial is on [this](https://docs.sentry.io/platforms/android/) page. Sentry can set itself with a command, but if you prefer a manual approach or have problems, follow the instructions below or this [page](https://docs.sentry.io/platforms/android/manual-setup/). Sentry will also give you a direct link for setup in Android Studio ([https://sentry.io/organizations/XXX/projects/YYY/getting-started/](https://sentry.cleevio.io/organizations/cleevio/projects/microinfluencers/getting-started/))

1. Add this to your main module **build.gradle**

```swift
// you can find latest version here
// https://mvnrepository.com/artifact/io.sentry/sentry-android
implementation platform('io.sentry:sentry-bom:6.30.0')
implementation('io.sentry:sentry-android') // core library
implementation('io.sentry:sentry-android-fragment') // if you use fragments or
implementation('io.sentry:sentry-compose-android') // if you use compose
```

1. Open your application class and initiate Sentry

```swift
override fun onCreate() {
  super.onCreate()
  SentryAndroid.init(this) { options ->
    options.environment = BuildConfig.FLAVOR + BuildConfig.BUILD_TYPE
    options.tracesSampleRate = if (BuildConfig.DEBUG) SENTRY_FULL_RATE else SENTRY_TRACES_SAMPLE_RATE
    options.sampleRate = if (BuildConfig.DEBUG) SENTRY_FULL_RATE else SENTRY_SAMPLE_RATE
  }  
}

private companion object {
  private const val SENTRY_SAMPLE_RATE = 0.25
  private const val SENTRY_TRACES_SAMPLE_RATE = 0.1
  private const val SENTRY_FULL_RATE = 1.0
}
```

Let’s explore what each line does:

```swift
options.environment = BuildConfig.FLAVOR + BuildConfig.BUILD_TYPE
```

This line applies the environment to the issue. If you use several build flavors like development and production, you can then easily filter specific flavors.

```swift
options.tracesSampleRate = if (BuildConfig.DEBUG) SENTRY_FULL_RATE else SENTRY_TRACES_SAMPLE_RATE
options.sampleRate = if (BuildConfig.DEBUG) SENTRY_FULL_RATE else SENTRY_SAMPLE_RATE
```

These lines set the percentage value of how many traces to log. 100% means everything is logged. It’s not recommended to collect everything in a production environment; however, e.g., on development, it’s a good thing since testers could find bugs. In our example, we set the percentage on the release version to 10%.

1. Open your **AndroidManifest.xml** file inside your main module and insert this

If you don’t want to use CI/CD and just hardcode your DSN, replace **${sentryDsnUrl}**with your collected DSN link.

1. Open app’s **build.gradle** and setup DSN there as well

```swift
// build.gradle
def sentryDndsUrl = System.getenv("SENTRY_DNS_URL") ?: ""

android {
  ...
  defaultConfig {
    ...
    manifestPlaceholders = [sentryDsnUrl: sentryDsnUrl]
  }
  ...
```

1. Add Sentry gradle plugin to project

```swift
// project's build.gradle
buildscript {
  dependencies {
    classpath "io.sentry:sentry-android-gradle-plugin"
  }
  ...
}

plugins {
    id "io.sentry.android.gradle" version "3.13.0"
}

sentry {
    includeProguardMapping = true
    autoUploadProguardMapping = true
    includeDependenciesReport = true

    tracingInstrumentation {
        enabled = true

        features = [InstrumentationFeature.DATABASE, InstrumentationFeature.FILE_IO, InstrumentationFeature.COMPOSE]

        logcat {
            enabled = false
        }
    }
}
```

This code enables sentry gradle plugin. This prepares scripts for your convenience like uploading aab to sentry with proguard mapping files.

### Nice things to know

#### Assign someone to new issue

Every issue in Sentry can be assigned to someone and you can do that manually or automatically with [rules](https://docs.sentry.io/product/issues/ownership-rules/).

#### Create Jira issue directly from Sentry

If you setup the Jira integration mentioned in the previous article, Sentry enables you to quickly and easily create Jira issues from the issue itself.

#### Discover page

[Discover page](https://docs.sentry.io/product/discover-queries/) let’s you quickly query in all yours collected data so you can answer analytics questions or analyse potential health issues in your project. You can use predefined queries or create, edit and delete your own queries. It’s really powerful tool but it’s limited only to *Trial* and *Business* Plan.

#### Pluggins for Sentry

[Sentry for Android](https://docs.sentry.io/platforms/android/configuration/integrations/) offers plugins which let’s you automatically collect more specific data. For example:

```swift
// support for compose
implementation 'io.sentry:sentry-compose-android:6.30.0'

// support for Room logs
implementation 'io.sentry:sentry-android-sqlite:6.30.0'

// navigation logs
implementation 'io.sentry:sentry-android-navigation:6.30.0'

// support for Apollo3 (GraphQL)
implementation 'io.sentry:sentry-apollo-3:6.30.0'

// support for Timber
implementation 'io.sentry:sentry-android-timber:6.30.0'
```

Some stuff like Android Fragments, okHttp events and logcat are already logged automatically from Sentry version 3.1.0.

In order to disable them, change the sentry config in the project’s **build.gradle**.

```swift
sentry {
    ...
    tracingInstrumentation {
        enabled = true
        
        // options in enum InstrumentationFeature
        // InstrumentationFeature.DATABASE
        // InstrumentationFeature.FILE_IO
        // InstrumentationFeature.OKHTTP
        // InstrumentationFeature.COMPOSE

        features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.DATABASE

        // enabling/disabling logcat
        logcat {
            enabled = false
        }
    }
    ...
}
```

#### OkHttp plugin

Like said above, from Sentry 3.1.0, Sentry will automatically log OkHttp event via manipulating your final bytecode and inserting **SentryOkHttpInterceptor.**This will create breadcrumbs which would look like this:

If you use Sentry 3.11.0+, Sentry will also insert **SentryOkHttpEventListener** which will log even more data from http requests. Everything will be logged with logcat:

Thank you for diving into the second part of [our series.](https://cleevio.com/blog/sentry-guide-part-one) If you have any questions, feel free to catch up or reach out.

