# Set up a mobile app

Send errors from an Android, iOS, macOS, React Native or Expo app with Sentry's own SDK, and make its release stack traces readable.

Sentry's mobile SDKs send to App Monitoring unmodified: only the DSN, and for uploads the token,
point here. Create a project with the **Platform** of your app (**Android**, **iOS**, **macOS** or
**React Native**, see [Create a project](create-a-project.md)); its **Setup** tab shows the snippets
below with your DSN, organization and project filled in.

## What you see in a release build

| App                            | Stack traces of a release build                                   | What makes them readable                                         |
| ------------------------------ | ----------------------------------------------------------------- | ---------------------------------------------------------------- |
| Android, Java or Kotlin code   | Class and method names as R8 renamed them (`a.b.c`)               | The build's ProGuard mapping file, uploaded by the Gradle plugin |
| React Native and Expo, JS code | Positions in `index.android.bundle` or `main.jsbundle`            | The bundle's source map, uploaded by the build                   |
| iOS and macOS, Swift and Obj-C | The binary and the address of each frame (`MyApp 0x0000000100a4`) | Not supported yet: debug symbols (dSYMs) are accepted, not used  |
| Native code (NDK, C and C++)   | The binary and the address                                        | Not supported yet                                                |

Mapping files and source maps are applied when an event arrives: upload them with the build,
before the app reaches users. A frame that is shown as it was sent is marked on the issue page
(**no mapping file**, **no source map** or **not symbolicated**).

## Android

1. In `app/build.gradle.kts`, add the Sentry Android Gradle plugin and point it at App Monitoring.
   The plugin adds the SDK to the app and uploads the mapping file of every release build.

   ```kotlin
   plugins {
       id("com.android.application")
       id("io.sentry.android.gradle") version "6.27.0"
   }

   sentry {
       org.set("my-org")
       projectName.set("my-app")
       url.set("https://errors.example.com/")
       authToken.set(System.getenv("SENTRY_AUTH_TOKEN"))
       includeProguardMapping.set(true)
       autoUploadProguardMapping.set(true)
       telemetry.set(false)
   }
   ```

2. In `AndroidManifest.xml`, inside `<application>`, set the DSN:

   ```xml
   <meta-data
       android:name="io.sentry.dsn"
       android:value="https://public-key@errors.example.com/42" />
   ```

3. Set `SENTRY_AUTH_TOKEN` where the release build runs (see
   [Upload ProGuard mapping files](upload-proguard-mappings.md)).

The app's own classes are marked in-app, and Android, AndroidX, Kotlin and Sentry classes are not.

## iOS and macOS

1. Add `https://github.com/getsentry/sentry-cocoa` with Swift Package Manager, or `pod 'Sentry'`
   to the Podfile.
2. Start the SDK at launch, in your `App` initializer or in
   `application(_:didFinishLaunchingWithOptions:)`:

   ```swift
   import Sentry

   SentrySDK.start { options in
       options.dsn = "https://public-key@errors.example.com/42"
   }
   ```

3. (Optional) In a macOS app built with AppKit, report uncaught exceptions as crashes:

   ```swift
   UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions": true])
   ```

A crash's stack is read from the crashed thread. Frames are grouped by binary and function name,
never by address, so the same crash from two launches or two builds is one issue. An upload of
debug symbols (`sentry-cli debug-files upload`, the Xcode build phase, Fastlane) succeeds, and
the files are not kept.

## React Native

1. Install the SDK: `npm install @sentry/react-native`, then `pod install` in `apps/ios/`.
2. In `App.tsx`, initialize the SDK and wrap the root component:

   ```ts
   import * as Sentry from '@sentry/react-native';

   Sentry.init({ dsn: 'https://public-key@errors.example.com/42' });

   export default Sentry.wrap(App);
   ```

3. In `metro.config.js`, add the Sentry serializer, which writes the debug IDs that tie each
   bundle to its source map:

   ```js
   const { getDefaultConfig } = require('@react-native/metro-config');
   const { withSentryConfig } = require('@sentry/react-native/metro');

   module.exports = withSentryConfig(getDefaultConfig(__dirname));
   ```

4. Write `android/sentry.properties` and `ios/sentry.properties`:

   ```ini
   defaults.url=https://errors.example.com/
   defaults.org=my-org
   defaults.project=my-app
   ```

5. Upload the source maps of every release build: apply `sentry.gradle` in
   `android/app/build.gradle`, and run `sentry-xcode.sh` in Xcode's **Bundle React Native code and
   images** build phase. The **Setup** tab shows both lines.
6. Set `SENTRY_AUTH_TOKEN` where the release build runs (see
   [Upload source maps](upload-source-maps.md)).

Source maps of Hermes builds are applied too. The function name comes from your source; the
build machine's project directory is cut from the file name, so builds on two machines group
alike.

## Expo

1. Install the SDK: `npx expo install @sentry/react-native`.
2. Add the config plugin to `app.json`, pointed at App Monitoring:

   ```json
   {
     "expo": {
       "plugins": [
         [
           "@sentry/react-native/expo",
           {
             "url": "https://errors.example.com/",
             "organization": "my-org",
             "project": "my-app"
           }
         ]
       ]
     }
   }
   ```

3. In `metro.config.js`, use `getSentryExpoConfig`:

   ```js
   const { getSentryExpoConfig } = require('@sentry/react-native/metro');

   module.exports = getSentryExpoConfig(__dirname);
   ```

4. Initialize the SDK and wrap the root layout, as for React Native.
5. Store `SENTRY_AUTH_TOKEN` as a secret environment variable of the EAS project, never in
   `app.json`. EAS builds then upload their source maps.

## Related

- [Upload ProGuard mapping files](upload-proguard-mappings.md)
- [Upload source maps](upload-source-maps.md)
- [Issues and grouping](issues-and-grouping.md)
- [What App Monitoring does not support](not-supported.md)
