Install, build and debug a react native app in WSL2 (Windows Subsystem for Linux) and Ubuntu.
- Install tools in Windows
- Enable mirrored networking mode in WSL2
- Install tools in WSL2
- Connect to android hardware device from Windows
- Connect to android hardware device from WSL2
- Connect to android virtual device in Windows
- Create react native app in WSL2
- Build app in WSL2
- Debug app in Visual Studio Code from WSL2
- Install WSL2 and Ubuntu, see here
- Install Android Studio, see here
- Install Viusal Studio Code, see here
Mirrored mode networking has the goal of 'mirroring' the network interfaces that you have on Windows into Linux. All programs on Windows and Linux can connect via localhost.
Add the following lines to %UserProfile%/.wslconfig (thanks to @craigjones-dev)
[wsl2]
networkingMode=mirrored
hostAddressLoopback=true
- Install java-21-openjdk in WSL2 (sudo apt-get install openjdk-21-jre)
- Install Android SDK cmdline tools in WSL2, see here and adjust directory structure, see here
- Install nodejs in WSL2, see here
Set environment variables in .profile or .bash_profile
export ANDROID_HOME=/home/xxx/Android/cmdline-tools/latest
export ANDROID_SDK_ROOT=/home/xxx/Android
PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools
PATH=$PATH:$ANDROID_HOME/bin
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
To debug on a hardware device connect to android device via adb
- start adb server in Windows: adb kill-server && adb server start
To debug on a hardware device connect to android device via usbip and adb from WSL2 (thanks to @cjshearer):
- Connect USB device to WSL
- start adb server in WSL2: adb kill-server && adb server start
To debug on a virtual device
- create a virtual device in windows with Android Virtual Device Manager, see here
- start the Android Emulator, see here, the adb server automatically starts on Windows.
npx react-native init AwesomeProject
Start metro JavaScript bundler and bind to an ipv4 address to enable port forwarding to windows
npx react-native start --host 127.0.0.1
Build app, set device as parameter deviceId from result of adb devices
- deviceId emulator: emulator-5554
npx react-native run-android --variant=debug --deviceId <deviceId>
Start vs code in WSL2
code .
and install extensions for VS Code
- Remote - WSL
- React Native Tools
VS Code UI runs in windows and the VS Code Server runs in WSL2, see here
Add a launch configuration in file launch.json with specified type and request
"name": "Attach to packager",
"cwd": "${workspaceFolder}",
"type": "reactnative",
"request": "attach"
Build app, attach to packager and start debugging, see here.
Ok so after a few days I've realized that this only works if I type
adb devices
in wsl terminal before starting the expo project, this is because I had 2 copies of adb, one in wsl and the other in windows, and this starts the adb server in windows.After digging through the source code of expo, found out that expo checks if adb server is already running, if it is, it uses that server, this explains why running the adb server in windows then starting the expo project in wsl works. We can also see that in
assertSdkRoot()
(source) it tries to find the adb through the environment variable$ANDROID_HOME
, so this explains why when we start the project it still runs the adb in wsl.In order for us to no longer do the extra step of starting the adb server in windows through typing
adb devices
oradb start-server
in wsl terminal before we start the project, a hacky way to fix this is to make a symlink (not really the same but think of it as a shortcut file windows but in linux) in the platform-tools folder in wsl that points to the adb in windows.Type this is wsl terminal:
with this, we've eliminated the extra step, now we can just start the project in wsl and it should connect to your physical device 😄