Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 111 additions & 81 deletions docs/platforms/android/index.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Android
description: "Learn how to set up Sentry's Android SDK for error monitoring and performance tracking."
description: "Learn how to set up Sentry's Android SDK, capture your first events, and monitor performance."
caseStyle: camelCase
supportLevel: production
sdk: sentry.java.android
Expand All @@ -22,30 +22,6 @@ If you don't already have an account and Sentry project established, head over t

</Alert>

## Features

In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also collect and analyze performance profiles from real users with [profiling](/product/profiling/).

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

<OnboardingOptionButtons
options={[
'error-monitoring',
'performance',
'profiling',
'session-replay',
'logs'
]}
/>

<OnboardingOption optionId="profiling">

<Alert level="info">
On Android 15 (API level 35) and higher, profiling uses the Android <Link to="https://developer.android.com/reference/android/os/ProfilingManager">ProfilingManager</Link> to capture <Link to="https://perfetto.dev/">Perfetto</Link> traces. On older devices, the SDK automatically falls back to the <PlatformLink to="/profiling/legacy/">legacy profiler</PlatformLink>.
</Alert>

</OnboardingOption>

## Install

Sentry captures data by using an SDK within your application's runtime. These are platform-specific and allow Sentry to have a deep understanding of how your application works.
Expand All @@ -61,35 +37,35 @@ npx @sentry/wizard@latest -i android
```

```bash {tabTitle:macOS (Intel/x64)}
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v5.1.0/sentry-wizard-darwin-x64"
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v9.0.0/sentry-wizard-darwin-x64"
curl -L $downloadUrl -o sentry-wizard
chmod +x sentry-wizard
./sentry-wizard -i android
```

```bash {tabTitle:macOS (Apple Silicon/arm64)}
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v5.1.0/sentry-wizard-darwin-arm64"
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v9.0.0/sentry-wizard-darwin-arm64"
curl -L $downloadUrl -o sentry-wizard
chmod +x sentry-wizard
./sentry-wizard -i android
```

```bash {tabTitle:Linux (x64)}
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v5.1.0/sentry-wizard-linux-x64"
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v9.0.0/sentry-wizard-linux-x64"
curl -L $downloadUrl -o sentry-wizard
chmod +x sentry-wizard
./sentry-wizard -i android
```

```bash {tabTitle:Linux (arm64)}
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v5.1.0/sentry-wizard-linux-arm64"
downloadUrl="https://github.com/getsentry/sentry-wizard/releases/download/v9.0.0/sentry-wizard-linux-arm64"
curl -L $downloadUrl -o sentry-wizard
chmod +x sentry-wizard
./sentry-wizard -i android
```

```powershell {tabTitle:Windows}
$downloadUrl = "https://github.com/getsentry/sentry-wizard/releases/download/v5.1.0/sentry-wizard-win-x64.exe"
$downloadUrl = "https://github.com/getsentry/sentry-wizard/releases/download/v9.0.0/sentry-wizard-win-x64.exe"
Invoke-WebRequest $downloadUrl -OutFile sentry-wizard.exe
./sentry-wizard.exe -i android
```
Expand All @@ -102,90 +78,144 @@ If you prefer, you can also <PlatformLink to="/manual-setup/">set up the SDK man
The wizard will prompt you to log in to Sentry. It'll then automatically do the following steps for you:

- Update your app's `build.gradle` file with the Sentry Gradle plugin and configure it
- Update your `AndroidManifest.xml` with the default Sentry configuration
- Update your `AndroidManifest.xml` with your DSN, tracing, user-interaction tracing, screenshots, view hierarchies, and default PII collection
- Ask whether to enable automatic Logs collection
- Create `sentry.properties` with an auth token to upload proguard mappings (this file is automatically added to `.gitignore`)
- Add an example error to your app's Main Activity to verify your Sentry setup

</Expandable>

The wizard does not enable Profiling or Session Replay. Follow [Configure](#configure) to add them and adjust the generated configuration, including <PlatformLink to="/data-management/data-collected/">data collection</PlatformLink>.

## Configure

Configuration is done via the application `AndroidManifest.xml`. Here's an example config which should get you started:
**Error Monitoring** starts working as soon as you initialize the SDK. You can also send **Logs** and **Application Metrics** after initialization, as shown in [Send Logs](#send-logs) and [Send Application Metrics](#send-application-metrics-).

This guide also enables **Tracing**, **Profiling**, and **Session Replay**. The Android SDK also supports **User Feedback**; see <PlatformLink to="/user-feedback/">Set Up User Feedback</PlatformLink> to add a feedback form to your app.

Use SDK version **8.58.0 or later** for the full configuration below, including the <PlatformLink to="/configuration/options/#dataCollection">Data Collection options</PlatformLink>. Session Replay requires **Android 8.0 (API level 26) or higher**.

The SDK initializes automatically using your application's `AndroidManifest.xml`. If you ran the wizard, update the existing Sentry entries and add the missing options from the snippet below. Keep a single `io.sentry.dsn` entry.

The snippet also turns on:

- Tracing: via <PlatformLink to="/configuration/options/#tracesSampleRate">`tracesSampleRate`</PlatformLink>
- Profiling: via <PlatformLink to="/configuration/options/#profileSessionSampleRate">`profileSessionSampleRate`</PlatformLink> and <PlatformLink to="/configuration/options/#profileLifecycle">`profileLifecycle`</PlatformLink>
- Session Replay: via `sessionSampleRate` and `onErrorSampleRate` in the <PlatformLink to="/session-replay/configuration/#general-configuration">Session Replay configuration</PlatformLink>
- Automatic Logs collection: via `io.sentry.logs.enabled`, described in the <PlatformLink to="/logs/#setup">Logs setup</PlatformLink>

Don't need a feature? Remove its options from your app's manifest. The `trace` profiling lifecycle requires tracing: if you disable tracing, also remove the profiling options or <PlatformLink to="/profiling/#enabling-manual-lifecycle-ui-profiling">configure manual profiling</PlatformLink>.

```xml {filename:AndroidManifest.xml}
<application>
<!-- Required: set your sentry.io project identifier (DSN) -->
<meta-data android:name="io.sentry.dsn" android:value="___PUBLIC_DSN___" />
<!-- Use Data Collection defaults but don't collect automatic user information. -->
<!-- https://docs.sentry.io/platforms/android/data-management/data-collected/ -->
<meta-data android:name="io.sentry.data-collection.user-info" android:value="false" />
<!-- ___PRODUCT_OPTION_START___ performance -->
<!-- Enable the performance API by setting a sample-rate, adjust in production env -->

