android.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.onErrorSampleRate = 1.0
  95. options.experimental.sessionReplay.sessionSampleRate = 1.0
  96. }`;
  97. const getReplaySetupSnippetXml = () => `
  98. <meta-data android:name="io.sentry.session-replay.on-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 [code: sentry.properties] with an auth token to upload proguard mappings (this file is automatically added to [code: .gitignore])',
  148. {
  149. code: <code />,
  150. }
  151. )}
  152. </ListItem>
  153. <ListItem>
  154. {t(
  155. "Add an example error to your app's Main Activity to verify your Sentry setup"
  156. )}
  157. </ListItem>
  158. </List>
  159. </Fragment>
  160. ),
  161. additionalInfo: tct(
  162. 'Alternatively, you can also [manualSetupLink:set up the SDK manually].',
  163. {
  164. manualSetupLink: (
  165. <ExternalLink href="https://docs.sentry.io/platforms/android/manual-setup/" />
  166. ),
  167. }
  168. ),
  169. },
  170. ],
  171. },
  172. ]
  173. : [
  174. {
  175. type: StepType.INSTALL,
  176. description: tct(
  177. 'Add the [sagpLink:Sentry Android Gradle plugin] to your [app:app] module:',
  178. {
  179. sagpLink: (
  180. <ExternalLink href="https://docs.sentry.io/platforms/android/configuration/gradle/" />
  181. ),
  182. app: <code />,
  183. }
  184. ),
  185. configurations: [
  186. {
  187. language: 'groovy',
  188. partialLoading: params.sourcePackageRegistries?.isLoading,
  189. code: getManualInstallSnippet(params),
  190. },
  191. ],
  192. },
  193. ],
  194. configure: params =>
  195. isAutoInstall(params)
  196. ? []
  197. : [
  198. {
  199. type: StepType.CONFIGURE,
  200. description: (
  201. <Fragment>
  202. <p>
  203. {tct(
  204. '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.',
  205. {
  206. code: <code />,
  207. }
  208. )}
  209. </p>
  210. <p>{t("Here's an example config which should get you started:")}</p>
  211. </Fragment>
  212. ),
  213. configurations: [
  214. {
  215. language: 'xml',
  216. code: getConfigurationSnippet(params),
  217. },
  218. ],
  219. },
  220. ],
  221. verify: params =>
  222. isAutoInstall(params)
  223. ? []
  224. : [
  225. {
  226. type: StepType.VERIFY,
  227. description: tct(
  228. "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].",
  229. {
  230. mainActivity: <code />,
  231. }
  232. ),
  233. configurations: [
  234. {
  235. language: 'kotlin',
  236. code: getVerifySnippet(),
  237. },
  238. ],
  239. },
  240. ],
  241. nextSteps: params =>
  242. isAutoInstall(params)
  243. ? [
  244. {
  245. id: 'advanced-configuration',
  246. name: t('Advanced Configuration'),
  247. description: t('Customize the SDK initialization behavior.'),
  248. link: 'https://docs.sentry.io/platforms/android/configuration/manual-init/#manual-initialization',
  249. },
  250. {
  251. id: 'jetpack-compose',
  252. name: t('Jetpack Compose'),
  253. description: t(
  254. 'Learn about our first class integration with Jetpack Compose.'
  255. ),
  256. link: 'https://docs.sentry.io/platforms/android/configuration/integrations/jetpack-compose/',
  257. },
  258. ]
  259. : [
  260. {
  261. id: 'advanced-configuration',
  262. name: t('Advanced Configuration'),
  263. description: t('Customize the SDK initialization behavior.'),
  264. link: 'https://docs.sentry.io/platforms/android/configuration/manual-init/#manual-initialization',
  265. },
  266. {
  267. id: 'proguard-r8',
  268. name: t('ProGuard/R8'),
  269. description: t(
  270. 'Deobfuscate and get readable stacktraces in your Sentry errors.'
  271. ),
  272. link: 'https://docs.sentry.io/platforms/android/configuration/gradle/#proguardr8--dexguard',
  273. },
  274. {
  275. id: 'jetpack-compose',
  276. name: t('Jetpack Compose'),
  277. description: t(
  278. 'Learn about our first class integration with Jetpack Compose.'
  279. ),
  280. link: 'https://docs.sentry.io/platforms/android/configuration/integrations/jetpack-compose/',
  281. },
  282. {
  283. id: 'source-context',
  284. name: t('Source Context'),
  285. description: t('See your source code as part of your stacktraces in Sentry.'),
  286. link: 'https://docs.sentry.io/platforms/android/enhance-errors/source-context/',
  287. },
  288. ],
  289. };
  290. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  291. introduction: () => (
  292. <MobileBetaBanner link="https://docs.sentry.io/platforms/android/session-replay/" />
  293. ),
  294. install: (params: Params) => [
  295. {
  296. type: StepType.INSTALL,
  297. description: tct(
  298. "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.",
  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:
  420. 'options\u200b.experimental\u200b.sessionReplay\u200b.onErrorSampleRate',
  421. replaySessionSampleRateName:
  422. 'options\u200b.experimental\u200b.sessionReplay\u200b.sessionSampleRate',
  423. }),
  424. nextSteps: () => [],
  425. };
  426. const docs: Docs<PlatformOptions> = {
  427. onboarding,
  428. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiJava,
  429. crashReportOnboarding: feedbackOnboardingCrashApiJava,
  430. customMetricsOnboarding: getAndroidMetricsOnboarding(),
  431. platformOptions,
  432. replayOnboarding,
  433. };
  434. export default docs;