@react-native-harness/platform-android 1.0.0 → 1.1.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/__tests__/adb.test.d.ts +2 -0
  2. package/dist/__tests__/adb.test.d.ts.map +1 -0
  3. package/dist/__tests__/adb.test.js +72 -0
  4. package/dist/__tests__/app-monitor.test.d.ts +2 -0
  5. package/dist/__tests__/app-monitor.test.d.ts.map +1 -0
  6. package/dist/__tests__/app-monitor.test.js +202 -0
  7. package/dist/__tests__/crash-parser.test.d.ts +2 -0
  8. package/dist/__tests__/crash-parser.test.d.ts.map +1 -0
  9. package/dist/__tests__/crash-parser.test.js +45 -0
  10. package/dist/__tests__/shared-prefs.test.d.ts +2 -0
  11. package/dist/__tests__/shared-prefs.test.d.ts.map +1 -0
  12. package/dist/__tests__/shared-prefs.test.js +87 -0
  13. package/dist/adb.d.ts +6 -1
  14. package/dist/adb.d.ts.map +1 -1
  15. package/dist/adb.js +84 -18
  16. package/dist/app-monitor.d.ts +13 -0
  17. package/dist/app-monitor.d.ts.map +1 -0
  18. package/dist/app-monitor.js +359 -0
  19. package/dist/config.d.ts +21 -0
  20. package/dist/config.d.ts.map +1 -1
  21. package/dist/config.js +6 -0
  22. package/dist/crash-parser.d.ts +11 -0
  23. package/dist/crash-parser.d.ts.map +1 -0
  24. package/dist/crash-parser.js +39 -0
  25. package/dist/runner.d.ts +2 -2
  26. package/dist/runner.d.ts.map +1 -1
  27. package/dist/runner.js +21 -5
  28. package/dist/shared-prefs.d.ts +3 -0
  29. package/dist/shared-prefs.d.ts.map +1 -0
  30. package/dist/shared-prefs.js +92 -0
  31. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  32. package/eslint.config.mjs +4 -1
  33. package/package.json +4 -4
  34. package/src/__tests__/adb.test.ts +89 -0
  35. package/src/__tests__/app-monitor.test.ts +273 -0
  36. package/src/__tests__/crash-parser.test.ts +52 -0
  37. package/src/__tests__/shared-prefs.test.ts +144 -0
  38. package/src/adb.ts +111 -18
  39. package/src/app-monitor.ts +544 -0
  40. package/src/config.ts +10 -0
  41. package/src/crash-parser.ts +66 -0
  42. package/src/runner.ts +31 -7
  43. package/src/shared-prefs.ts +205 -0
  44. package/tsconfig.json +2 -2
  45. package/tsconfig.lib.json +2 -2
  46. package/tsconfig.tsbuildinfo +1 -0
  47. package/dist/assertions.d.ts +0 -5
  48. package/dist/assertions.d.ts.map +0 -1
  49. package/dist/assertions.js +0 -6
  50. package/dist/emulator.d.ts +0 -6
  51. package/dist/emulator.d.ts.map +0 -1
  52. package/dist/emulator.js +0 -27
  53. package/dist/errors.d.ts +0 -15
  54. package/dist/errors.d.ts.map +0 -1
  55. package/dist/errors.js +0 -28
  56. package/dist/reader.d.ts +0 -6
  57. package/dist/reader.d.ts.map +0 -1
  58. package/dist/reader.js +0 -57
  59. package/dist/types.d.ts +0 -381
  60. package/dist/types.d.ts.map +0 -1
  61. package/dist/types.js +0 -107
