react-native启动项目报错error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.

问题:

报错如下:

$ npx react-native run-android

info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 864 file(s) to forward-jetify. Using 8 workers...
info Starting JS server...
/bin/sh: adb: command not found
info Launching emulator...
error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.
warn Please launch an emulator manually or connect a device. Otherwise app may fail to launch.
info Installing the app...
Starting a Gradle Daemon, 5 stopped Daemons could not be reused, use --status for details

> Task :app:stripDebugDebugSymbols UP-TO-DATE
Compatible side by side NDK version was not found.

> Task :app:installDebug

> Task :app:installDebug FAILED
27 actionable tasks: 9 executed, 18 up-to-date
[adb]: * daemon not running; starting now at tcp:5037
[adb]: * daemon started successfully

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:installDebug'.
> com.android.builder.testing.api.DeviceException: No connected devices!

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 22s

error Failed to install the app. Make sure you have an Android emulator running or a device connected. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
[adb]: * daemon not running; starting now at tcp:5037
[adb]: * daemon started successfully

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:installDebug'.
> com.android.builder.testing.api.DeviceException: No connected devices!

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 22s

    at checkExecSyncError (child_process.js:611:11)
    at execFileSync (child_process.js:629:15)
    at runOnAllDevices (/Users/myname/AwesomeProject/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:94:39)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

报错重点:

error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.
warn Please launch an emulator manually or connect a device. Otherwise app may fail to launch.

解决:

原因是未添加到环境变量,导致找不到模拟器路径,配置步骤如下:
打开“高级系统设置” - “环境变量”,在“系统变量”中 点击 新建,增加以下内容:
变量名:

ANDROID_HOME

变量值:

C:\Users\myName\AppData\Local\Android\Sdk

之后在“系统变量”中,找到 Path,点击 编辑,之后点击 新建,增加以下内容:

C:\Users\myName\AppData\Local\Android\Sdk\platform-tools
%ANDROID_HOME%\emulator
%ANDROID_HOME%\tools\bin

之后重新启动模拟器和项目即可。

posted @ 2023-01-18 17:22  ZerlinM  阅读(2746)  评论(0编辑  收藏  举报