使用直接在 Windows 中运行的模拟器在 WSL 中运行 React Native [英] Running React Native in WSL with the emulator running directly in Windows

查看:28
本文介绍了使用直接在 Windows 中运行的模拟器在 WSL 中运行 React Native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段时间没有做过 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

但是,我找到了一种解决方法,将原生 Windows android 构建与 WSL 中的 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.

工作流程

在 WSL 中使用 npm/yarn 执行所有项目设置和包管理.在 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,或者使用 Creator's Update,您可以在 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.

这篇关于使用直接在 Windows 中运行的模拟器在 WSL 中运行 React Native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