npx react-native run-android 不会启动模拟器,如果模拟器正在运行,也不会运行

Posted

技术标签:

【中文标题】npx react-native run-android 不会启动模拟器,如果模拟器正在运行,也不会运行【英文标题】:npx react-native run-android doesn't start emulator and doesn't run if emulator is running 【发布时间】:2020-11-22 08:06:03 【问题描述】:

我使用 react-native 0.63 创建了一个新项目 当我在 ios 模拟器上运行时,一切正常 但是当尝试在android上运行它时,它不起作用并给我这个错误

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:installDebug'.
> SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at '/Volumes/SSD2/developer/MyProject/AwesomeProject/android/local.properties'.

* 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 2s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

我安装 react-native like react-native Setting up

这是我的 react-native 医生的输出

✓ Node.js
 ✓ npm
 ✓ Watchman - Used for watching changes in the filesystem when in development mode

Android
 ✓ JDK
 ✓ Android Studio - Required for building and installing your app on Android
 ✖ Android SDK - Required for building and installing your app on Android
   - Versions found: N/A
   - Version supported: 29.0.2
 ✖ ANDROID_HOME

iOS
 ✓ Xcode - Required for building and installing your app on iOS
 ✓ CocoaPods - Required for installing iOS dependencies
 ✓ ios-deploy - Required for installing your app on a physical device with the CLI

Errors:   2
Warnings: 0

如果我运行 npx react-native start 那么应用程序会从 android studio 顺利运行。

编辑 如果我从终端源运行项目 .bash_profile

模拟器启动但项目未运行

"--no-jetifier" flag.
Jetifier found 5194 file(s) to forward-jetify. Using 12 workers...
info JS server already running.
info Installing the app...
env: sh\r: No such file or directory

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
env: sh\r: No such file or directory

【问题讨论】:

【参考方案1】:

看来您需要设置 ANDROID_HOME 环境变量。

将以下行添加到您的 $HOME/.bash_profile 或 $HOME/.bashrc(如果您使用的是 zsh,则为 ~/.zprofile 或 ~/.zshrc)配置文件:

export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools

为 bash 键入 source $HOME/.bash_profile 或 source $HOME/.zprofile 将配置加载到您当前的 shell 中。通过运行 echo $ANDROID_HOME 验证 ANDROID_HOME 是否已设置,并且通过运行 echo $PATH 已将适当的目录添加到您的路径中。

请确保您使用正确的 Android SDK 路径。您可以在 Android Studio“首选项”对话框的外观和行为 → 系统设置 → Android SDK 下找到 SDK 的实际位置。

参考资料:

https://reactnative.dev/docs/environment-setup 3.配置ANDROID_HOME环境变量

【讨论】:

在终端运行`echo $ANDROID_HOME`会得到什么? 确保您在开头有美元符号,例如$ANDROID_HOME 这意味着它没有设置。如果您认为它已设置,请尝试打开一个新的终端窗口并再次运行它。这样,新窗口将以您设置的新环境变量开始。 好的,所以当你运行echo $ANDROID_HOME 它应该输出/Volumes/SSD2/developer/sdk ,如果不是,那么你需要运行source .bash_profile 或打开一个新的终端窗口。 您是否正在从新的终端窗口运行npx react-native run-android

以上是关于npx react-native run-android 不会启动模拟器,如果模拟器正在运行,也不会运行的主要内容,如果未能解决你的问题,请参考以下文章

无法在 react native CLI 中运行 npx react-native init MyApp

我已经按照 react-native 文档开始使用,但是在运行 npx react-native run-android 后我一直无法启动默认应用程序本身

npx react-native run-android:E/Device:同步期间出错:EOF

当我运行“npx react-native start”时找不到模块“@expo/metro-config”

npx react-native run-android 不会启动模拟器,如果模拟器正在运行,也不会运行

当我运行“npx react-native run-ad”时,新鲜的 React Native App 不会安装在模拟器上