From 4d4859efc931906f167752c2b4c00ec49cf6b970 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sat, 28 Nov 2026 11:28:31 +0100 Subject: [PATCH 1/2] feat(android): Add opt-in out-of-process native crash backend Setting `SENTRY_NATIVE_BACKEND=native` as a Gradle property or an environment variable replaces the inproc `sentry-native-ndk` with `sentry-android-ndk-outproc`. Its crash service sends native crashes at crash time instead of on the next launch. CI builds an extra production sample APK with the switch and runs a Maestro flow that crashes in native code without relaunching the app, so only the crash service can send the `SIGSEGV` event. The mocked Sentry server now gunzips only gzip-encoded requests, because the crash daemon sends envelopes uncompressed. --- .github/workflows/sample-application.yml | 23 +++++-- CHANGELOG.md | 7 +++ packages/core/android/build.gradle | 12 +++- .../e2e/jest.config.android.native.js | 8 +++ .../captureNativeCrash.test.android.native.ts | 63 +++++++++++++++++++ ...captureNativeCrash.test.android.native.yml | 21 +++++++ .../e2e/utils/mockedSentryServer.ts | 21 +++++-- samples/react-native/package.json | 1 + .../scripts/test-android-native.sh | 27 ++++++++ 9 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 samples/react-native/e2e/jest.config.android.native.js create mode 100644 samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.ts create mode 100644 samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.yml create mode 100755 samples/react-native/scripts/test-android-native.sh diff --git a/.github/workflows/sample-application.yml b/.github/workflows/sample-application.yml index d3b0e102ab..b9ad40e190 100644 --- a/.github/workflows/sample-application.yml +++ b/.github/workflows/sample-application.yml @@ -140,7 +140,7 @@ jobs: path: ${{ env.REACT_NATIVE_SAMPLE_PATH }}/ios/*.log build-android: - name: Build ${{ matrix.rn-architecture }} android ${{ matrix.build-type }} + name: Build ${{ matrix.rn-architecture }} android ${{ matrix.build-type }} ${{ matrix.native-backend }} runs-on: ubuntu-latest needs: [diff_check, detect-changes] if: >- @@ -156,6 +156,7 @@ jobs: matrix: rn-architecture: ["new"] build-type: ["dev", "production"] + native-backend: ["inproc", "native"] steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -185,6 +186,8 @@ jobs: - name: Build Android App working-directory: ${{ env.REACT_NATIVE_SAMPLE_PATH }} + env: + SENTRY_NATIVE_BACKEND: ${{ matrix.native-backend }} run: | export RN_ARCHITECTURE="${{ matrix.rn-architecture }}" [[ "${{ matrix.build-type }}" == "production" ]] && export CONFIG='release' || export CONFIG='debug' @@ -227,7 +230,7 @@ jobs: if: ${{ matrix.rn-architecture == 'new' && matrix.build-type == 'production' }} uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 with: - name: sample-rn-${{ matrix.rn-architecture }}-${{ matrix.build-type }}-android + name: sample-rn-${{ matrix.rn-architecture }}-${{ matrix.build-type }}-${{ matrix.native-backend }}-android path: ${{ env.ANDROID_APP_ARCHIVE_PATH }} retention-days: 1 @@ -235,7 +238,7 @@ jobs: if: ${{ always() }} uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 with: - name: build-sample-${{ matrix.rn-architecture }}-android-${{ matrix.build-type }}-no-frameworks-logs + name: build-sample-${{ matrix.rn-architecture }}-android-${{ matrix.build-type }}-${{ matrix.native-backend }}-no-frameworks-logs path: ${{ env.REACT_NATIVE_SAMPLE_PATH }}/android/*.log build-macos: @@ -666,7 +669,7 @@ jobs: run: yarn test-ios test-android: - name: Test android production REV2 + name: Test android production ${{ matrix.native-backend }} REV2 runs-on: ubuntu-latest needs: [diff_check, detect-changes, build-android] if: >- @@ -675,6 +678,14 @@ jobs: && needs.detect-changes.outputs.needs_sample_react_native == 'true' && (needs.detect-changes.outputs.sample_react_native == 'true' || needs.detect-changes.outputs.needs_android == 'true') }} + strategy: + fail-fast: false + matrix: + include: + - native-backend: "inproc" + test-script: "test-android" + - native-backend: "native" + test-script: "test-android-native" steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -686,7 +697,7 @@ jobs: - name: Download Android APK uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8 with: - name: sample-rn-new-production-android + name: sample-rn-new-production-${{ matrix.native-backend }}-android path: ${{ env.REACT_NATIVE_SAMPLE_PATH }} - name: Unzip Android APK @@ -740,4 +751,4 @@ jobs: -camera-front none -timezone US/Pacific working-directory: ${{ env.REACT_NATIVE_SAMPLE_PATH }} - script: yarn test-android + script: yarn ${{ matrix.test-script }} diff --git a/CHANGELOG.md b/CHANGELOG.md index c5be7c6e57..5ae72a61e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ > make sure you follow our [migration guide](https://docs.sentry.io/platforms/react-native/migration/) first. +## Unreleased + +### Features + +- Add experimental, opt-in out-of-process crash reporting on Android + - To enable it, set `SENTRY_NATIVE_BACKEND=native` in `android/gradle.properties` or in the build environment. A crash service then sends native crashes at crash time, instead of on the next launch. + ## 8.37.0 ### Features diff --git a/packages/core/android/build.gradle b/packages/core/android/build.gradle index c6c3bc8b3d..9766103003 100644 --- a/packages/core/android/build.gradle +++ b/packages/core/android/build.gradle @@ -186,11 +186,21 @@ android { } def sentryAndroidVersion = '8.66.0' +def sentryNativeBackend = project.findProperty('SENTRY_NATIVE_BACKEND') + ?: providers.environmentVariable('SENTRY_NATIVE_BACKEND').getOrNull() dependencies { compileOnly files('libs/replay-stubs.jar') implementation 'com.facebook.react:react-native:+' - api "io.sentry:sentry-android:$sentryAndroidVersion" + if (sentryNativeBackend == 'native') { + // Both backends provide `libsentry.so`, so the out-of-process one replaces the inproc one. + api("io.sentry:sentry-android:$sentryAndroidVersion") { + exclude group: 'io.sentry', module: 'sentry-native-ndk' + } + api "io.sentry:sentry-android-ndk-outproc:$sentryAndroidVersion" + } else { + api "io.sentry:sentry-android:$sentryAndroidVersion" + } debugImplementation "io.sentry:sentry-spotlight:$sentryAndroidVersion" testImplementation 'junit:junit:4.13.2' diff --git a/samples/react-native/e2e/jest.config.android.native.js b/samples/react-native/e2e/jest.config.android.native.js new file mode 100644 index 0000000000..cd890cf597 --- /dev/null +++ b/samples/react-native/e2e/jest.config.android.native.js @@ -0,0 +1,8 @@ +const baseConfig = require('./jest.config.base'); + +/** @type {import('@jest/types').Config.InitialOptions} */ +module.exports = { + ...baseConfig, + globals: { E2E_PLATFORM: 'android' }, + testMatch: ['/e2e/**/*.test.android.native.ts'], +}; diff --git a/samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.ts b/samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.ts new file mode 100644 index 0000000000..21c988b24d --- /dev/null +++ b/samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.ts @@ -0,0 +1,63 @@ +import { describe, it, beforeAll, expect, afterAll } from '@jest/globals'; +import { Envelope, EventItem } from '@sentry/core'; + +import { + createSentryServer, + containingEventWithExceptionType, +} from '../../utils/mockedSentryServer'; +import { getItemOfTypeFrom } from '../../utils/event'; +import { maestro } from '../../utils/maestro'; + +describe('Capture native crash at crash time (Android native backend)', () => { + let sentryServer = createSentryServer(); + + let envelope: Envelope; + + beforeAll(async () => { + await sentryServer.start(); + + const envelopePromise = sentryServer.waitForEnvelope( + containingEventWithExceptionType('SIGSEGV'), + ); + + // The flow never relaunches the app, so only the crash service can send + // the crash. + await maestro( + 'tests/captureNativeCrash/captureNativeCrash.test.android.native.yml', + ); + + envelope = await envelopePromise; + }, 240000); // 240 seconds timeout + + afterAll(async () => { + await sentryServer.close(); + }); + + it('envelope contains fatal native crash event', async () => { + const item = getItemOfTypeFrom(envelope, 'event'); + + expect(item).toEqual([ + expect.objectContaining({ + type: 'event', + }), + expect.objectContaining({ + level: 'fatal', + platform: 'native', + sdk: expect.objectContaining({ + name: 'sentry.native.android.react-native', + }), + exception: { + values: [ + expect.objectContaining({ + type: 'SIGSEGV', + mechanism: expect.objectContaining({ + type: 'signalhandler', + handled: false, + }), + }), + ], + }, + }), + ]); + }); +}); diff --git a/samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.yml b/samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.yml new file mode 100644 index 0000000000..ca7ed14647 --- /dev/null +++ b/samples/react-native/e2e/tests/captureNativeCrash/captureNativeCrash.test.android.native.yml @@ -0,0 +1,21 @@ +appId: io.sentry.reactnative.sample +--- +- launchApp: + clearState: true + stopApp: true + +# App launches on ErrorsTab (first tab) +# Wait a moment for the screen to fully load +- waitForAnimationToEnd: + timeout: 2000 + +- scrollUntilVisible: + element: "Crash in Android Cpp" + timeout: 10000 + direction: DOWN +- tapOn: "Crash in Android Cpp" + +# Don't relaunch: the crash service must send the crash on its own +- extendedWaitUntil: + notVisible: "Crash in Android Cpp" + timeout: 15000 diff --git a/samples/react-native/e2e/utils/mockedSentryServer.ts b/samples/react-native/e2e/utils/mockedSentryServer.ts index 14c3631a08..626c0dc9a9 100644 --- a/samples/react-native/e2e/utils/mockedSentryServer.ts +++ b/samples/react-native/e2e/utils/mockedSentryServer.ts @@ -32,14 +32,17 @@ export function createSentryServer({ port = 8961 } = {}): { const server = createServer((req: IncomingMessage, res: ServerResponse) => { let body: Buffer = Buffer.from([]); - const gunzip = createGunzip(); - req.pipe(gunzip); + // The native crash daemon sends uncompressed envelopes. + const stream = + req.headers['content-encoding'] === 'gzip' + ? req.pipe(createGunzip()) + : req; - gunzip.on('data', (chunk: Buffer) => { + stream.on('data', (chunk: Buffer) => { body = Buffer.concat([body, chunk]); }); - gunzip.on('end', () => { + stream.on('end', () => { const request = { path: req.url, headers: req.headers, @@ -133,6 +136,16 @@ export function containingEventWithMessage(message: string) { ); } +export function containingEventWithExceptionType(type: string) { + return (envelope: Envelope) => + envelope[1].some( + item => + itemHeaderIsType(item[0], 'event') && + itemBodyIsEvent(item[1]) && + !!item[1].exception?.values?.some(value => value.type === type), + ); +} + export function containingTransaction(envelope: Envelope) { return envelope[1].some(item => itemHeaderIsType(item[0], 'transaction')); } diff --git a/samples/react-native/package.json b/samples/react-native/package.json index 08a88eadf6..2dcb2ea33f 100644 --- a/samples/react-native/package.json +++ b/samples/react-native/package.json @@ -21,6 +21,7 @@ "set-test-dsn-ios": "scripts/set-dsn-ios.mjs", "test-android-manual": "scripts/test-android-manual.sh", "test-android-auto": "scripts/test-android-auto.sh", + "test-android-native": "scripts/test-android-native.sh", "test-ios-manual": "scripts/test-ios-manual.sh", "test-ios-auto": "scripts/test-ios-auto.sh", "lint": "npx oxlint", diff --git a/samples/react-native/scripts/test-android-native.sh b/samples/react-native/scripts/test-android-native.sh new file mode 100755 index 0000000000..16f8e06a70 --- /dev/null +++ b/samples/react-native/scripts/test-android-native.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +set -e # exit on error + +# Get current directory +thisFileDirPath=$(dirname "$0") +reactProjectRootPath="$(cd "$thisFileDirPath/.." && pwd)" + +maybeApkPath=$(find "${reactProjectRootPath}" -maxdepth 1 -name "*.apk") + +# Check if any APK files exist +apk_count=$(echo "$maybeApkPath" | wc -l) + +if [ -n "$maybeApkPath" ] && [ $apk_count -eq 1 ]; then + # Force install single APK using adb + apk_file="${maybeApkPath}" + echo "Installing $apk_file..." + adb install -r "$apk_file" +elif [ $apk_count -gt 1 ]; then + echo "Error: Multiple APK files found. Expected only one APK file." + exit 1 +else + echo "No APK files found, continuing without install" +fi + +# Run the tests +npx jest --config e2e/jest.config.android.native.js From 89ff7feceaaea25d2f729dc7f69224a3ac351e82 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 18:03:17 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ae72a61e6..65e0b02df3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ ### Features -- Add experimental, opt-in out-of-process crash reporting on Android +- Add experimental, opt-in out-of-process crash reporting on Android ([#7152](https://github.com/getsentry/sentry-react-native/pull/7152)) - To enable it, set `SENTRY_NATIVE_BACKEND=native` in `android/gradle.properties` or in the build environment. A crash service then sends native crashes at crash time, instead of on the next launch. ## 8.37.0