android.tsx 14 KB

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