<!-- Enables Tracing. -->
<!-- Set to 1.0 to capture 100% of transactions. -->
<!-- We recommend reducing this value in production. -->
<meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />
<!-- ___PRODUCT_OPTION_END___ performance -->
<!-- ___PRODUCT_OPTION_START___ profiling -->
<!-- Enable profiling, adjust in production env. This is evaluated only once per session -->

<!-- Enables Profiling while sampled traces are active. -->
<!-- Sampling is evaluated once per session. Reduce this value in production. -->
<meta-data android:name="io.sentry.traces.profiling.session-sample-rate" android:value="1.0" />
<!-- Set profiling lifecycle, can be `manual` (controlled through `Sentry.startProfiler()` and `Sentry.stopProfiler()`) or `trace` (automatically starts and stop a profile whenever a sampled trace starts and finishes) -->
<meta-data android:name="io.sentry.traces.profiling.lifecycle" android:value="trace" />
<!-- ___PRODUCT_OPTION_END___ profiling -->
<!-- ___PRODUCT_OPTION_START___ logs -->
<!-- Enable logs to be sent to Sentry -->
<meta-data android:name="io.sentry.logs.enabled" android:value="true" />
<!-- ___PRODUCT_OPTION_END___ logs -->
<!-- ___PRODUCT_OPTION_START___ session-replay -->
<!-- Record session replays for 100% of errors and 10% of sessions -->

<!-- Captures replays for 10% of sessions and 100% of sessions with errors. -->
<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" />
<!-- ___PRODUCT_OPTION_END___ session-replay -->

<!-- Enables automatic Logs collection through integrations. -->
<meta-data android:name="io.sentry.logs.enabled" android:value="true" />
</application>
```

## Verify
### Configure the Sentry Android Gradle Plugin

The Sentry Wizard configures the Gradle plugin, which supports automatic tracing instrumentation and uploads ProGuard/R8 mappings and source context. For manual setup, see the <PlatformLink to="/configuration/gradle/">Gradle plugin</PlatformLink>, <PlatformLink to="/enhance-errors/proguard/">ProGuard/R8</PlatformLink>, and <PlatformLink to="/enhance-errors/source-context/">source context</PlatformLink> guides.

<Alert level="info">

Starting with SDK version 8.51.0, profiling on Android 15 (API level 35) and higher uses the Android <Link to="https://developer.android.com/reference/android/os/ProfilingManager">ProfilingManager</Link> to capture <Link to="https://perfetto.dev/">Perfetto</Link> traces. On older devices, the SDK automatically falls back to the <PlatformLink to="/profiling/legacy/">legacy profiler</PlatformLink>.

</Alert>

## Send Logs

After SDK initialization, use `Sentry.logger()` to send log messages at different levels.

```java {tabTitle: Java}
import io.sentry.Sentry;

Sentry.logger().info("Order processed");
Sentry.logger().error("Payment failed");
```

```kotlin {tabTitle: Kotlin}
import io.sentry.Sentry

