android.tsx 15 KB

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