android.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import List from 'sentry/components/list/';
  4. import ListItem from 'sentry/components/list/listItem';
  5. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  6. import type {
  7. BasePlatformOptions,
  8. Docs,
  9. DocsParams,
  10. OnboardingConfig,
  11. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  12. import {MobileBetaBanner} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  13. import {getAndroidMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  14. import {
  15. getReplayMobileConfigureDescription,
  16. getReplayVerifyStep,
  17. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  18. import {feedbackOnboardingCrashApiJava} from 'sentry/gettingStartedDocs/java/java';
  19. import {t, tct} from 'sentry/locale';
  20. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  21. export enum InstallationMode {
  22. AUTO = 'auto',
  23. MANUAL = 'manual',
  24. }
  25. const platformOptions = {
  26. installationMode: {
  27. label: t('Installation Mode'),
  28. items: [
  29. {
  30. label: t('Auto'),
  31. value: InstallationMode.AUTO,
  32. },
  33. {
  34. label: t('Manual'),
  35. value: InstallationMode.MANUAL,
  36. },
  37. ],
  38. defaultValue:
  39. navigator.userAgent.indexOf('Win') !== -1
  40. ? InstallationMode.MANUAL
  41. : InstallationMode.AUTO,
  42. },
  43. } satisfies BasePlatformOptions;
  44. type PlatformOptions = typeof platformOptions;
  45. type Params = DocsParams<PlatformOptions>;
  46. const isAutoInstall = (params: Params) =>
  47. params.platformOptions.installationMode === InstallationMode.AUTO;
  48. const getManualInstallSnippet = (params: Params) => `
  49. plugins {
  50. id "com.android.application" // should be in the same module
  51. id "io.sentry.android.gradle" version "${getPackageVersion(
  52. params,
  53. 'sentry.java.android.gradle-plugin',
  54. '3.12.0'
  55. )}"
  56. }`;
  57. const getConfigurationSnippet = (params: Params) => `
  58. <application>
  59. <!-- Required: set your sentry.io project identifier (DSN) -->
  60. <meta-data android:name="io.sentry.dsn" android:value="${params.dsn.public}" />
  61. <!-- enable automatic breadcrumbs for user interactions (clicks, swipes, scrolls) -->
  62. <meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
  63. <!-- enable screenshot for crashes -->
  64. <meta-data android:name="io.sentry.attach-screenshot" android:value="true" />
  65. <!-- enable view hierarchy for crashes -->
  66. <meta-data android:name="io.sentry.attach-view-hierarchy" android:value="true" />${
  67. params.isPerformanceSelected
  68. ? `
  69. <!-- enable the performance API by setting a sample-rate, adjust in production env -->
  70. <meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />`
  71. : ''
  72. }${
  73. params.isProfilingSelected
  74. ? `
  75. <!-- enable profiling when starting transactions, adjust in production env -->
  76. <meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />`
  77. : ''
  78. }
  79. </application>`;
  80. const getVerifySnippet = () => `
  81. val breakWorld = Button(this).apply {
  82. text = "Break the world"
  83. setOnClickListener {
  84. Sentry.captureException(RuntimeException("This app uses Sentry! :)"))
  85. }
  86. }
  87. addContentView(breakWorld, ViewGroup.LayoutParams(
  88. ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))`;
  89. const getReplaySetupSnippetKotlin = (params: Params) => `
  90. SentryAndroid.init(context) { options ->
  91. options.dsn = "${params.dsn.public}"
  92. options.isDebug = true
  93. // Currently under experimental options:
  94. options.experimental.sessionReplay.errorSampleRate = 1.0
  95. options.experimental.sessionReplay.sessionSampleRate = 1.0
  96. }`;
  97. const getReplaySetupSnippetXml = () => `
  98. <meta-data android:name="io.sentry.session-replay.error-sample-rate" android:value="1.0" />
  99. <meta-data android:name="io.sentry.session-replay.session-sample-rate" android:value="1.0" />`;
  100. const getReplayConfigurationSnippet = () => `
  101. options.experimental.sessionReplay.redactAllText = true
  102. options.experimental.sessionReplay.redactAllImages = true`;
  103. const onboarding: OnboardingConfig<PlatformOptions> = {
  104. install: params =>
  105. isAutoInstall(params)
  106. ? [
  107. {
  108. type: StepType.INSTALL,
  109. description: tct(
  110. 'Add Sentry automatically to your app with the [wizardLink:Sentry wizard] (call this inside your project directory).',
  111. {
  112. wizardLink: (
  113. <ExternalLink href="https://docs.sentry.io/platforms/android/#install" />
  114. ),
  115. }
  116. ),
  117. configurations: [
  118. {
  119. language: 'bash',
  120. code: `brew install getsentry/tools/sentry-wizard && sentry-wizard -i android`,
  121. },
  122. {
  123. description: (
  124. <Fragment>
  125. <p>
  126. {t('The Sentry wizard will automatically patch your application:')}
  127. </p>
  128. <List symbol="bullet">
  129. <ListItem>
  130. {tct(
  131. "Update your app's [buildGradle:build.gradle] file with the Sentry Gradle plugin and configure it.",
  132. {
  133. buildGradle: <code />,
  134. }
  135. )}
  136. </ListItem>
  137. <ListItem>
  138. {tct(
  139. 'Update your [manifest: AndroidManifest.xml] with the default Sentry configuration',
  140. {
  141. manifest: <code />,
  142. }
  143. )}
  144. </ListItem>
  145. <ListItem>
  146. {tct(
  147. 'Create [sentryProperties: sentry.properties] with an auth token to upload proguard mappings (this file is automatically added to [gitignore: .gitignore])',
  148. {
  149. sentryProperties: <code />,
  150. gitignore: <code />,
  151. }
  152. )}
  153. </ListItem>
  154. <ListItem>
  155. {t(
  156. "Add an example error to your app's Main Activity to verify your Sentry setup"
  157. )}
  158. </ListItem>
  159. </List>
  160. </Fragment>
  161. ),
  162. additionalInfo: tct(
  163. 'Alternatively, you can also [manualSetupLink:set up the SDK manually].',
  164. {
  165. manualSetupLink: (
  166. <ExternalLink href="https://docs.sentry.io/platforms/android/manual-setup/" />
  167. ),
  168. }
  169. ),
  170. },
  171. ],
  172. },
  173. ]
  174. : [
  175. {
  176. type: StepType.INSTALL,
  177. description: tct(
  178. 'Add the [sagpLink:Sentry Android Gradle plugin] to your [app:app] module:',
  179. {
  180. sagpLink: (
  181. <ExternalLink href="https://docs.sentry.io/platforms/android/configuration/gradle/" />
  182. ),
  183. app: <code />,
  184. }
  185. ),
  186. configurations: [
  187. {
  188. language: 'groovy',
  189. partialLoading: params.sourcePackageRegistries?.isLoading,
  190. code: getManualInstallSnippet(params),
  191. },
  192. ],
  193. },
  194. ],
  195. configure: params =>
  196. isAutoInstall(params)
  197. ? []
  198. : [
  199. {
  200. type: StepType.CONFIGURE,
  201. description: (
  202. <Fragment>
  203. <p>
  204. {tct(
  205. 'Configuration is done via the application [manifest: AndroidManifest.xml]. Under the hood Sentry uses a [provider:ContentProvider] to initialize the SDK based on the values provided below. This way the SDK can capture important crashes and metrics right from the app start.',
  206. {
  207. manifest: <code />,
  208. provider: <code />,
  209. }
  210. )}
  211. </p>
  212. <p>{t("Here's an example config which should get you started:")}</p>
  213. </Fragment>
  214. ),
  215. configurations: [
  216. {
  217. language: 'xml',
  218. code: getConfigurationSnippet(params),
  219. },
  220. ],
  221. },
  222. ],
  223. verify: params =>
  224. isAutoInstall(params)
  225. ? []
  226. : [
  227. {
  228. type: StepType.VERIFY,
  229. description: tct(
  230. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected. You can add it to your app's [mainActivity: MainActivity].",
  231. {
  232. mainActivity: <code />,
  233. }
  234. ),
  235. configurations: [
  236. {
  237. language: 'kotlin',
  238. code: getVerifySnippet(),
  239. },
  240. ],
  241. },
  242. ],
  243. nextSteps: params =>
  244. isAutoInstall(params)
  245. ? [
  246. {
  247. id: 'advanced-configuration',
  248. name: t('Advanced Configuration'),
  249. description: t('Customize the SDK initialization behavior.'),
  250. link: 'https://docs.sentry.io/platforms/android/configuration/manual-init/#manual-initialization',
  251. },
  252. {
  253. id: 'jetpack-compose',
  254. name: t('Jetpack Compose'),
  255. description: t(
  256. 'Learn about our first class integration with Jetpack Compose.'
  257. ),
  258. link: 'https://docs.sentry.io/platforms/android/configuration/integrations/jetpack-compose/',
  259. },
  260. ]
  261. : [
  262. {
  263. id: 'advanced-configuration',
  264. name: t('Advanced Configuration'),
  265. description: t('Customize the SDK initialization behavior.'),
  266. link: 'https://docs.sentry.io/platforms/android/configuration/manual-init/#manual-initialization',
  267. },
  268. {
  269. id: 'proguard-r8',
  270. name: t('ProGuard/R8'),
  271. description: t(
  272. 'Deobfuscate and get readable stacktraces in your Sentry errors.'
  273. ),
  274. link: 'https://docs.sentry.io/platforms/android/configuration/gradle/#proguardr8--dexguard',
  275. },
  276. {
  277. id: 'jetpack-compose',
  278. name: t('Jetpack Compose'),
  279. description: t(
  280. 'Learn about our first class integration with Jetpack Compose.'
  281. ),
  282. link: 'https://docs.sentry.io/platforms/android/configuration/integrations/jetpack-compose/',
  283. },
  284. {
  285. id: 'source-context',
  286. name: t('Source Context'),
  287. description: t('See your source code as part of your stacktraces in Sentry.'),
  288. link: 'https://docs.sentry.io/platforms/android/enhance-errors/source-context/',
  289. },
  290. ],
  291. };
  292. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  293. introduction: () => (
  294. <MobileBetaBanner link="https://docs.sentry.io/platforms/android/session-replay/" />
  295. ),
  296. install: (params: Params) => [
  297. {
  298. type: StepType.INSTALL,
  299. description: tct(
  300. "Make sure your Sentry Android SDK version is at least 7.12.0. The easiest way to update through the Sentry Android Gradle plugin to your app module's [code:build.gradle] file.",
  301. {code: <code />}
  302. ),
  303. configurations: [
  304. {
  305. code: [
  306. {
  307. label: 'Groovy',
  308. value: 'groovy',
  309. language: 'groovy',
  310. filename: 'app/build.gradle',
  311. code: `plugins {
  312. id "com.android.application"
  313. id "io.sentry.android.gradle" version "${getPackageVersion(
  314. params,
  315. 'sentry.java.android.gradle-plugin',
  316. '4.11.0'
  317. )}"
  318. }`,
  319. },
  320. {
  321. label: 'Kotlin',
  322. value: 'kotlin',
  323. language: 'kotlin',
  324. filename: 'app/build.gradle.kts',
  325. code: `plugins {
  326. id("com.android.application")
  327. id("io.sentry.android.gradle") version "${getPackageVersion(
  328. params,
  329. 'sentry.java.android.gradle-plugin',
  330. '4.11.0'
  331. )}"
  332. }`,
  333. },
  334. ],
  335. },
  336. {
  337. description: tct(
  338. 'If you have the SDK installed without the Sentry Gradle Plugin, you can update the version directly in the [code:build.gradle] through:',
  339. {code: <code />}
  340. ),
  341. },
  342. {
  343. code: [
  344. {
  345. label: 'Groovy',
  346. value: 'groovy',
  347. language: 'groovy',
  348. filename: 'app/build.gradle',
  349. code: `dependencies {
  350. implementation 'io.sentry:sentry-android:${getPackageVersion(
  351. params,
  352. 'sentry.java.android',
  353. '7.14.0'
  354. )}'
  355. }`,
  356. },
  357. {
  358. label: 'Kotlin',
  359. value: 'kotlin',
  360. language: 'kotlin',
  361. filename: 'app/build.gradle.kts',
  362. code: `dependencies {
  363. implementation("io.sentry:sentry-android:${getPackageVersion(
  364. params,
  365. 'sentry.java.android',
  366. '7.14.0'
  367. )}")
  368. }`,
  369. },
  370. ],
  371. },
  372. {
  373. description: t(
  374. 'To set up the integration, add the following to your Sentry initialization:'
  375. ),
  376. },
  377. {
  378. code: [
  379. {
  380. label: 'Kotlin',
  381. value: 'kotlin',
  382. language: 'kotlin',
  383. code: getReplaySetupSnippetKotlin(params),
  384. },
  385. {
  386. label: 'XML',
  387. value: 'xml',
  388. language: 'xml',
  389. filename: 'AndroidManifest.xml',
  390. code: getReplaySetupSnippetXml(),
  391. },
  392. ],
  393. },
  394. ],
  395. },
  396. ],
  397. configure: () => [
  398. {
  399. type: StepType.CONFIGURE,
  400. description: getReplayMobileConfigureDescription({
  401. link: 'https://docs.sentry.io/platforms/android/session-replay/#privacy',
  402. }),
  403. configurations: [
  404. {
  405. description: t(
  406. 'The following code is the default configuration, which masks and blocks everything.'
  407. ),
  408. code: [
  409. {
  410. label: 'Kotlin',
  411. value: 'kotlin',
  412. language: 'kotlin',
  413. code: getReplayConfigurationSnippet(),
  414. },
  415. ],
  416. },
  417. ],
  418. },
  419. ],
  420. verify: getReplayVerifyStep({
  421. replayOnErrorSampleRateName:
  422. 'options\u200b.experimental\u200b.sessionReplay\u200b.errorSampleRate',
  423. replaySessionSampleRateName:
  424. 'options\u200b.experimental\u200b.sessionReplay\u200b.sessionSampleRate',
  425. }),
  426. nextSteps: () => [],
  427. };
  428. const docs: Docs<PlatformOptions> = {
  429. onboarding,
  430. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiJava,
  431. crashReportOnboarding: feedbackOnboardingCrashApiJava,
  432. customMetricsOnboarding: getAndroidMetricsOnboarding(),
  433. platformOptions,
  434. replayOnboarding,
  435. };
  436. export default docs;