-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathwithSentryAndroidGradlePlugin.ts
More file actions
113 lines (99 loc) · 4.08 KB
/
withSentryAndroidGradlePlugin.ts
File metadata and controls
113 lines (99 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type { ExpoConfig } from '@expo/config-types';
import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins';
import { warnOnce } from './logger';
export interface SentryAndroidGradlePluginOptions {
enableAndroidGradlePlugin?: boolean;
includeProguardMapping?: boolean;
dexguardEnabled?: boolean;
autoUploadNativeSymbols?: boolean;
autoUploadProguardMapping?: boolean;
uploadNativeSymbols?: boolean;
includeNativeSources?: boolean;
includeSourceContext?: boolean;
}
export const sentryAndroidGradlePluginVersion = '6.4.0';
/**
* Adds the Sentry Android Gradle Plugin to the project.
* https://docs.sentry.io/platforms/react-native/manual-setup/manual-setup/#enable-sentry-agp
*/
export function withSentryAndroidGradlePlugin(
config: ExpoConfig,
{
includeProguardMapping = true,
dexguardEnabled = false,
autoUploadProguardMapping = true,
uploadNativeSymbols = true,
autoUploadNativeSymbols = true,
includeNativeSources = true,
includeSourceContext = false,
}: SentryAndroidGradlePluginOptions = {},
): ExpoConfig {
// Modify android/build.gradle
const withSentryProjectBuildGradle = (config: ExpoConfig): ExpoConfig => {
return withProjectBuildGradle(config, projectBuildGradle => {
if (!projectBuildGradle.modResults?.contents) {
warnOnce('android/build.gradle content is missing or undefined.');
return projectBuildGradle;
}
if (projectBuildGradle.modResults.language !== 'groovy') {
warnOnce('Cannot configure Sentry in android/build.gradle because it is not in Groovy.');
return projectBuildGradle;
}
const dependency = `classpath("io.sentry:sentry-android-gradle-plugin:${sentryAndroidGradlePluginVersion}")`;
if (projectBuildGradle.modResults.contents.includes(dependency)) {
warnOnce('sentry-android-gradle-plugin dependency in already in android/build.gradle.');
return projectBuildGradle;
}
try {
const updatedContents = projectBuildGradle.modResults.contents.replace(
/dependencies\s*{/,
`dependencies {\n ${dependency}`,
);
if (updatedContents === projectBuildGradle.modResults.contents) {
warnOnce('Failed to inject the dependency. Could not find `dependencies` in build.gradle.');
} else {
projectBuildGradle.modResults.contents = updatedContents;
}
} catch (error) {
warnOnce('An error occurred while trying to modify build.gradle');
}
return projectBuildGradle;
});
};
// Modify android/app/build.gradle
const withSentryAppBuildGradle = (config: ExpoConfig): ExpoConfig => {
return withAppBuildGradle(config, appBuildGradle => {
if (appBuildGradle.modResults.language !== 'groovy') {
warnOnce('Cannot configure Sentry in android/app/build.gradle because it is not in Groovy.');
return appBuildGradle;
}
const sentryPlugin = 'apply plugin: "io.sentry.android.gradle"';
const sentryConfig = `
sentry {
autoUploadProguardMapping = ${autoUploadProguardMapping ? 'shouldSentryAutoUpload()' : 'false'}
includeProguardMapping = ${includeProguardMapping}
dexguardEnabled = ${dexguardEnabled}
uploadNativeSymbols = ${uploadNativeSymbols ? 'shouldSentryAutoUpload()' : 'false'}
autoUploadNativeSymbols = ${autoUploadNativeSymbols ? 'shouldSentryAutoUpload()' : 'false'}
includeNativeSources = ${includeNativeSources}
includeSourceContext = ${includeSourceContext ? 'shouldSentryAutoUpload()' : 'false'}
tracingInstrumentation {
enabled = false
}
autoInstallation {
enabled = false
}
}`;
let contents = appBuildGradle.modResults.contents;
if (!contents.includes(sentryPlugin)) {
contents = `${sentryPlugin}\n${contents}`;
}
if (!contents.includes('sentry {')) {
contents = `${contents}\n${sentryConfig}`;
}
appBuildGradle.modResults.contents = contents;
return appBuildGradle;
});
};
return withSentryAppBuildGradle(withSentryProjectBuildGradle(config));
}