Skip to content

Commit 242c497

Browse files
perf: remove Gradle task check on run-android (#2144) (#2145)
* remove gradle task check on run-android * add loader and README note * throw error when no tasks found Co-authored-by: Tomasz Misiukiewicz <TMisiukiewicz@users.noreply.github.com>
1 parent 9d486c8 commit 242c497

File tree

6 files changed

+22
-42
lines changed

6 files changed

+22
-42
lines changed

packages/cli-platform-android/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ Build native libraries only for the current device architecture for debug builds
9595
9696
List all available Android devices and simulators and let you choose one to run the app.
9797

98+
99+
#### `--interactive`
100+
101+
Manually select a task and device/simulator you want to run your app on.
102+
103+
> [!WARNING]
104+
> 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.
105+
98106
### `build-android`
99107

100108
Usage:

packages/cli-platform-android/src/commands/buildAndroid/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ async function buildAndroid(
9292
args.mode || args.variant,
9393
tasks,
9494
'bundle',
95-
androidProject.sourceDir,
9695
);
9796

9897
if (args.extraParams) {

packages/cli-platform-android/src/commands/runAndroid/getTaskNames.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,15 @@
11
import {toPascalCase} from './toPascalCase';
22
import type {BuildFlags} from '../buildAndroid';
3-
import {getGradleTasks} from './listAndroidTasks';
4-
import {CLIError, logger} from '@react-native-community/cli-tools';
53

64
export function getTaskNames(
75
appName: string,
86
mode: BuildFlags['mode'] = 'debug',
97
tasks: BuildFlags['tasks'],
108
taskPrefix: 'assemble' | 'install' | 'bundle',
11-
sourceDir: string,
129
): Array<string> {
13-
let appTasks =
10+
const appTasks =
1411
tasks && tasks.length ? tasks : [taskPrefix + toPascalCase(mode)];
1512

16-
// Check against build flavors for "install" task ("assemble" don't care about it so much and will build all)
17-
if (!tasks?.length && taskPrefix === 'install') {
18-
const actionableInstallTasks = getGradleTasks('install', sourceDir);
19-
if (!actionableInstallTasks.find((t) => t.task.includes(appTasks[0]))) {
20-
const installTasksForMode = actionableInstallTasks.filter((t) =>
21-
t.task.toLowerCase().includes(mode),
22-
);
23-
if (!installTasksForMode.length) {
24-
throw new CLIError(
25-
`Couldn't find "${appTasks
26-
.map((taskName) => taskName.replace(taskPrefix, ''))
27-
.join(
28-
', ',
29-
)}" build variant. Available variants are: ${actionableInstallTasks
30-
.map((t) => `"${t.task.replace(taskPrefix, '')}"`)
31-
.join(', ')}.`,
32-
);
33-
}
34-
logger.warn(
35-
`Found multiple tasks for "install" command: ${installTasksForMode
36-
.map((t) => t.task)
37-
.join(', ')}.\nSelecting first available: ${
38-
installTasksForMode[0].task
39-
}.`,
40-
);
41-
appTasks = [installTasksForMode[0].task];
42-
}
43-
}
44-
4513
return appName
4614
? appTasks.map((command) => `${appName}:${command}`)
4715
: appTasks;

packages/cli-platform-android/src/commands/runAndroid/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ function runOnSpecificDevice(
191191
args.mode || args.variant,
192192
args.tasks ?? buildTask,
193193
'install',
194-
androidProject.sourceDir,
195194
);
196195

197196
// using '-x lint' in order to ignore linting errors while building the apk

packages/cli-platform-android/src/commands/runAndroid/listAndroidTasks.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {CLIError} from '@react-native-community/cli-tools';
1+
import {CLIError, getLoader} from '@react-native-community/cli-tools';
22
import chalk from 'chalk';
33
import execa from 'execa';
44
import prompts from 'prompts';
@@ -32,12 +32,19 @@ export const getGradleTasks = (
3232
taskType: 'install' | 'build',
3333
sourceDir: string,
3434
) => {
35+
const loader = getLoader();
36+
loader.start('Searching for available Gradle tasks...');
3537
const cmd = process.platform.startsWith('win') ? 'gradlew.bat' : './gradlew';
36-
37-
const out = execa.sync(cmd, ['tasks', '--group', taskType], {
38-
cwd: sourceDir,
39-
}).stdout;
40-
return parseTasksFromGradleFile(taskType, out);
38+
try {
39+
const out = execa.sync(cmd, ['tasks', '--group', taskType], {
40+
cwd: sourceDir,
41+
}).stdout;
42+
loader.succeed();
43+
return parseTasksFromGradleFile(taskType, out);
44+
} catch {
45+
loader.fail();
46+
return [];
47+
}
4148
};
4249

4350
export const promptForTaskSelection = async (

packages/cli-platform-android/src/commands/runAndroid/runOnAllDevices.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ async function runOnAllDevices(
5959
args.mode || args.variant,
6060
args.tasks,
6161
'install',
62-
androidProject.sourceDir,
6362
);
6463

6564
if (args.extraParams) {

0 commit comments

Comments
 (0)