Skip to content

Commit fbf3daa

Browse files
misc(sample): Add SAGP JVM source context (#3532)
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
1 parent e00922b commit fbf3daa

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

CONTRIBUTING.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,30 @@ dependencies {
168168
+ api 'io.sentry:sentry-android:6.7.7-my-local-version'
169169
}
170170
```
171+
172+
## Develop with sentry-android-gradle-plugin
173+
174+
Here are steps on how to debug the gradle builds process with `sentry-android-gradle-plugin`. We assume that you have `sentry-android-gradle-plugin` setup, Android SDK installed, correct JAVA version etc.
175+
176+
1. Add the following code to `samples/react-native/android/settings.gradle`, this ensure the plugin builds at the beginning of the application build:
177+
178+
```groovy
179+
includeBuild('../../../../sentry-android-gradle-plugin/plugin-build') {
180+
dependencySubstitution {
181+
substitute(module 'io.sentry:sentry-android-gradle-plugin') using project(':')
182+
}
183+
}
184+
```
185+
186+
`../../../../sentry-android-gradle-plugin/plugin-build` this example works if `sentry-react-native` and `sentry-android-gradle-plugin` are sibling directories.
187+
188+
2. Open `samples/react-native/android` in Android Studio.
189+
3. Add `Remote JVM Debug` configuration (keep all defaults).
190+
4. Run build command with `-Dorg.gradle.debug=true` and `--no-daemon`, example:
191+
192+
```groovy
193+
./gradlew assembleRelease -Dorg.gradle.debug=true --no-daemon
194+
```
195+
196+
5. The build command will wait for the debugger connection, go to the Android Studio and select the newly created `Remote JVM Debug` configuration and click `Debug`.
197+
6. The build process will stop on active breakpoint.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ sentry {
3535
// Default is disabled.
3636
includeNativeSources = true
3737

38+
// Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
39+
// This enables source context, allowing you to see your source
40+
// code as part of your stack traces in Sentry.
41+
//
42+
// Default is disabled.
43+
includeSourceContext = true
44+
45+
// Includes additional source directories into the source bundle.
46+
// These directories are resolved relative to the project directory
47+
// which is /android/app in this sample application.
48+
// additionalSourceDirsForSourceContext = ["src/main/java"]
49+
3850
// `@sentry/react-native` ships with compatible `sentry-android`
3951
// This option would install the latest version that ships with the SDK or SAGP (Sentry Android Gradle Plugin)
4052
// which might be incompatible with the React Native SDK

samples/react-native/android/app/src/main/java/com/samplenewarchitecture/SamplePackage.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ public List<NativeModule> createNativeModules(
5252
}
5353
});
5454

55+
modules.add(new ReactContextBaseJavaModule() {
56+
@Override public String getName() {
57+
return "CrashModule";
58+
}
59+
60+
@ReactMethod public void crashOrUndefined() {
61+
this.crashNow();
62+
}
63+
64+
@ReactMethod public int crashOrNumber() {
65+
this.crashNow();
66+
return 42;
67+
}
68+
69+
private void crashNow() {
70+
throw new RuntimeException("CrashModule.crashNow()");
71+
}
72+
});
73+
5574
return modules;
5675
}
5776

samples/react-native/src/Screens/HomeScreen.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { UserFeedbackModal } from '../components/UserFeedbackModal';
2020
import { FallbackRender } from '@sentry/react';
2121
import NativeSampleModule from '../../tm/NativeSampleModule';
2222

23-
const { AssetsModule, CppModule } = NativeModules;
23+
const { AssetsModule, CppModule, CrashModule } = NativeModules;
2424

2525
interface Props {
2626
navigation: StackNavigationProp<any, 'HomeScreen'>;
@@ -120,12 +120,26 @@ const HomeScreen = (props: Props) => {
120120
}}
121121
/>
122122
{Platform.OS === 'android' && (
123-
<Button
124-
title="Crash in Android Cpp"
125-
onPress={() => {
126-
CppModule?.crashCpp();
127-
}}
128-
/>
123+
<>
124+
<Button
125+
title="Crash in Android Cpp"
126+
onPress={() => {
127+
CppModule?.crashCpp();
128+
}}
129+
/>
130+
<Button
131+
title="JVM Crash or Undefined"
132+
onPress={() => {
133+
CrashModule.crashOrUndefined();
134+
}}
135+
/>
136+
<Button
137+
title="JVM Crash or Number"
138+
onPress={() => {
139+
CrashModule.crashOrNumber();
140+
}}
141+
/>
142+
</>
129143
)}
130144
<Spacer />
131145
<Sentry.ErrorBoundary fallback={errorBoundaryFallback}>

0 commit comments

Comments
 (0)