|
| 1 | +import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins'; |
| 2 | + |
| 3 | +import { warnOnce } from './utils'; |
| 4 | + |
| 5 | +export interface SentryAndroidGradlePluginOptions { |
| 6 | + enableAndroidGradlePlugin?: boolean; |
| 7 | + includeProguardMapping?: boolean; |
| 8 | + dexguardEnabled?: boolean; |
| 9 | + autoUploadNativeSymbols?: boolean; |
| 10 | + autoUploadProguardMapping?: boolean; |
| 11 | + uploadNativeSymbols?: boolean; |
| 12 | + includeNativeSources?: boolean; |
| 13 | + includeSourceContext?: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Adds the Sentry Android Gradle Plugin to the project. |
| 18 | + * https://docs.sentry.io/platforms/react-native/manual-setup/manual-setup/#enable-sentry-agp |
| 19 | + */ |
| 20 | +export function withSentryAndroidGradlePlugin( |
| 21 | + config: any, |
| 22 | + { |
| 23 | + includeProguardMapping = true, |
| 24 | + dexguardEnabled = false, |
| 25 | + autoUploadProguardMapping = true, |
| 26 | + uploadNativeSymbols = true, |
| 27 | + autoUploadNativeSymbols = true, |
| 28 | + includeNativeSources = true, |
| 29 | + includeSourceContext = false, |
| 30 | + }: SentryAndroidGradlePluginOptions = {}, |
| 31 | +): any { |
| 32 | + const version = '4.14.1'; |
| 33 | + |
| 34 | + // Modify android/build.gradle |
| 35 | + const withSentryProjectBuildGradle = (config: any): any => { |
| 36 | + return withProjectBuildGradle(config, (projectBuildGradle: any) => { |
| 37 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 38 | + if (!projectBuildGradle.modResults || !projectBuildGradle.modResults.contents) { |
| 39 | + warnOnce('android/build.gradle content is missing or undefined.'); |
| 40 | + return config; |
| 41 | + } |
| 42 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 43 | + if (projectBuildGradle.modResults.language !== 'groovy') { |
| 44 | + warnOnce('Cannot configure Sentry in android/build.gradle because it is not in Groovy.'); |
| 45 | + return config; |
| 46 | + } |
| 47 | + |
| 48 | + const dependency = `classpath("io.sentry:sentry-android-gradle-plugin:${version}")`; |
| 49 | + |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 51 | + if (projectBuildGradle.modResults.contents.includes(dependency)) { |
| 52 | + warnOnce('sentry-android-gradle-plugin dependency in already in android/build.gradle.'); |
| 53 | + return config; |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 58 | + const updatedContents = projectBuildGradle.modResults.contents.replace( |
| 59 | + /dependencies\s*{/, |
| 60 | + `dependencies {\n ${dependency}`, |
| 61 | + ); |
| 62 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 63 | + if (updatedContents === projectBuildGradle.modResults.contents) { |
| 64 | + warnOnce('Failed to inject the dependency. Could not find `dependencies` in build.gradle.'); |
| 65 | + } else { |
| 66 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 67 | + projectBuildGradle.modResults.contents = updatedContents; |
| 68 | + } |
| 69 | + } catch (error) { |
| 70 | + warnOnce(`An error occurred while trying to modify build.gradle`); |
| 71 | + } |
| 72 | + return projectBuildGradle; |
| 73 | + }); |
| 74 | + }; |
| 75 | + |
| 76 | + // Modify android/app/build.gradle |
| 77 | + const withSentryAppBuildGradle = (config: any): any => { |
| 78 | + return withAppBuildGradle(config, (config: any) => { |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 80 | + if (config.modResults.language !== 'groovy') { |
| 81 | + warnOnce('Cannot configure Sentry in android/app/build.gradle because it is not in Groovy.'); |
| 82 | + return config; |
| 83 | + } |
| 84 | + const sentryPlugin = `apply plugin: "io.sentry.android.gradle"`; |
| 85 | + const sentryConfig = ` |
| 86 | + sentry { |
| 87 | + autoUploadProguardMapping = ${autoUploadProguardMapping} |
| 88 | + includeProguardMapping = ${includeProguardMapping} |
| 89 | + dexguardEnabled = ${dexguardEnabled} |
| 90 | + uploadNativeSymbols = ${uploadNativeSymbols} |
| 91 | + autoUploadNativeSymbols = ${autoUploadNativeSymbols} |
| 92 | + includeNativeSources = ${includeNativeSources} |
| 93 | + includeSourceContext = ${includeSourceContext} |
| 94 | + tracingInstrumentation { |
| 95 | + enabled = false |
| 96 | + } |
| 97 | + autoInstallation { |
| 98 | + enabled = false |
| 99 | + } |
| 100 | + }`; |
| 101 | + |
| 102 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 103 | + let contents = config.modResults.contents; |
| 104 | + |
| 105 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 106 | + if (!contents.includes(sentryPlugin)) { |
| 107 | + contents = `${sentryPlugin}\n${contents}`; |
| 108 | + } |
| 109 | + |
| 110 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 111 | + if (!contents.includes('sentry {')) { |
| 112 | + contents = `${contents}\n${sentryConfig}`; |
| 113 | + } |
| 114 | + |
| 115 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 116 | + config.modResults.contents = contents; |
| 117 | + return config; |
| 118 | + }); |
| 119 | + }; |
| 120 | + |
| 121 | + return withSentryAppBuildGradle(withSentryProjectBuildGradle(config)); |
| 122 | +} |
0 commit comments