public async runApp()

in src/extension/android/androidPlatform.ts [131:232]


    public async runApp(shouldLaunchInAllDevices: boolean = false): Promise<void> {
        let extProps: any = {
            platform: {
                value: PlatformType.Android,
                isPii: false,
            },
        };

        if (this.runOptions.isDirect) {
            extProps.isDirect = {
                value: true,
                isPii: false,
            };
            this.projectObserver?.updateRNAndroidHermesProjectState(true);
        }

        extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
            this.runOptions,
            this.runOptions.reactNativeVersions,
            extProps,
        );

        await TelemetryHelper.generate("AndroidPlatform.runApp", extProps, async () => {
            const env = GeneralMobilePlatform.getEnvArgument(
                process.env,
                this.runOptions.env,
                this.runOptions.envFile,
            );

            if (
                !semver.valid(
                    this.runOptions.reactNativeVersions.reactNativeVersion,
                ) /* Custom RN implementations should support this flag*/ ||
                semver.gte(
                    this.runOptions.reactNativeVersions.reactNativeVersion,
                    AndroidPlatform.NO_PACKAGER_VERSION,
                )
            ) {
                this.runArguments.push("--no-packager");
            }

            const mainActivity = GeneralMobilePlatform.getOptFromRunArgs(
                this.runArguments,
                "--main-activity",
            );

            if (mainActivity) {
                this.adbHelper.setLaunchActivity(mainActivity);
            } else if (notNullOrUndefined(this.runOptions.debugLaunchActivity)) {
                this.runArguments.push("--main-activity", this.runOptions.debugLaunchActivity);
                this.adbHelper.setLaunchActivity(this.runOptions.debugLaunchActivity);
            }

            const runAndroidSpawn = new CommandExecutor(
                this.runOptions.nodeModulesRoot,
                this.projectPath,
                this.logger,
            ).spawnReactCommand("run-android", this.runArguments, { env });
            const output = new OutputVerifier(
                () => Promise.resolve(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS),
                () => Promise.resolve(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS),
                PlatformType.Android,
            ).process(runAndroidSpawn);

            let devicesIdsForLaunch: string[] = [];
            const onlineTargetsIds = (await this.adbHelper.getOnlineTargets()).map(
                target => target.id,
            );
            let targetId: string | undefined;
            try {
                try {
                    await output;
                } finally {
                    targetId = await this.getTargetIdForRunApp(onlineTargetsIds);
                    this.packageName = await this.getPackageName();
                    devicesIdsForLaunch = [targetId];
                }
            } catch (error) {
                if (!targetId) {
                    targetId = await this.getTargetIdForRunApp(onlineTargetsIds);
                }
                if (
                    error.message ===
                        ErrorHelper.getInternalError(
                            InternalErrorCode.AndroidMoreThanOneDeviceOrEmulator,
                        ).message &&
                    onlineTargetsIds.length >= 1 &&
                    targetId
                ) {
                    /* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */
                    this.needsToLaunchApps = true;
                    devicesIdsForLaunch = shouldLaunchInAllDevices ? onlineTargetsIds : [targetId];
                } else {
                    throw error;
                }
            }

            await PromiseUtil.forEach(devicesIdsForLaunch, deviceId =>
                this.launchAppWithADBReverseAndLogCat(deviceId),
            );
        });
    }