@@ -0,0 +1,544 @@
1
+ import {
2
+ type AppMonitor,
3
+ type AppCrashDetails,
4
+ type CrashArtifactWriter,
5
+ type CrashDetailsLookupOptions,
6
+ type AppMonitorEvent,
7
+ type AppMonitorListener,
8
+ } from '@react-native-harness/platforms';
9
+ import { escapeRegExp, getEmitter, logger, spawn, SubprocessError, type Subprocess } from '@react-native-harness/tools';
10
+ import * as adb from './adb.js';
11
+ import { androidCrashParser } from './crash-parser.js';
12
+
13
+ const androidAppMonitorLogger = logger.child('android-app-monitor');
14
+
15
+ const getLogcatArgs = (uid: number, fromTime: string) =>
16
+ ['logcat', '-v', 'threadtime', '-b', 'crash', `--uid=${uid}`, '-T', fromTime] as const;
17
+ const MAX_RECENT_LOG_LINES = 200;
18
+ const MAX_RECENT_CRASH_ARTIFACTS = 10;
19
+ const CRASH_ARTIFACT_SETTLE_DELAY_MS = 100;
20
+
21
+ const startProcPattern = (bundleId: string) =>
22
+ new RegExp(`Start proc (\\d+):${escapeRegExp(bundleId)}(?:/|\\s)`);
23
+
24
+ const processPattern = (bundleId: string) =>
25
+ new RegExp(`Process:\\s*${escapeRegExp(bundleId)},\\s*PID:\\s*(\\d+)`);
26
+
27
+ const nativeCrashPattern = (bundleId: string) =>
28
+ new RegExp(`>>>\\s*${escapeRegExp(bundleId)}\\s*<<<`);
29
+
30
+ const processDiedPattern = (bundleId: string) =>
31
+ new RegExp(
32
+ `Process\\s+${escapeRegExp(bundleId)}\\s+\\(pid\\s+(\\d+)\\)\\s+has\\s+died`,
33
+ 'i'
34
+ );
35
+
36
+ const getSignal = (line: string) => {
37
+ const namedSignalMatch = line.match(/\b(SIG[A-Z0-9]+)\b/);
38
+
39
+ if (namedSignalMatch) {
40
+ return namedSignalMatch[1];
41
+ }
42
+
43
+ const signalNumberMatch = line.match(/signal\s+(\d+)/i);
44
+
45
+ if (signalNumberMatch) {
46
+ return `signal ${signalNumberMatch[1]}`;
47
+ }
48
+
49
+ return undefined;
50
+ };
51
+
52
+ const getAndroidLogLineCrashDetails = ({
53
+ line,
54
+ bundleId,
55
+ pid,
56
+ }: {
57
+ line: string;
58
+ bundleId: string;
59
+ pid?: number;
60
+ }): AppCrashDetails => {
61
+ const fatalExceptionMatch = line.match(/FATAL EXCEPTION:\s*(.+)$/i);
62
+ const processMatch = line.match(processPattern(bundleId));
63
+
64
+ return {
65
+ source: 'logs',
66
+ summary: line.trim(),
67
+ signal: getSignal(line),
68
+ exceptionType: fatalExceptionMatch?.[1]?.trim(),
69
+ processName: processMatch ? bundleId : line.includes(bundleId) ? bundleId : undefined,
70
+ pid: pid ?? (processMatch ? Number(processMatch[1]) : undefined),
71
+ rawLines: [line],
72
+ };
73
+ };
74
+
75
+ type TimedLogLine = {
76
+ line: string;
77
+ occurredAt: number;
78
+ };
79
+
80
+ type AndroidCrashArtifact = AppCrashDetails & {
81
+ occurredAt: number;
82
+ triggerLine: string;
83
+ triggerOccurredAt?: number;
84
+ };
85
+
86
+ const CRASH_BLOCK_HEADER = '--------- beginning of crash';
87
+
88
+ const getLatestCrashBlock = (recentLogLines: TimedLogLine[]) => {
89
+ const lines = recentLogLines.map(({ line }) => line);
90
+ let latestCrashHeaderIndex = -1;
91
+
92
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
93
+ if (/FATAL EXCEPTION:|Process:\s+.+,\s+PID:/i.test(lines[index])) {
94
+ latestCrashHeaderIndex = index;
95
+ break;
96
+ }
97
+ }
98
+
99
+ const blockStartIndex = Math.max(
100
+ lines.lastIndexOf(CRASH_BLOCK_HEADER),
101
+ latestCrashHeaderIndex
102
+ );
103
+
104
+ if (blockStartIndex === -1) {
105
+ return lines;
106
+ }
107
+
108
+ return lines.slice(blockStartIndex);
109
+ };
110
+
111
+ const getCrashBlockForArtifact = ({
112
+ artifact,
113
+ recentLogLines,
114
+ }: {
115
+ artifact: AndroidCrashArtifact;
116
+ recentLogLines: TimedLogLine[];
117
+ }): string[] => {
118
+ const targetIndex = recentLogLines.findIndex(
119
+ ({ line, occurredAt }) =>
120
+ line === artifact.triggerLine &&
121
+ (artifact.triggerOccurredAt === undefined ||
122
+ occurredAt === artifact.triggerOccurredAt)
123
+ );
124
+
125
+ if (targetIndex === -1) {
126
+ return artifact.rawLines ?? [];
127
+ }
128
+
129
+ let blockStartIndex = targetIndex;
130
+
131
+ for (let index = targetIndex; index >= 0; index -= 1) {
132
+ const { line } = recentLogLines[index];
133
+
134
+ if (line === CRASH_BLOCK_HEADER) {
135
+ blockStartIndex = index;
136
+ break;
137
+ }
138
+ }
139
+
140
+ let blockEndIndex = recentLogLines.length;
141
+
142
+ for (let index = targetIndex + 1; index < recentLogLines.length; index += 1) {
143
+ if (recentLogLines[index].line === CRASH_BLOCK_HEADER) {
144
+ blockEndIndex = index;
145
+ break;
146
+ }
147
+ }
148
+
149
+ return recentLogLines
150
+ .slice(blockStartIndex, blockEndIndex)
151
+ .map(({ line }) => line);
152
+ };
153
+
154
+ const hydrateCrashArtifact = ({
155
+ artifact,
156
+ recentLogLines,
157
+ }: {
158
+ artifact: AndroidCrashArtifact;
159
+ recentLogLines: TimedLogLine[];
160
+ }): AppCrashDetails => {
161
+ const rawLines = getCrashBlockForArtifact({ artifact, recentLogLines });
162
+
163
+ if (rawLines.length === 0) {
164
+ return artifact;
165
+ }
166
+
167
+ const parsedDetails = androidCrashParser.parse({
168
+ contents: rawLines.join('\n'),
169
+ bundleId: artifact.processName ?? '',
170
+ pid: artifact.pid,
171
+ });
172
+
173
+ return {
174
+ ...artifact,
175
+ ...parsedDetails,
176
+ artifactType: artifact.artifactType,
177
+ artifactPath: artifact.artifactPath,
178
+ rawLines,
179
+ };
180
+ };
181
+
182
+ const createCrashArtifact = ({
183
+ details,
184
+ recentLogLines,
185
+ }: {
186
+ details: AppCrashDetails;
187
+ recentLogLines: TimedLogLine[];
188
+ }): AndroidCrashArtifact => {
189
+ const occurredAt = Date.now();
190
+ const rawLines = getLatestCrashBlock(recentLogLines);
191
+ const triggerOccurredAt = [...recentLogLines]
192
+ .reverse()
193
+ .find(({ line }) => line === details.summary)?.occurredAt;
194
+ const contents =
195
+ rawLines.length > 0
196
+ ? rawLines.join('\n')
197
+ : (details.rawLines ?? []).join('\n');
198
+ const parsedDetails =
199
+ details.processName !== undefined
200
+ ? androidCrashParser.parse({
201
+ contents,
202
+ bundleId: details.processName,
203
+ pid: details.pid,
204
+ })
205
+ : details;
206
+
207
+ return {
208
+ ...parsedDetails,
209
+ occurredAt,
210
+ triggerLine: details.summary ?? '',
211
+ triggerOccurredAt,
212
+ artifactType: 'logcat',
213
+ rawLines:
214
+ rawLines.length > 0 ? rawLines : parsedDetails.rawLines ?? details.rawLines,
215
+ };
216
+ };
217
+
218
+ const persistCrashArtifact = ({
219
+ details,
220
+ crashArtifactWriter,
221
+ }: {
222
+ details: AppCrashDetails;
223
+ crashArtifactWriter?: CrashArtifactWriter;
224
+ }): AppCrashDetails => {
225
+ if (!crashArtifactWriter || details.artifactType !== 'logcat') {
226
+ return details;
227
+ }
228
+
229
+ const artifactBody = details.rawLines?.join('\n');
230
+
231
+ if (!artifactBody) {
232
+ return details;
233
+ }
234
+
235
+ return {
236
+ ...details,
237
+ artifactPath: crashArtifactWriter.persistArtifact({
238
+ artifactKind: details.artifactType,
239
+ source: {
240
+ kind: 'text',
241
+ fileName: 'logcat.txt',
242
+ text: `${artifactBody}\n`,
243
+ },
244
+ }),
245
+ };
246
+ };
247
+
248
+ const getLatestCrashArtifact = ({
249
+ crashArtifacts,
250
+ recentLogLines,
251
+ processName,
252
+ pid,
253
+ occurredAt,
254
+ }: CrashDetailsLookupOptions & {
255
+ crashArtifacts: AndroidCrashArtifact[];
256
+ recentLogLines: TimedLogLine[];
257
+ }): AppCrashDetails | null => {
258
+ const matchingByPid = pid
259
+ ? crashArtifacts.filter((artifact) => artifact.pid === pid)
260
+ : [];
261
+ const matchingByProcess = processName
262
+ ? crashArtifacts.filter((artifact) => artifact.processName === processName)
263
+ : [];
264
+ const candidates =
265
+ matchingByPid.length > 0
266
+ ? matchingByPid
267
+ : matchingByProcess.length > 0
268
+ ? matchingByProcess
269
+ : crashArtifacts;
270
+ const sortedCandidates = [...candidates].sort(
271
+ (left, right) =>
272
+ Math.abs(left.occurredAt - occurredAt) - Math.abs(right.occurredAt - occurredAt)
273
+ );
274
+
275
+ const artifact = sortedCandidates[0];
276
+
277
+ if (!artifact) {
278
+ return null;
279
+ }
280
+
281
+ return hydrateCrashArtifact({
282
+ artifact,
283
+ recentLogLines,
284
+ });
285
+ };
286
+
287
+ const createAndroidLogEvent = (
288
+ line: string,
289
+ bundleId: string
290
+ ): AppMonitorEvent | null => {
291
+ const startMatch = line.match(startProcPattern(bundleId));
292
+
293
+ if (startMatch) {
294
+ return {
295
+ type: 'app_started',
296
+ pid: Number(startMatch[1]),
297
+ source: 'logs',
298
+ line,
299
+ };
300
+ }
301
+
302
+ const processMatch = line.match(processPattern(bundleId));
303
+
304
+ if (processMatch) {
305
+ return {
306
+ type: 'possible_crash',
307
+ pid: Number(processMatch[1]),
308
+ source: 'logs',
309
+ line,
310
+ crashDetails: getAndroidLogLineCrashDetails({
311
+ line,
312
+ bundleId,
313
+ pid: Number(processMatch[1]),
314
+ }),
315
+ };
316
+ }
317
+
318
+ if (nativeCrashPattern(bundleId).test(line)) {
319
+ return {
320
+ type: 'possible_crash',
321
+ source: 'logs',
322
+ line,
323
+ crashDetails: getAndroidLogLineCrashDetails({
324
+ line,
325
+ bundleId,
326
+ }),
327
+ };
328
+ }
329
+
330
+ const diedMatch = line.match(processDiedPattern(bundleId));
331
+
332
+ if (diedMatch) {
333
+ return {
334
+ type: 'app_exited',
335
+ pid: Number(diedMatch[1]),
336
+ source: 'logs',
337
+ line,
338
+ crashDetails: getAndroidLogLineCrashDetails({
339
+ line,
340
+ bundleId,
341
+ pid: Number(diedMatch[1]),
342
+ }),
343
+ };
344
+ }
345
+
346
+ if (
347
+ line.includes(bundleId) &&
348
+ /fatal|crash|signal 11|signal 6|backtrace/i.test(line)
349
+ ) {
350
+ return {
351
+ type: 'possible_crash',
352
+ source: 'logs',
353
+ line,
354
+ crashDetails: getAndroidLogLineCrashDetails({
355
+ line,
356
+ bundleId,
357
+ }),
358
+ };
359
+ }
360
+
361
+ return null;
362
+ };
363
+
364
+ export const createAndroidAppMonitor = ({
365
+ adbId,
366
+ bundleId,
367
+ appUid,
368
+ crashArtifactWriter,
369
+ }: {
370
+ adbId: string;
371
+ bundleId: string;
372
+ appUid: number;
373
+ crashArtifactWriter?: CrashArtifactWriter;
374
+ }): AndroidAppMonitor => {
375
+ const emitter = getEmitter<AppMonitorEvent>();
376
+
377
+ let isStarted = false;
378
+ let logcatProcess: Subprocess | null = null;
379
+ let logTask: Promise<void> | null = null;
380
+ let recentLogLines: TimedLogLine[] = [];
381
+ let recentCrashArtifacts: AndroidCrashArtifact[] = [];
382
+
383
+ const emit = (event: AppMonitorEvent) => {
384
+ emitter.emit(event);
385
+ };
386
+
387
+ const recordLogLine = (line: string) => {
388
+ recentLogLines = [...recentLogLines, { line, occurredAt: Date.now() }].slice(
389
+ -MAX_RECENT_LOG_LINES
390
+ );
391
+ };
392
+
393
+ const recordCrashArtifact = (details?: AppCrashDetails) => {
394
+ if (!details) {
395
+ return;
396
+ }
397
+
398
+ recentCrashArtifacts = [
399
+ ...recentCrashArtifacts,
400
+ createCrashArtifact({
401
+ details,
402
+ recentLogLines,
403
+ }),
404
+ ].slice(-MAX_RECENT_CRASH_ARTIFACTS);
405
+ };
406
+
407
+ const stopProcess = async (child: Subprocess | null) => {
408
+ if (!child) {
409
+ return;
410
+ }
411
+
412
+ try {
413
+ (await child.nodeChildProcess).kill();
414
+ } catch {
415
+ // Ignore termination failures for background monitors.
416
+ }
417
+ };
418
+
419
+ const startLogcat = async () => {
420
+ const logcatTimestamp = await adb.getLogcatTimestamp(adbId);
421
+
422
+ logcatProcess = spawn('adb', ['-s', adbId, ...getLogcatArgs(appUid, logcatTimestamp)], {
423
+ stdout: 'pipe',
424
+ stderr: 'pipe',
425
+ });
426
+
427
+ const currentProcess = logcatProcess;
428
+
429
+ if (!currentProcess) {
430
+ return;
431
+ }
432
+
433
+ logTask = (async () => {
434
+ try {
435
+ for await (const line of currentProcess) {
436
+ recordLogLine(line);
437
+ emit({ type: 'log', source: 'logs', line });
438
+
439
+ const event = createAndroidLogEvent(line, bundleId);
440
+
441
+ if (event) {
442
+ if (event.type === 'possible_crash' || event.type === 'app_exited') {
443
+ recordCrashArtifact(event.crashDetails);
444
+ }
445
+ emit(event);
446
+ }
447
+ }
448
+ } catch (error) {
449
+ if (!(error instanceof SubprocessError && error.signalName === 'SIGTERM')) {
450
+ androidAppMonitorLogger.debug('Android logcat monitor stopped', error);
451
+ }
452
+ }
453
+ })();
454
+ };
455
+
456
+ const start = async () => {
457
+ if (isStarted) {
458
+ return;
459
+ }
460
+
461
+ try {
462
+ await startLogcat();
463
+ isStarted = true;
464
+ } catch (error) {
465
+ const currentProcess = logcatProcess;
466
+ const currentTask = logTask;
467
+
468
+ logcatProcess = null;
469
+ logTask = null;
470
+
471
+ await stopProcess(currentProcess);
472
+ await currentTask;
473
+
474
+ throw error;
475
+ }
476
+ };
477
+
478
+ const stop = async () => {
479
+ if (!isStarted) {
480
+ return;
481
+ }
482
+
483
+ isStarted = false;
484
+
485
+ const currentProcess = logcatProcess;
486
+ const currentTask = logTask;
487
+
488
+ logcatProcess = null;
489
+ logTask = null;
490
+
491
+ await stopProcess(currentProcess);
492
+ await currentTask;
493
+ };
494
+
495
+ const dispose = async () => {
496
+ await stop();
497
+ emitter.clearAllListeners();
498
+ recentLogLines = [];
499
+ recentCrashArtifacts = [];
500
+ };
501
+
502
+ const addListener = (listener: AppMonitorListener) => {
503
+ emitter.addListener(listener);
504
+ };
505
+
506
+ const removeListener = (listener: AppMonitorListener) => {
507
+ emitter.removeListener(listener);
508
+ };
509
+
510
+ return {
511
+ start,
512
+ stop,
513
+ dispose,
514
+ addListener,
515
+ removeListener,
516
+ getCrashDetails: async (options: CrashDetailsLookupOptions) => {
517
+ await new Promise((resolve) =>
518
+ setTimeout(resolve, CRASH_ARTIFACT_SETTLE_DELAY_MS)
519
+ );
520
+
521
+ const details = getLatestCrashArtifact({
522
+ crashArtifacts: recentCrashArtifacts,
523
+ recentLogLines,
524
+ ...options,
525
+ });
526
+
527
+ if (!details) {
528
+ return null;
529
+ }
530
+
531
+ return persistCrashArtifact({
532
+ details,
533
+ crashArtifactWriter,
534
+ });
535
+ },
536
+ } satisfies AndroidAppMonitor;
537
+ };
538
+
539
+ export { createAndroidLogEvent };
540
+ export type AndroidAppMonitor = AppMonitor & {
541
+ getCrashDetails: (
542
+ options: CrashDetailsLookupOptions
543
+ ) => Promise<AppCrashDetails | null>;
544
+ };
package/src/config.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import { z } from 'zod';
2
2
 
