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