From 377030dce92d614eab798c7d56b32ecb96c12c17 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 24 Mar 2021 23:19:47 +0000 Subject: [PATCH] feat: add option to build only active architectures on android --- .../src/commands/runAndroid/adb.ts | 22 +++++++++++++++++++ .../src/commands/runAndroid/index.ts | 7 ++++++ .../commands/runAndroid/runOnAllDevices.ts | 14 ++++++++++++ 3 files changed, 43 insertions(+) diff --git a/packages/platform-android/src/commands/runAndroid/adb.ts b/packages/platform-android/src/commands/runAndroid/adb.ts index 46c82ec0f..b628ac0ad 100644 --- a/packages/platform-android/src/commands/runAndroid/adb.ts +++ b/packages/platform-android/src/commands/runAndroid/adb.ts @@ -67,7 +67,29 @@ function getAvailableCPUs(adbPath: string, device: string): Array { } } +/** + * Gets the CPU architecture of a device from ADB + */ +function getCPU(adbPath: string, device: string): string | null { + try { + const cpus = execFileSync(adbPath, [ + '-s', + device, + 'shell', + 'getprop', + 'ro.product.cpu.abi', + ]) + .toString() + .trim(); + + return cpus.length > 0 ? cpus : null; + } catch (e) { + return null; + } +} + export default { getDevices, getAvailableCPUs, + getCPU, }; diff --git a/packages/platform-android/src/commands/runAndroid/index.ts b/packages/platform-android/src/commands/runAndroid/index.ts index 29f351d77..37631a720 100644 --- a/packages/platform-android/src/commands/runAndroid/index.ts +++ b/packages/platform-android/src/commands/runAndroid/index.ts @@ -51,6 +51,7 @@ export interface Flags { port: number; terminal: string; jetifier: boolean; + activeArchOnly: boolean; } type AndroidProject = NonNullable; @@ -380,5 +381,11 @@ export default { description: 'Do not run "jetifier" – the AndroidX transition tool. By default it runs before Gradle to ease working with libraries that don\'t support AndroidX yet. See more at: https://www.npmjs.com/package/jetifier.', }, + { + name: '--active-arch-only', + description: + 'Build native libraries only for the current device architecture for debug builds.', + default: false, + }, ], }; diff --git a/packages/platform-android/src/commands/runAndroid/runOnAllDevices.ts b/packages/platform-android/src/commands/runAndroid/runOnAllDevices.ts index 2a968fc72..96b28ca28 100644 --- a/packages/platform-android/src/commands/runAndroid/runOnAllDevices.ts +++ b/packages/platform-android/src/commands/runAndroid/runOnAllDevices.ts @@ -63,6 +63,20 @@ async function runOnAllDevices( gradleArgs.push('-PreactNativeDevServerPort=' + args.port); } + if (args.activeArchOnly) { + const architectures = devices + .map((device) => { + return adb.getCPU(adbPath, device); + }) + .filter((arch) => arch != null); + if (architectures.length > 0) { + logger.info(`Detected architectures ${architectures.join(', ')}`); + gradleArgs.push( + '-PreactNativeDebugArchitectures=' + architectures.join(','), + ); + } + } + logger.info('Installing the app...'); logger.debug( `Running command "cd android && ${cmd} ${gradleArgs.join(' ')}"`,