Sentry.logger().info("Order processed")
Sentry.logger().error("Payment failed")
```

Starting with SDK version **8.59.0**, direct `Sentry.logger()` calls work regardless of the `io.sentry.logs.enabled` setting. On earlier SDK versions, set this option to `true` to send logs through this API.

To collect logs automatically through the <PlatformLink to="/integrations/timber/">Timber</PlatformLink> or <PlatformLink to="/integrations/logcat/">Logcat</PlatformLink> integration, keep `io.sentry.logs.enabled` set to `true`. If you <PlatformLink to="/manual-setup/#configuration-via-sentryoptions">initialize the SDK in code</PlatformLink> instead, set `options.logs.isEnabled = true` in Kotlin or `options.getLogs().setEnabled(true)` in Java.

See <PlatformLink to="/logs/">Set Up Logs</PlatformLink> for all available logging methods and configuration options.

## Send Application Metrics <FeatureBadge type="new" />

The Android SDK provides counters, gauges, and distributions through `Sentry.metrics()`. Metrics need no separate enable option.

<PlatformContent includePath="metrics/requirements" />

```java {tabTitle: Java}
import io.sentry.Sentry;

Sentry.metrics().count("button_click", 1.0);
Sentry.metrics().gauge("queue_depth", 42.0);
Sentry.metrics().distribution("response_time", 187.5);
```

```kotlin {tabTitle: Kotlin}
import io.sentry.Sentry

Sentry.metrics().count("button_click", 1.0)
Sentry.metrics().gauge("queue_depth", 42.0)
Sentry.metrics().distribution("response_time", 187.5)
```

See <PlatformLink to="/metrics/">Set Up Metrics</PlatformLink> for all available metric types and configuration options.

## Verify Your Setup

Verify that your app is sending events to Sentry by adding the following snippet, which includes an intentional error. You should see the error reported in Sentry within a few minutes.
Add an intentional error to your application to confirm that Sentry captures it:

```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.lang.Exception;
```java {tabTitle: Java}
import io.sentry.Sentry;

public class MyActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
throw new Exception("This is a test.");
} catch (Exception e) {
Sentry.captureException(e);
}
}
}
Sentry.captureException(new Exception("This is a test."));
```

```kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
```kotlin {tabTitle: Kotlin}
import io.sentry.Sentry

class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
try {
throw Exception("This is a test.")
} catch (e: Exception) {
Sentry.captureException(e)
}
}
}
Sentry.captureException(Exception("This is a test."))
```

### View Captured Data in Sentry

Open your project in Sentry and find the test error in Issues.

## Next Steps

At this point, you should have integrated Sentry into your Android application. Here's what to explore next:

- Explore [practical guides](/get-started/guides/) on what to monitor, log, track, and investigate after setup
- <PlatformLink to="/features/">Learn about the features of Sentry's Android SDK</PlatformLink>
- <PlatformLink to="/enhance-errors/">Learn how to enhance stack traces of your Sentry errors</PlatformLink>
- <PlatformLink to="/enriching-events/">Enrich events with additional context to make debugging simpler</PlatformLink>
- <PlatformLink to="/configuration/app-not-respond/">Diagnose ANRs</PlatformLink> with profiling and automatic fingerprinting
- Monitor <PlatformLink to="/configuration/memory-limiter/">Memory Limiter process deaths</PlatformLink> on supported Android 17+ devices
- <PlatformLink to="/enhance-errors/">Enhance stack traces</PlatformLink> to
make error details more readable
- <PlatformLink to="/user-feedback/">Collect User Feedback</PlatformLink> from
your users
6 changes: 4 additions & 2 deletions platform-includes/logs/setup/android.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
To enable logging integrations, you need to initialize the SDK with the `logs.enabled` option set to `true`. Since SDK version 8.59.0, direct `Sentry.logger()` calls don't require this option.
Starting with SDK version `8.59.0`, direct `Sentry.logger()` calls work after initialization regardless of the `logs.enabled` setting. On earlier SDK versions, set `logs.enabled` to `true` to send logs through this API.

To collect logs automatically through the <PlatformLink to="/integrations/timber/">Timber</PlatformLink> or <PlatformLink to="/integrations/logcat/">Logcat</PlatformLink> integration, enable `logs.enabled` in the manifest or when you <PlatformLink to="/manual-setup/#configuration-via-sentryoptions">initialize the SDK in code</PlatformLink>:

```XML {filename:AndroidManifest.xml}
<meta-data android:name="io.sentry.logs.enabled" android:value="true" />
Expand Down Expand Up @@ -31,7 +33,7 @@ class MyApplication : Application() {
super.onCreate()
SentryAndroid.init(this) { options ->
options.dsn = "___PUBLIC_DSN___"
options.logs.enabled = true
options.logs.isEnabled = true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion platform-includes/logs/usage/android.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `Sentry.logger()` APIs.
After SDK initialization, send logs using the `Sentry.logger()` APIs. On SDK versions earlier than `8.59.0`, first enable `logs.enabled` as described in [Setup](#setup).

The `Sentry.logger()` namespace exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warn`, `error`, and `fatal`.

Expand Down
2 changes: 1 addition & 1 deletion platform-includes/metrics/requirements/android.mdx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Metrics are supported in Sentry Android SDK version `8.34.0` and above.
Metrics are supported in Sentry Android SDK version `8.30.0` and above.
Loading