3
+ export const AndroidAppLaunchOptionsSchema = z.object({
4
+ extras: z
5
+ .record(z.union([z.string(), z.boolean(), z.number().int().safe()]))
6
+ .optional(),
7
+ });
8
+
3
9
  export const AndroidEmulatorAVDConfigSchema = z.object({
4
10
  apiLevel: z.number().min(1, 'API level is required'),
5
11
  profile: z.string().min(1, 'Profile is required'),
@@ -32,12 +38,16 @@ export const AndroidPlatformConfigSchema = z.object({
32
38
  .string()
33
39
  .min(1, 'Activity name is required')
34
40
  .default('.MainActivity'),
41
+ appLaunchOptions: AndroidAppLaunchOptionsSchema.optional(),
35
42
  });
36
43
 
37
44
  export type AndroidEmulator = z.infer<typeof AndroidEmulatorSchema>;
38
45
  export type PhysicalAndroidDevice = z.infer<typeof PhysicalAndroidDeviceSchema>;
39
46
  export type AndroidDevice = z.infer<typeof AndroidDeviceSchema>;
40
47
  export type AndroidPlatformConfig = z.infer<typeof AndroidPlatformConfigSchema>;
48
+ export type AndroidAppLaunchOptions = z.infer<
49
+ typeof AndroidAppLaunchOptionsSchema
50
+ >;
41
51
  export type AndroidEmulatorAVDConfig = z.infer<
42
52
  typeof AndroidEmulatorAVDConfigSchema
43
53
  >;
@@ -0,0 +1,66 @@
1
+ import type { AppCrashDetails } from '@react-native-harness/platforms';
2
+ import { escapeRegExp } from '@react-native-harness/tools';
3
+
4
+ type ParseAndroidCrashReportOptions = {
5
+ contents: string;
6
+ bundleId: string;
7
+ pid?: number;
8
+ };
9
+
10
+ const getSignal = (contents: string) => {
11
+ const namedSignalMatch = contents.match(/\b(SIG[A-Z0-9]+)\b/);
12
+
13
+ if (namedSignalMatch) {
14
+ return namedSignalMatch[1];
15
+ }
16
+
17
+ const signalNumberMatch = contents.match(/signal\s+(\d+)/i);
18
+
19
+ if (signalNumberMatch) {
20
+ return `signal ${signalNumberMatch[1]}`;
21
+ }
22
+
23
+ return undefined;
24
+ };
25
+
26
+ const getStackTrace = (rawLines: string[]) => {
27
+ const frames = rawLines.filter((line) =>
28
+ /^\S.*(?:\s+at\s+|\s+#\d+\s+pc\s+)/.test(line.trim()) ||
29
+ /^\S.*AndroidRuntime:\s+at\s+/.test(line.trim()) ||
30
+ /^\S.*AndroidRuntime:\s+Caused by:/.test(line.trim())
31
+ );
32
+
33
+ return frames.length > 0 ? frames : undefined;
34
+ };
35
+
36
+ export const androidCrashParser = {
37
+ parse({
38
+ contents,
39
+ bundleId,
40
+ pid,
41
+ }: ParseAndroidCrashReportOptions): AppCrashDetails {
42
+ const rawLines = contents.split(/\r?\n/);
43
+ const processPattern = new RegExp(
44
+ `Process:\\s*${escapeRegExp(bundleId)},\\s*PID:\\s*(\\d+)`
45
+ );
46
+ const fatalExceptionMatch = contents.match(/FATAL EXCEPTION:\s*(.+)$/im);
47
+ const processMatch = contents.match(processPattern);
48
+ const runtimeExceptionLine = rawLines.find((line) =>
49
+ /AndroidRuntime: (?:java\.|kotlin\.|[\w$.]+(?:Exception|Error):)/.test(line)
50
+ );
51
+ const exceptionType =
52
+ fatalExceptionMatch?.[1]?.trim() ??
53
+ runtimeExceptionLine?.match(/AndroidRuntime:\s+(.+)$/)?.[1]?.trim();
54
+
55
+ return {
56
+ source: 'logs',
57
+ summary: contents.trim(),
58
+ signal: getSignal(contents),
59
+ exceptionType,
60
+ processName: processMatch ? bundleId : contents.includes(bundleId) ? bundleId : undefined,
61
+ pid: pid ?? (processMatch ? Number(processMatch[1]) : undefined),
62
+ rawLines,
63
+ stackTrace: getStackTrace(rawLines),
64
+ };
65
+ },
66
+ };