@react-native-harness/platform-android 1.4.0-rc.1 → 1.4.0

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 (42) hide show
  1. package/dist/__tests__/adb-wait.test.d.ts +2 -0
  2. package/dist/__tests__/adb-wait.test.d.ts.map +1 -0
  3. package/dist/__tests__/adb-wait.test.js +69 -0
  4. package/dist/__tests__/app-session.test.d.ts +2 -0
  5. package/dist/__tests__/app-session.test.d.ts.map +1 -0
  6. package/dist/__tests__/app-session.test.js +57 -0
  7. package/dist/__tests__/avd-config.test.js +58 -0
  8. package/dist/__tests__/emulator-startup.test.js +30 -3
  9. package/dist/__tests__/instance.test.js +10 -4
  10. package/dist/adb.d.ts +3 -1
  11. package/dist/adb.d.ts.map +1 -1
  12. package/dist/adb.js +30 -21
  13. package/dist/app-session.d.ts +11 -2
  14. package/dist/app-session.d.ts.map +1 -1
  15. package/dist/app-session.js +17 -12
  16. package/dist/avd-config.d.ts +1 -0
  17. package/dist/avd-config.d.ts.map +1 -1
  18. package/dist/avd-config.js +11 -0
  19. package/dist/emulator-startup.d.ts +23 -0
  20. package/dist/emulator-startup.d.ts.map +1 -1
  21. package/dist/emulator-startup.js +58 -0
  22. package/dist/index.d.ts +1 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +1 -0
  25. package/dist/instance.d.ts.map +1 -1
  26. package/dist/instance.js +21 -15
  27. package/dist/runner.d.ts +2 -2
  28. package/dist/runner.d.ts.map +1 -1
  29. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  30. package/package.json +4 -4
  31. package/src/__tests__/adb-wait.test.ts +95 -0
  32. package/src/__tests__/app-session.test.ts +76 -0
  33. package/src/__tests__/avd-config.test.ts +70 -0
  34. package/src/__tests__/emulator-startup.test.ts +53 -6
  35. package/src/__tests__/instance.test.ts +107 -108
  36. package/src/adb.ts +72 -65
  37. package/src/app-session.ts +28 -12
  38. package/src/avd-config.ts +17 -0
  39. package/src/emulator-startup.ts +74 -0
  40. package/src/index.ts +1 -0
  41. package/src/instance.ts +30 -16
  42. package/src/runner.ts +5 -9
@@ -1,9 +1,24 @@
1
+ import { getDeviceCoreBudget, logger, spawn, SubprocessError, } from '@react-native-harness/tools';
2
+ import os from 'node:os';
3
+ import { getEmulatorBinaryPath } from './environment.js';
4
+ const emulatorStartupLogger = logger.child('android-emulator-startup');
5
+ /**
6
+ * Returns the vCPU count baked into an AVD for this runner.
7
+ *
8
+ * AVD snapshots require an identical hardware configuration to load, so all
9
+ * AVD creation, compatibility, and cache-key callers must use this value on
10
+ * the same host.
11
+ */
12
+ export const getEmulatorCpuCores = () => {
13
+ return getDeviceCoreBudget(os.availableParallelism());
14
+ };
1
15
  const COMMON_EMULATOR_ARGS = [
2
16
  '-no-window',
3
17
  '-gpu',
4
18
  'swiftshader_indirect',
5
19
  '-noaudio',
6
20
  '-no-boot-anim',
21
+ '-no-metrics',
7
22
  ];
