From 470c19c44d54398e8e5fea05c17a9911ccd6f1e8 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich Date: Mon, 10 Feb 2025 18:06:55 +0100 Subject: [PATCH] misc(sample): Change RN Sample to use native file init by default --- samples/react-native/android/app/build.gradle | 1 + .../reactnative/sample/MainApplication.kt | 17 +++-------- .../react-native/android/gradle.properties | 5 ++++ .../sentryreactnativesample.xcscheme | 6 ++++ .../sentryreactnativesample/AppDelegate.mm | 11 +++++-- samples/react-native/package.json | 2 ++ samples/react-native/src/App.tsx | 8 +++-- samples/react-native/src/utils.ts | 30 +++++++++++++++++++ yarn.lock | 19 ++++++++++++ 9 files changed, 81 insertions(+), 18 deletions(-) diff --git a/samples/react-native/android/app/build.gradle b/samples/react-native/android/app/build.gradle index 2d24b2ede1..6c0d4a7ca1 100644 --- a/samples/react-native/android/app/build.gradle +++ b/samples/react-native/android/app/build.gradle @@ -138,6 +138,7 @@ android { targetSdkVersion rootProject.ext.targetSdkVersion versionCode 39 versionName "6.7.0-alpha.0" + buildConfigField "boolean", "SENTRY_DISABLE_NATIVE_START", System.getenv('SENTRY_DISABLE_NATIVE_START') ?: String.valueOf(sentryDisableNativeStart) } signingConfigs { diff --git a/samples/react-native/android/app/src/main/java/io/sentry/reactnative/sample/MainApplication.kt b/samples/react-native/android/app/src/main/java/io/sentry/reactnative/sample/MainApplication.kt index 6546ca8b10..5b5ac47444 100644 --- a/samples/react-native/android/app/src/main/java/io/sentry/reactnative/sample/MainApplication.kt +++ b/samples/react-native/android/app/src/main/java/io/sentry/reactnative/sample/MainApplication.kt @@ -37,23 +37,14 @@ class MainApplication : override fun onCreate() { super.onCreate() - // When the native init is enabled the `autoInitializeNativeSdk` - // in JS has to be set to `false` - // this.initializeSentry() + if (!BuildConfig.SENTRY_DISABLE_NATIVE_START) { + RNSentrySDK.init(this) + } + SoLoader.init(this, OpenSourceMergedSoMapping) if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { // If you opted-in for the New Architecture, we load the native entry point for this app. load() } } - - private fun initializeSentry() { - RNSentrySDK.init(this) { options -> - // Options set here will apply to the Android SDK overriding the ones from `sentry.options.json` - options.dsn = "https://1df17bd4e543fdb31351dee1768bb679@o447951.ingest.sentry.io/5428561" - options.isDebug = true - } - - // RNSentrySDK.init(this) - } } diff --git a/samples/react-native/android/gradle.properties b/samples/react-native/android/gradle.properties index 600fea4b77..d71a974450 100644 --- a/samples/react-native/android/gradle.properties +++ b/samples/react-native/android/gradle.properties @@ -38,3 +38,8 @@ newArchEnabled=true # Use this property to enable or disable the Hermes JS engine. # If set to false, you will be using JSC instead. hermesEnabled=true + +# Only implemented in this sample project. +# It's used for testing the native SDK auto-start feature. +# true means manual native start is disabled and JS auto initializes native SDK. +sentryDisableNativeStart=false diff --git a/samples/react-native/ios/sentryreactnativesample.xcodeproj/xcshareddata/xcschemes/sentryreactnativesample.xcscheme b/samples/react-native/ios/sentryreactnativesample.xcodeproj/xcshareddata/xcschemes/sentryreactnativesample.xcscheme index 15d942042b..61b12d2c2c 100644 --- a/samples/react-native/ios/sentryreactnativesample.xcodeproj/xcshareddata/xcschemes/sentryreactnativesample.xcscheme +++ b/samples/react-native/ios/sentryreactnativesample.xcodeproj/xcshareddata/xcschemes/sentryreactnativesample.xcscheme @@ -60,6 +60,12 @@ ReferencedContainer = "container:sentryreactnativesample.xcodeproj"> + + + + * arguments = [[NSProcessInfo processInfo] arguments]; + return ![arguments containsObject:@"--sentry-disable-native-start"]; +} + @end diff --git a/samples/react-native/package.json b/samples/react-native/package.json index 143a4b6c7f..2cdee52a1d 100644 --- a/samples/react-native/package.json +++ b/samples/react-native/package.json @@ -29,7 +29,9 @@ "delay": "^6.0.0", "react": "18.3.1", "react-native": "0.76.3", + "react-native-build-config": "^0.3.2", "react-native-gesture-handler": "^2.21.1", + "react-native-launch-arguments": "^4.0.4", "react-native-reanimated": "3.16.1", "react-native-safe-area-context": "4.14.0", "react-native-screens": "4.1.0", diff --git a/samples/react-native/src/App.tsx b/samples/react-native/src/App.tsx index f6d1063736..d84f1b9470 100644 --- a/samples/react-native/src/App.tsx +++ b/samples/react-native/src/App.tsx @@ -31,12 +31,16 @@ import GesturesTracingScreen from './Screens/GesturesTracingScreen'; import { LogBox, Platform, StyleSheet, View } from 'react-native'; import Ionicons from 'react-native-vector-icons/Ionicons'; import PlaygroundScreen from './Screens/PlaygroundScreen'; -import { logWithoutTracing } from './utils'; +import { clearSentryOptionsFromFile, logWithoutTracing, shouldUseAutoStart } from './utils'; import { ErrorEvent } from '@sentry/core'; import HeavyNavigationScreen from './Screens/HeavyNavigationScreen'; import WebviewScreen from './Screens/WebviewScreen'; import { isTurboModuleEnabled } from '@sentry/react-native/dist/js/utils/environment'; +if (shouldUseAutoStart()) { + clearSentryOptionsFromFile(); +} + if (typeof setImmediate === 'undefined') { require('setimmediate'); } @@ -130,7 +134,7 @@ Sentry.init({ spotlight: true, // This should be disabled when manually initializing the native SDK // Note that options from JS are not passed to the native SDKs when initialized manually - autoInitializeNativeSdk: true, + // autoInitializeNativeSdk: true, }); const Stack = isMobileOs diff --git a/samples/react-native/src/utils.ts b/samples/react-native/src/utils.ts index 8681333e30..33ccfdf084 100644 --- a/samples/react-native/src/utils.ts +++ b/samples/react-native/src/utils.ts @@ -1,3 +1,7 @@ +import { LaunchArguments } from 'react-native-launch-arguments'; +import BuildConfig from 'react-native-build-config'; +import { Platform } from 'react-native'; + export function logWithoutTracing(...args: unknown[]) { if ('__sentry_original__' in console.log) { console.log.__sentry_original__(...args); @@ -5,3 +9,29 @@ export function logWithoutTracing(...args: unknown[]) { console.log(...args); } } + +export function shouldUseAutoStart(): boolean { + if (Platform.OS === 'android') { + return !!( + BuildConfig as { + SENTRY_DISABLE_NATIVE_START?: boolean; + } + ).SENTRY_DISABLE_NATIVE_START; + } else if (Platform.OS === 'ios') { + const args = LaunchArguments.value<{ + sentrydisablenativestart?: boolean; + }>(); + return !!args.sentrydisablenativestart; + } else { + return false; + } +} + +export function clearSentryOptionsFromFile() { + ( + globalThis as { + __SENTRY_OPTIONS__?: Record; + } + ).__SENTRY_OPTIONS__ = undefined; + logWithoutTracing('Sentry options from file cleared.'); +} diff --git a/yarn.lock b/yarn.lock index 64b67a8bb0..ddc9c22aa9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22463,6 +22463,13 @@ __metadata: languageName: node linkType: hard +"react-native-build-config@npm:^0.3.2": + version: 0.3.2 + resolution: "react-native-build-config@npm:0.3.2" + checksum: d2095580be7e6662c968bce5d64cc6524b7f34380999756fb9d4ad24a34e3566b203f93e9bc63bd1e0213d28e1e76dae0c0f39b0b65c55528a876e96dd6c5810 + languageName: node + linkType: hard + "react-native-codegen@npm:^0.70.7": version: 0.70.7 resolution: "react-native-codegen@npm:0.70.7" @@ -22561,6 +22568,16 @@ __metadata: languageName: node linkType: hard +"react-native-launch-arguments@npm:^4.0.4": + version: 4.0.4 + resolution: "react-native-launch-arguments@npm:4.0.4" + peerDependencies: + react: ">=16.8.1" + react-native: ">=0.60.0-rc.0 <1.0.x" + checksum: 7346af606cedc35c58bdccabd88a8ef9b2b55138accf490fe8291c6d7110679f9125af072eaaf896909554cd54be20e863c987d1ce91c39a2f401a999c7fde9f + languageName: node + linkType: hard + "react-native-macos@npm:0.73.34": version: 0.73.34 resolution: "react-native-macos@npm:0.73.34" @@ -24283,7 +24300,9 @@ __metadata: prettier: 2.8.8 react: 18.3.1 react-native: 0.76.3 + react-native-build-config: ^0.3.2 react-native-gesture-handler: ^2.21.1 + react-native-launch-arguments: ^4.0.4 react-native-reanimated: 3.16.1 react-native-safe-area-context: 4.14.0 react-native-screens: 4.1.0