在WSL中运行React Native,并在Windows中直接运行模拟器 [英] Running React Native in WSL with the emulator running directly in Windows
问题描述
我已经有一段时间没有进行Android开发了,所以我对现代Android开发的了解参差不齐.
I haven't done Android development in a while, so my knowledge of modern Android development is spotty.
我正在尝试学习React Native.我使用WSL作为主要开发环境.由于在Linux/WSL中设置Android仿真器可能会很痛苦,因此我想直接在Windows上运行仿真器.但是,我希望能够在WSL中运行react-native run-android
.
I'm trying to learn React Native. I use WSL as my primary development environment. Since it'll probably be a pain to set up the Android emulator in Linux/WSL, I want to run the emulator directly on Windows. However, I'd prefer to be able to run react-native run-android
in WSL.
我将如何设置React Native使其能够在WSL中运行react-native run-android
并在Windows上的Android模拟器中运行该应用程序?
How would I set up React Native to be able to run react-native run-android
in WSL and run the app in an Android emulator on Windows?
我的ANDROID_HOME
变量应该正确设置,但是我对Android的了解还不足以知道它是否在做任何事情:
My ANDROID_HOME
variable should be set up correctly, but I don't know enough about Android to know if this is doing anything:
export ANDROID_HOME=/mnt/c/Users/Leo/AppData/Local/Android/sdk
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
推荐答案
AFAIK由于许多问题,无法在WSL中使用react-native.
AFAIK it is not possible to use react-native in WSL due to a number of issues.
例如 https://github.com/Microsoft/BashOnWindows/issues/1523
但是,我发现了一种变通方法,该方法将WWindows中的本机Windows android构建与npm堆栈结合在一起.实际上,您将要安装本机Windows Android Studio/SDK以使用Intellij IDE和仿真器.技巧是将基于Gradle的Android编译器分离出来.
However, I've found a workaround that combines a native windows android build with a npm stack in WSL. Realistically, you'll want to install the native Windows Android Studio/SDK to use the Intellij IDE and the emulator anyway. The trick is to separate out the Gradle based Android compile.
工作流程
使用npm/yarn在WSL中执行的所有项目设置和程序包管理. WSL中全局安装的react-native-cli.不需要重复的Windows二进制节点/npm堆栈.
All project setup and package management performed in WSL with npm/yarn. react-native-cli installed globally in WSL. No need for a duplicate windows binary node/npm stack.
不要使用react-native run-android
,而是从cmd进行编译和部署.在项目内部的/android
目录中,执行gradle包装器gradlew.bat installDebug
,或者使用创建者的更新,您可以从WSL bash shell /mnt/c/Windows/System32/cmd.exe /C gradlew.bat installDebug
内部执行此操作.不要使用unix gradlew脚本.
Don't use react-native run-android
, instead compile and deploy from cmd. From the /android
directory inside your project, execute the gradle wrapper gradlew.bat installDebug
, or with the Creator's Update, you can do this from inside the WSL bash shell /mnt/c/Windows/System32/cmd.exe /C gradlew.bat installDebug
. Don't use the unix gradlew script.
将APK组装好并上传到设备或仿真器后,请使用命令react-native start
在WSL中运行调试服务器.
Once the APK has been assembled and uploaded to your device or emulator, run the debug server from within WSL using the command react-native start
.
我已经使用多个本地Android组件在一些相当复杂的项目中对此进行了测试.希望这会有所帮助.
I've tested this out with some fairly complex projects using multiple native Android components. Hope this helps.
- 注意:要使其正常工作,您的项目需要位于本机Windows文件系统中,即/mnt/c.
这篇关于在WSL中运行React Native,并在Windows中直接运行模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!