8
23
  export const getEmulatorStartupArgs = (name, mode) => {
9
24
  const modeArgs = mode === 'clean-snapshot-generation'
@@ -13,3 +28,46 @@ export const getEmulatorStartupArgs = (name, mode) => {
13
28
  : ['-no-snapshot-load', '-no-snapshot-save'];
14
29
  return [`@${name}`, ...modeArgs, ...COMMON_EMULATOR_ARGS];
15
30
  };
31
+ /**
32
+ * Parses the output of `emulator -accel-check` to determine whether
33
+ * hardware acceleration (KVM on Linux, HVF on macOS) is usable. The
34
+ * emulator prints a message containing "is installed and usable" when
35
+ * acceleration is available.
36
+ */
37
+ export const isAccelerationUsable = (accelCheckOutput) => {
38
+ return accelCheckOutput.includes('is installed and usable');
39
+ };
40
+ /**
41
+ * Runs `emulator -accel-check` and logs a non-fatal warning when hardware
42
+ * acceleration (KVM/HVF) appears unavailable, in which case the emulator
43
+ * falls back to very slow software emulation. This check must never fail or
44
+ * block emulator startup - any error running it is swallowed and only
45
+ * debug-logged.
46
+ */
47
+ export const warnIfEmulatorAccelerationUnavailable = async () => {
48
+ let combinedOutput;
49
+ try {
50
+ const emulatorBinaryPath = getEmulatorBinaryPath();
51
+ const { stdout, stderr } = await spawn(emulatorBinaryPath, [
52
+ '-accel-check',
53
+ ]);
54
+ combinedOutput = `${stdout}\n${stderr}`;
55
+ }
56
+ catch (error) {
57
+ if (error instanceof SubprocessError) {
58
+ // The check itself ran but exited non-zero - this is how
59
+ // `-accel-check` reports that acceleration is unavailable, so treat
60
+ // its output as a normal (non-fatal) result rather than swallowing it.
61
+ combinedOutput = `${error.stdout}\n${error.stderr}`;
62
+ }
63
+ else {
64
+ // Failed to even run the check (e.g. binary missing). This must never
65
+ // fail or block emulator startup.
66
+ emulatorStartupLogger.debug('Failed to run "emulator -accel-check": %o', error);
67
+ return;
68
+ }
69
+ }
70
+ if (!isAccelerationUsable(combinedOutput)) {
71
+ logger.warn('Hardware acceleration (KVM/HVF) appears unavailable for the Android emulator. It will fall back to very slow software emulation.');
72
+ }
73
+ };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export { androidEmulator, physicalAndroidDevice, androidPlatform, } from './fact
2
2
  export type { AndroidPlatformConfig } from './config.js';
3
3
  export { getNormalizedAvdCacheConfig, resolveAvdCachingEnabled, } from './avd-config.js';
4
4
  export { getHostAndroidSystemImageArch } from './environment.js';
5
+ export { getEmulatorCpuCores } from './emulator-startup.js';
5
6
  export { HarnessAppPathError, HarnessEmulatorConfigError } from './errors.js';
6
7
  export { getRunTargets } from './targets.js';
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,eAAe,GAChB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EACL,2BAA2B,EAC3B,wBAAwB,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,eAAe,GAChB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EACL,2BAA2B,EAC3B,wBAAwB,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { androidEmulator, physicalAndroidDevice, androidPlatform, } from './factory.js';
2
2
  export { getNormalizedAvdCacheConfig, resolveAvdCachingEnabled, } from './avd-config.js';
3
3
  export { getHostAndroidSystemImageArch } from './environment.js';
4
+ export { getEmulatorCpuCores } from './emulator-startup.js';
4
5
  export { HarnessAppPathError, HarnessEmulatorConfigError } from './errors.js';
5
6
  export { getRunTargets } from './targets.js';
@@ -1 +1 @@
1
- {"version":3,"file":"instance.d.ts","sourceRoot":"","sources":["../src/instance.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,0BAA0B,EAC/B,qBAAqB,EACtB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAE5E,OAAO,EACL,qBAAqB,EAGtB,MAAM,aAAa,CAAC;AAuKrB,eAAO,MAAM,kCAAkC,GAC7C,QAAQ,qBAAqB,EAC7B,eAAe,aAAa,EAC5B,MAAM,0BAA0B,KAC/B,OAAO,CAAC,qBAAqB,CA8H/B,CAAC;AAEF,eAAO,MAAM,wCAAwC,GACnD,QAAQ,qBAAqB,EAC7B,eAAe,aAAa,EAC5B,OAAO,0BAA0B,KAChC,OAAO,CAAC,qBAAqB,CAyD/B,CAAC"}
1
+ {"version":3,"file":"instance.d.ts","sourceRoot":"","sources":["../src/instance.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,0BAA0B,EAC/B,qBAAqB,EACtB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAE5E,OAAO,EACL,qBAAqB,EAGtB,MAAM,aAAa,CAAC;AAyKrB,eAAO,MAAM,kCAAkC,GAC7C,QAAQ,qBAAqB,EAC7B,eAAe,aAAa,EAC5B,MAAM,0BAA0B,KAC/B,OAAO,CAAC,qBAAqB,CAoI/B,CAAC;AAEF,eAAO,MAAM,wCAAwC,GACnD,QAAQ,qBAAqB,EAC7B,eAAe,aAAa,EAC5B,OAAO,0BAA0B,KAChC,OAAO,CAAC,qBAAqB,CA+D/B,CAAC"}
package/dist/instance.js CHANGED
@@ -1,13 +1,14 @@
1
1
  import { AppNotInstalledError, DeviceNotFoundError, } from '@react-native-harness/platforms';
2
- import { logger } from '@react-native-harness/tools';
2
+ import { instrumented, logger, noopDiagnostics } from '@react-native-harness/tools';
3
3
  import { assertAndroidDeviceEmulator, assertAndroidDevicePhysical, } from './config.js';
4
4
  import { isAvdCompatible, readAvdConfig, resolveAvdCachingEnabled, } from './avd-config.js';
5
5
  import { getAdbId } from './adb-id.js';
6
- import * as adb from './adb.js';
6
+ import * as adbModule from './adb.js';
7
7
  import { applyHarnessDebugHttpHost, clearHarnessDebugHttpHost, } from './shared-prefs.js';
8
8
  import { getDeviceName } from './utils.js';
9
9
  import { HarnessAppPathError, HarnessEmulatorConfigError } from './errors.js';
10
10
  import { ensureAndroidEmulatorAvailable, ensureAndroidEmulatorEnvironment, getHostAndroidSystemImageArch, } from './environment.js';
11
+ import { warnIfEmulatorAccelerationUnavailable } from './emulator-startup.js';
11
12
  import { isInteractive } from '@react-native-harness/tools';
12
13
  import fs from 'node:fs';
13
14
  import { createAndroidAppSession } from './app-session.js';
@@ -35,23 +36,24 @@ const getOptionalHarnessAppPath = () => {
35
36
  const configureAndroidRuntime = async (adbId, config, harnessConfig) => {
36
37
  const metroPort = harnessConfig.metroPort;
37
38
  await Promise.all([
38
- adb.reversePort(adbId, metroPort),
39
- adb.reversePort(adbId, 8080),
40
- adb.setHideErrorDialogs(adbId, true),
39
+ adbModule.reversePort(adbId, metroPort),
40
+ adbModule.reversePort(adbId, 8080),
41
+ adbModule.setHideErrorDialogs(adbId, true),
41
42
  applyHarnessDebugHttpHost(adbId, config.bundleId, `localhost:${metroPort}`),
42
43
  ]);
43
- return adb.getAppUid(adbId, config.bundleId);
44
+ return adbModule.getAppUid(adbId, config.bundleId);
44
45
  };
45
46
  const startAndWaitForBoot = async ({ emulatorName, signal, mode, }) => {
46
47
  await ensureAndroidEmulatorAvailable();
47
- await adb.startEmulator(emulatorName, mode);
48
- return adb.waitForBoot(emulatorName, signal);
48
+ await warnIfEmulatorAccelerationUnavailable();
49
+ await adbModule.startEmulator(emulatorName, mode);
50
+ return adbModule.waitForBoot(emulatorName, signal);
49
51
  };
50
52
  const recreateAvd = async ({ emulatorConfig, }) => {
51
53
  if (!emulatorConfig.avd) {
52
54
  throw new HarnessEmulatorConfigError(emulatorConfig.name);
53
55
  }
54
- await adb.createAvd({
56
+ await adbModule.createAvd({
55
57
  name: emulatorConfig.name,
56
58
  apiLevel: emulatorConfig.avd.apiLevel,
57
59
  profile: emulatorConfig.avd.profile,
@@ -62,7 +64,7 @@ const recreateAvd = async ({ emulatorConfig, }) => {
62
64
  const prepareCachedAvd = async ({ emulatorConfig, signal, }) => {
63
65
  const emulatorName = emulatorConfig.name;
64
66
  const hostArch = getHostAndroidSystemImageArch();
65
- const hasExistingAvd = await adb.hasAvd(emulatorName);
67
+ const hasExistingAvd = await adbModule.hasAvd(emulatorName);
66
68
  const avdConfig = hasExistingAvd ? await readAvdConfig(emulatorName) : null;
67
69
  const compatibility = avdConfig == null
68
70
  ? { compatible: false, reason: 'Missing AVD config.ini.' }
@@ -77,7 +79,7 @@ const prepareCachedAvd = async ({ emulatorConfig, signal, }) => {
77
79
  : 'Creating Android emulator %s...', emulatorName);
78
80
  if (hasExistingAvd && !compatibility.compatible) {
79
81
  androidInstanceLogger.debug('Android AVD %s is not reusable: %s', emulatorName, compatibility.reason);
80
- await adb.deleteAvd(emulatorName);
82
+ await adbModule.deleteAvd(emulatorName);
81
83
  }
82
84
  await recreateAvd({ emulatorConfig });
83
85
  const generationAdbId = await startAndWaitForBoot({
@@ -86,8 +88,8 @@ const prepareCachedAvd = async ({ emulatorConfig, signal, }) => {
86
88
  mode: 'clean-snapshot-generation',
87
89
  });
88
90
  logger.info('Saving Android emulator snapshot for %s...', emulatorName);
89
- await adb.stopEmulator(generationAdbId);
90
- await adb.waitForEmulatorDisconnect(generationAdbId, signal);
91
+ await adbModule.stopEmulator(generationAdbId);
92
+ await adbModule.waitForEmulatorDisconnect(generationAdbId, signal);
91
93
  }
92
94
  else {
93
95
  logger.info('Using cached Android emulator %s...', emulatorName);
@@ -100,6 +102,7 @@ const prepareCachedAvd = async ({ emulatorConfig, signal, }) => {
100
102
  };
101
103
  export const getAndroidEmulatorPlatformInstance = async (config, harnessConfig, init) => {
102
104
  assertAndroidDeviceEmulator(config.device);
105
+ const adb = instrumented(adbModule, 'platform.android.adb', init.diagnostics ?? noopDiagnostics);
103
106
  const permissionsEnabled = harnessConfig.permissions ?? false;
104
107
  const emulatorConfig = config.device;
105
108
  const emulatorName = emulatorConfig.name;
@@ -169,10 +172,11 @@ export const getAndroidEmulatorPlatformInstance = async (config, harnessConfig,
169
172
  stopApp: () => adb.stopApp(adbId, config.bundleId),
170
173
  getAppPid: () => adb.getAppPid(adbId, config.bundleId),
171
174
  getLogcatTimestamp: () => adb.getLogcatTimestamp(adbId),
172
- startLogcat: (args) => adb.startLogcat(adbId, args),
175
+ startLogcat: (args, options) => adb.startLogcat(adbId, args, options),
173
176
  getDropboxOutput: () => adb.getDropboxPrint(adbId),
174
177
  getExitInfo: () => adb.getActivityExitInfo(adbId, config.bundleId),
175
178
  crashArtifactWriter: init.crashArtifactWriter,
179
+ signal: init.signal,
176
180
  });
177
181
  },
178
182
  dispose: async () => {
@@ -188,6 +192,7 @@ export const getAndroidEmulatorPlatformInstance = async (config, harnessConfig,
188
192
  };
189
193
  export const getAndroidPhysicalDevicePlatformInstance = async (config, harnessConfig, init) => {
190
194
  assertAndroidDevicePhysical(config.device);
195
+ const adb = instrumented(adbModule, 'platform.android.adb', init?.diagnostics ?? noopDiagnostics);
191
196
  const permissionsEnabled = harnessConfig.permissions ?? false;
192
197
  const adbId = await getAdbId(config.device);
193
198
  if (!adbId) {
@@ -213,10 +218,11 @@ export const getAndroidPhysicalDevicePlatformInstance = async (config, harnessCo
213
218
  stopApp: () => adb.stopApp(adbId, config.bundleId),
214
219
  getAppPid: () => adb.getAppPid(adbId, config.bundleId),
215
220
  getLogcatTimestamp: () => adb.getLogcatTimestamp(adbId),
216
- startLogcat: (args) => adb.startLogcat(adbId, args),
221
+ startLogcat: (args, options) => adb.startLogcat(adbId, args, options),
217
222
  getDropboxOutput: () => adb.getDropboxPrint(adbId),
218
223
  getExitInfo: () => adb.getActivityExitInfo(adbId, config.bundleId),
219
224
  crashArtifactWriter: init?.crashArtifactWriter,
225
+ signal: init?.signal,
220
226
  });
221
227
  },
222
228
  dispose: async () => {
package/dist/runner.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { HarnessPlatformRunner, type HarnessPlatformInitOptions } from '@react-native-harness/platforms';
1
+ import { type HarnessPlatformRunnerFactory } from '@react-native-harness/platforms';
2
2
  import type { Config as HarnessConfig } from '@react-native-harness/config';
3
3
  import { type AndroidPlatformConfig } from './config.js';
4
- declare const getAndroidRunner: (config: AndroidPlatformConfig, harnessConfig: HarnessConfig, init: HarnessPlatformInitOptions) => Promise<HarnessPlatformRunner>;
4
+ declare const getAndroidRunner: HarnessPlatformRunnerFactory<AndroidPlatformConfig, HarnessConfig>;
5
5
  export default getAndroidRunner;
6
6
  //# sourceMappingURL=runner.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,KAAK,0BAA0B,EAChC,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC5E,OAAO,EAEL,KAAK,qBAAqB,EAE3B,MAAM,aAAa,CAAC;AAUrB,QAAA,MAAM,gBAAgB,GACpB,QAAQ,qBAAqB,EAC7B,eAAe,aAAa,EAC5B,MAAM,0BAA0B,KAC/B,OAAO,CAAC,qBAAqB,CAoB/B,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
1
+ {"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,4BAA4B,EAAE,MAAM,iCAAiC,CAAC;AACpF,OAAO,KAAK,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC5E,OAAO,EAEL,KAAK,qBAAqB,EAE3B,MAAM,aAAa,CAAC;AAUrB,QAAA,MAAM,gBAAgB,EAAE,4BAA4B,CAClD,qBAAqB,EACrB,aAAa,CAqBd,CAAC;AAEF,eAAe,gBAAgB,CAAC"}