Skip to content

Instantly share code, notes, and snippets.

@xquangdang
Last active March 22, 2024 14:39
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save xquangdang/d52c8d7d37e396750fca37525aa034d7 to your computer and use it in GitHub Desktop.
Save xquangdang/d52c8d7d37e396750fca37525aa034d7 to your computer and use it in GitHub Desktop.
Develop React Native app with WSL and WSA

Develop React Native app with WSL and WSA

Requirement

  • Windows Subsystem for Linux 2 (WSL2)
  • Windows Subsystem for Android (WSA)
  • NodeJS install inside WSL2

Steps

WSA

  1. Enable developer mode for WSA image

WSL2

This tutorial will run on Ubuntu 20.04

  1. Install Android debug bridge (adb)
$ sudo apt install adb
  1. Connect to WSA

image

  • Connect to WSA via IP
$ adb connect <wsa-ip:port>

image

  • Make sure ADB connect successfully
$ adb devices
List of devices attached
172.19.99.153:5555      device

React Native setup

I'll follow tutorial on ReactNative.dev

# Assuming that you have Node 12 LTS or greater installed, you can use npm to install the Expo CLI command line utility
$ sudo npm install -g expo-cli
# Then run the following commands to create a new React Native project called "AwesomeProject"
$ expo init AwesomeProject

cd AwesomeProject
$ npm start # you can also use: expo start

Select Run on Android device/emulator in expo web client image

FAQ

@Sourav2005
Copy link

Thank you very much!

@ttay24
Copy link

ttay24 commented Oct 28, 2021

This is awesome, thank you!

@mwoodpatrick
Copy link

Many thanks

@petvi
Copy link

petvi commented Nov 25, 2021

Thank you!!!

@davioliveira-dev
Copy link

Thank you dude!

@kicksent
Copy link

kicksent commented Aug 31, 2022

failed to connect to '127.0.0.1:58526': Connection refused

I don't have the same options for the ip but there is this ip listed. However, I am unable to connect.

@raf2k07
Copy link

raf2k07 commented Sep 8, 2022

This is a local address and will only be visible to applications running on Windows (NOT WSL).
The IP address option is no longer present in WSA settings (this may have been part of some recent update, because I definitely had the option to copy the public IP address a while ago):

image

image

I honestly don't know where to find the exposed IP address that I can connect to from within WSL2. Anybody else facing this?

@xquangdang
Copy link
Author

@kicksent @raf2k07 can you try this:

export NAME_SERVER=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}')
adb connect $NAME_SERVER:58526 #58526 is WSA port, check in developer mode settings

WSL doesn't understand localhost as the Windows side. So basically, it will grab the nameserver in /etc/resolv.conf and connect to that IP

@ericrocha97
Copy link

@raf2k07
As it says in this matter, now the ip of the wsa is the same as your machine.
ex: 192.168.0.2:58526
https://winaero.com/windows-subsystem-for-android-has-got-new-settings-and-networking-improvements/

@raf2k07
Copy link

raf2k07 commented Sep 9, 2022

@raf2k07 As it says in this matter, now the ip of the wsa is the same as your machine. ex: 192.168.0.2:58526 https://winaero.com/windows-subsystem-for-android-has-got-new-settings-and-networking-improvements/

Yeah this works, thank you! I'm not sure why this bit isn't mentioned in the official WSA docs (as far as I can see).

@MoaJR
Copy link

MoaJR commented Nov 5, 2022

Can anyone help me?
I can see the device connected when I type adb devices, nut when I try to run the emulator i get a lot of errors
image

image

@xquangdang
Copy link
Author

Can anyone help me? I can see the device connected when I type adb devices, nut when I try to run the emulator i get a lot of errors image

image

Look like you do not have android SDK installed in WSL. you should have Android SDK and ANDROID_HOME path setup correctly.

@lagz0ne
Copy link

lagz0ne commented Dec 12, 2022

It seems the advanced networking changed the workflow quite a bit. After a bit of struggling, I am able to use expo-go to access https://gist.github.com/lagz0ne/6254a0da99719b650f5c6cf4548a1e6f

@Ra1NuX
Copy link

Ra1NuX commented May 17, 2023

For some reason, EXPO GO closes after trying to connect with port 19000, it gives a Java error, but after investigating, I think it may have to do with an option within the WSA settings that does not allow me to enable the wireless debugging option. It's a button that, no matter how much I press, it doesn't activate. Any other ideas?

@Ra1NuX
Copy link

Ra1NuX commented May 17, 2023

[ADB] Couldn't reverse port 19000: adb: error: cannot bind listener: Address already in use

@0xSimbaDev
Copy link

Can anyone help me? I can see the device connected when I type adb devices, nut when I try to run the emulator i get a lot of errors image

image

Set environment path in your bash profile:
`export ANDROID_HOME=/home/xxxx/Android
export ANDROID_SDK_ROOT=/home/xxxx/Android

PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools
PATH=$PATH:$ANDROID_HOME/bin
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment