Skip to content

Commit b947d7f

Browse files
misc(sample): Change RN Sample to use native file init by default (#4522)
1 parent dbdd4b5 commit b947d7f

File tree

9 files changed

+81
-18
lines changed

9 files changed

+81
-18
lines changed

samples/react-native/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ android {
138138
targetSdkVersion rootProject.ext.targetSdkVersion
139139
versionCode 39
140140
versionName "6.7.0-alpha.0"
141+
buildConfigField "boolean", "SENTRY_DISABLE_NATIVE_START", System.getenv('SENTRY_DISABLE_NATIVE_START') ?: String.valueOf(sentryDisableNativeStart)
141142
}
142143

143144
signingConfigs {

samples/react-native/android/app/src/main/java/io/sentry/reactnative/sample/MainApplication.kt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,14 @@ class MainApplication :
3737

3838
override fun onCreate() {
3939
super.onCreate()
40-
// When the native init is enabled the `autoInitializeNativeSdk`
41-
// in JS has to be set to `false`
42-
// this.initializeSentry()
40+
if (!BuildConfig.SENTRY_DISABLE_NATIVE_START) {
41+
RNSentrySDK.init(this)
42+
}
43+
4344
SoLoader.init(this, OpenSourceMergedSoMapping)
4445
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
4546
// If you opted-in for the New Architecture, we load the native entry point for this app.
4647
load()
4748
}
4849
}
49-
50-
private fun initializeSentry() {
51-
RNSentrySDK.init(this) { options ->
52-
// Options set here will apply to the Android SDK overriding the ones from `sentry.options.json`
53-
options.dsn = "https://[email protected]/5428561"
54-
options.isDebug = true
55-
}
56-
57-
// RNSentrySDK.init(this)
58-
}
5950
}

samples/react-native/android/gradle.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ newArchEnabled=true
3838
# Use this property to enable or disable the Hermes JS engine.
3939
# If set to false, you will be using JSC instead.
4040
hermesEnabled=true
41+
42+
# Only implemented in this sample project.
43+
# It's used for testing the native SDK auto-start feature.
44+
# true means manual native start is disabled and JS auto initializes native SDK.
45+
sentryDisableNativeStart=false

samples/react-native/ios/sentryreactnativesample.xcodeproj/xcshareddata/xcschemes/sentryreactnativesample.xcscheme

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
ReferencedContainer = "container:sentryreactnativesample.xcodeproj">
6161
</BuildableReference>
6262
</BuildableProductRunnable>
63+
<CommandLineArguments>
64+
<CommandLineArgument
65+
argument = "--sentry-disable-native-start"
66+
isEnabled = "NO">
67+
</CommandLineArgument>
68+
</CommandLineArguments>
6369
</LaunchAction>
6470
<ProfileAction
6571
buildConfiguration = "Release"

samples/react-native/ios/sentryreactnativesample/AppDelegate.mm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ @implementation AppDelegate
2323
- (BOOL)application:(UIApplication *)application
2424
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2525
{
26-
// When the native init is enabled the `autoInitializeNativeSdk`
27-
// in JS has to be set to `false`
28-
// [RNSentrySDK start];
26+
if ([self shouldStartSentry]) {
27+
[RNSentrySDK start];
28+
}
2929

3030
self.moduleName = @"sentry-react-native-sample";
3131
// You can add your custom initial props in the dictionary below.
@@ -73,4 +73,9 @@ - (BOOL)concurrentRootEnabled
7373
return nullptr;
7474
}
7575

76+
- (BOOL) shouldStartSentry {
77+
NSArray<NSString*>* arguments = [[NSProcessInfo processInfo] arguments];
78+
return ![arguments containsObject:@"--sentry-disable-native-start"];
79+
}
80+
7681
@end

samples/react-native/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
"delay": "^6.0.0",
3030
"react": "18.3.1",
3131
"react-native": "0.76.3",
32+
"react-native-build-config": "^0.3.2",
3233
"react-native-gesture-handler": "^2.21.1",
34+
"react-native-launch-arguments": "^4.0.4",
3335
"react-native-reanimated": "3.16.1",
3436
"react-native-safe-area-context": "4.14.0",
3537
"react-native-screens": "4.1.0",

samples/react-native/src/App.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ import GesturesTracingScreen from './Screens/GesturesTracingScreen';
3131
import { LogBox, Platform, StyleSheet, View } from 'react-native';
3232
import Ionicons from 'react-native-vector-icons/Ionicons';
3333
import PlaygroundScreen from './Screens/PlaygroundScreen';
34-
import { logWithoutTracing } from './utils';
34+
import { clearSentryOptionsFromFile, logWithoutTracing, shouldUseAutoStart } from './utils';
3535
import { ErrorEvent } from '@sentry/core';
3636
import HeavyNavigationScreen from './Screens/HeavyNavigationScreen';
3737
import WebviewScreen from './Screens/WebviewScreen';
3838
import { isTurboModuleEnabled } from '@sentry/react-native/dist/js/utils/environment';
3939

40+
if (shouldUseAutoStart()) {
41+
clearSentryOptionsFromFile();
42+
}
43+
4044
if (typeof setImmediate === 'undefined') {
4145
require('setimmediate');
4246
}
@@ -130,7 +134,7 @@ Sentry.init({
130134
spotlight: true,
131135
// This should be disabled when manually initializing the native SDK
132136
// Note that options from JS are not passed to the native SDKs when initialized manually
133-
autoInitializeNativeSdk: true,
137+
// autoInitializeNativeSdk: true,
134138
});
135139

136140
const Stack = isMobileOs

samples/react-native/src/utils.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1+
import { LaunchArguments } from 'react-native-launch-arguments';
2+
import BuildConfig from 'react-native-build-config';
3+
import { Platform } from 'react-native';
4+
15
export function logWithoutTracing(...args: unknown[]) {
26
if ('__sentry_original__' in console.log) {
37
console.log.__sentry_original__(...args);
48
} else {
59
console.log(...args);
610
}
711
}
12+
13+
export function shouldUseAutoStart(): boolean {
14+
if (Platform.OS === 'android') {
15+
return !!(
16+
BuildConfig as {
17+
SENTRY_DISABLE_NATIVE_START?: boolean;
18+
}
19+
).SENTRY_DISABLE_NATIVE_START;
20+
} else if (Platform.OS === 'ios') {
21+
const args = LaunchArguments.value<{
22+
sentrydisablenativestart?: boolean;
23+
}>();
24+
return !!args.sentrydisablenativestart;
25+
} else {
26+
return false;
27+
}
28+
}
29+
30+
export function clearSentryOptionsFromFile() {
31+
(
32+
globalThis as {
33+
__SENTRY_OPTIONS__?: Record<string, unknown>;
34+
}
35+
).__SENTRY_OPTIONS__ = undefined;
36+
logWithoutTracing('Sentry options from file cleared.');
37+
}

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22463,6 +22463,13 @@ __metadata:
2246322463
languageName: node
2246422464
linkType: hard
2246522465

22466+
"react-native-build-config@npm:^0.3.2":
22467+
version: 0.3.2
22468+
resolution: "react-native-build-config@npm:0.3.2"
22469+
checksum: d2095580be7e6662c968bce5d64cc6524b7f34380999756fb9d4ad24a34e3566b203f93e9bc63bd1e0213d28e1e76dae0c0f39b0b65c55528a876e96dd6c5810
22470+
languageName: node
22471+
linkType: hard
22472+
2246622473
"react-native-codegen@npm:^0.70.7":
2246722474
version: 0.70.7
2246822475
resolution: "react-native-codegen@npm:0.70.7"
@@ -22561,6 +22568,16 @@ __metadata:
2256122568
languageName: node
2256222569
linkType: hard
2256322570

22571+
"react-native-launch-arguments@npm:^4.0.4":
22572+
version: 4.0.4
22573+
resolution: "react-native-launch-arguments@npm:4.0.4"
22574+
peerDependencies:
22575+
react: ">=16.8.1"
22576+
react-native: ">=0.60.0-rc.0 <1.0.x"
22577+
checksum: 7346af606cedc35c58bdccabd88a8ef9b2b55138accf490fe8291c6d7110679f9125af072eaaf896909554cd54be20e863c987d1ce91c39a2f401a999c7fde9f
22578+
languageName: node
22579+
linkType: hard
22580+
2256422581
"react-native-macos@npm:0.73.34":
2256522582
version: 0.73.34
2256622583
resolution: "react-native-macos@npm:0.73.34"
@@ -24283,7 +24300,9 @@ __metadata:
2428324300
prettier: 2.8.8
2428424301
react: 18.3.1
2428524302
react-native: 0.76.3
24303+
react-native-build-config: ^0.3.2
2428624304
react-native-gesture-handler: ^2.21.1
24305+
react-native-launch-arguments: ^4.0.4
2428724306
react-native-reanimated: 3.16.1
2428824307
react-native-safe-area-context: 4.14.0
2428924308
react-native-screens: 4.1.0

0 commit comments

Comments
 (0)