diff --git a/packages/cli-platform-android/README.md b/packages/cli-platform-android/README.md index a6148000e..a75081740 100644 --- a/packages/cli-platform-android/README.md +++ b/packages/cli-platform-android/README.md @@ -73,6 +73,14 @@ Build native libraries only for the current device architecture for debug builds List all available Android devices and simulators and let you choose one to run the app. + +#### `--interactive` + +Manually select a task and device/simulator you want to run your app on. + +> [!WARNING] +> This flag is running `./gradlew tasks` under the hood, which might take some time for more complex apps. If that affects your project, consider using `--mode` and `--deviceId` flags instead. + ### `build-android` Usage: diff --git a/packages/cli-platform-android/src/commands/buildAndroid/index.ts b/packages/cli-platform-android/src/commands/buildAndroid/index.ts index 0f6674e67..2732b84d3 100644 --- a/packages/cli-platform-android/src/commands/buildAndroid/index.ts +++ b/packages/cli-platform-android/src/commands/buildAndroid/index.ts @@ -49,7 +49,6 @@ async function buildAndroid( args.mode, tasks, 'bundle', - androidProject.sourceDir, ); if (args.extraParams) { diff --git a/packages/cli-platform-android/src/commands/runAndroid/getTaskNames.ts b/packages/cli-platform-android/src/commands/runAndroid/getTaskNames.ts index 26b71dac4..23c022977 100644 --- a/packages/cli-platform-android/src/commands/runAndroid/getTaskNames.ts +++ b/packages/cli-platform-android/src/commands/runAndroid/getTaskNames.ts @@ -1,47 +1,15 @@ import {toPascalCase} from './toPascalCase'; import type {BuildFlags} from '../buildAndroid'; -import {getGradleTasks} from './listAndroidTasks'; -import {CLIError, logger} from '@react-native-community/cli-tools'; export function getTaskNames( appName: string, mode: BuildFlags['mode'] = 'debug', tasks: BuildFlags['tasks'], taskPrefix: 'assemble' | 'install' | 'bundle', - sourceDir: string, ): Array { - let appTasks = + const appTasks = tasks && tasks.length ? tasks : [taskPrefix + toPascalCase(mode)]; - // Check against build flavors for "install" task ("assemble" don't care about it so much and will build all) - if (!tasks?.length && taskPrefix === 'install') { - const actionableInstallTasks = getGradleTasks('install', sourceDir); - if (!actionableInstallTasks.find((t) => t.task.includes(appTasks[0]))) { - const installTasksForMode = actionableInstallTasks.filter((t) => - t.task.toLowerCase().includes(mode), - ); - if (!installTasksForMode.length) { - throw new CLIError( - `Couldn't find "${appTasks - .map((taskName) => taskName.replace(taskPrefix, '')) - .join( - ', ', - )}" build variant. Available variants are: ${actionableInstallTasks - .map((t) => `"${t.task.replace(taskPrefix, '')}"`) - .join(', ')}.`, - ); - } - logger.warn( - `Found multiple tasks for "install" command: ${installTasksForMode - .map((t) => t.task) - .join(', ')}.\nSelecting first available: ${ - installTasksForMode[0].task - }.`, - ); - appTasks = [installTasksForMode[0].task]; - } - } - return appName ? appTasks.map((command) => `${appName}:${command}`) : appTasks; diff --git a/packages/cli-platform-android/src/commands/runAndroid/index.ts b/packages/cli-platform-android/src/commands/runAndroid/index.ts index ba8237919..63636d64b 100644 --- a/packages/cli-platform-android/src/commands/runAndroid/index.ts +++ b/packages/cli-platform-android/src/commands/runAndroid/index.ts @@ -222,7 +222,6 @@ function runOnSpecificDevice( args.mode, args.tasks ?? buildTask, 'install', - androidProject.sourceDir, ); // using '-x lint' in order to ignore linting errors while building the apk diff --git a/packages/cli-platform-android/src/commands/runAndroid/listAndroidTasks.ts b/packages/cli-platform-android/src/commands/runAndroid/listAndroidTasks.ts index 1c1353fc5..983b080b7 100644 --- a/packages/cli-platform-android/src/commands/runAndroid/listAndroidTasks.ts +++ b/packages/cli-platform-android/src/commands/runAndroid/listAndroidTasks.ts @@ -1,4 +1,4 @@ -import {CLIError, prompt} from '@react-native-community/cli-tools'; +import {CLIError, getLoader, prompt} from '@react-native-community/cli-tools'; import chalk from 'chalk'; import execa from 'execa'; @@ -31,12 +31,19 @@ export const getGradleTasks = ( taskType: 'install' | 'build', sourceDir: string, ) => { + const loader = getLoader(); + loader.start('Searching for available Gradle tasks...'); const cmd = process.platform.startsWith('win') ? 'gradlew.bat' : './gradlew'; - - const out = execa.sync(cmd, ['tasks', '--group', taskType], { - cwd: sourceDir, - }).stdout; - return parseTasksFromGradleFile(taskType, out); + try { + const out = execa.sync(cmd, ['tasks', '--group', taskType], { + cwd: sourceDir, + }).stdout; + loader.succeed(); + return parseTasksFromGradleFile(taskType, out); + } catch { + loader.fail(); + return []; + } }; export const promptForTaskSelection = async ( diff --git a/packages/cli-platform-android/src/commands/runAndroid/runOnAllDevices.ts b/packages/cli-platform-android/src/commands/runAndroid/runOnAllDevices.ts index 91ecccac1..80b5df0c6 100644 --- a/packages/cli-platform-android/src/commands/runAndroid/runOnAllDevices.ts +++ b/packages/cli-platform-android/src/commands/runAndroid/runOnAllDevices.ts @@ -55,7 +55,6 @@ async function runOnAllDevices( args.mode, args.tasks, 'install', - androidProject.sourceDir, ); if (args.extraParams) {