Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelod1as/3d3755a2a6714398203442b760006859 to your computer and use it in GitHub Desktop.
Save angelod1as/3d3755a2a6714398203442b760006859 to your computer and use it in GitHub Desktop.
Building a react native app in WSL2

Building a react native app in WSL2

Install, build and debug a react native app in WSL2 (Windows Subsystem for Linux) and Ubuntu.

Install tools in Windows

  • Install WSL2 and Ubuntu, see here
  • Install Android Studion, see here
  • Install Viusal Studio Code, see here

Install tools in WSL2

  • Install java-8-openjdk in WSL2 (sudo apt-get install openjdk-8-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

Create android virtual device in Windows

Create a virtual device (e.g. Nexus_5X_API_29) in windows with Android Virtual Device Manager from Android Studio.

Start android virtual device in Windows

Start Android virtual device (e.g. Nexus_5X_API_29) in windows

"C:\Program Files (x86)\Android\android-sdk\emulator\emulator.exe" -avd Nexus_5X_API_29

Start adb server in Windows

adb kill-server
adb -a nodaemon server start

Change firewall rule for adb.exe on first usage in Defender Popup or with Windows Defender Firewall allowing access for the public profile, because the vEthernet (Wsl) adapter belongs to the public profile

Enable access to adb server from WSL2

Set environment variable to access adb server, WSL_HOST is ip of vEthernet (WSL) interface in windows

export WSL_HOST=$(tail -1 /etc/resolv.conf | cut -d' ' -f2)
export ADB_SERVER_SOCKET=tcp:$WSL_HOST:5037

Somtimes adb crashes using the environment variable config. One solution is to use socat (thanks to @tuanna1601).

Unset the environment variable if necessary.

unset ADB_SERVER_SOCKET

Install socat (eg. sudo apt-get install socat). Socat relays the requests from wsl2 to windows using the following command:

socat -d -d TCP-LISTEN:5037,reuseaddr,fork TCP:$(cat /etc/resolv.conf | tail -n1 | cut -d " " -f 2):5037

Enable port fowarding to metro bundler from Windows

Add port fowarding from windows to metro bundler running in WSL2, listening on port 8081, enabling a connection via adb from the emulator to WSL2.

WSL_CLIENT is ip of WSL2.

Below, in Powershell:

iex "netsh interface portproxy delete v4tov4 listenport=8081 listenaddress=127.0.0.1" | out-null;
$WSL_CLIENT = bash.exe -c "ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'";
$WSL_CLIENT -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
$WSL_CLIENT = $matches[0];
iex "netsh interface portproxy add v4tov4 listenport=8081 listenaddress=127.0.0.1 connectport=8081 connectaddress=$WSL_CLIENT"

Create react native app in WSL2

npx react-native init AwesomeProject

Build app in WSL2

Add parameter in file proguard-rules.pro to ignore okhttp3 warnings

-dontwarn okhttp3.internal.platform.*

Start metro builder

npx react-native start

Build app, set device as parameter deviceId from result of adb devices

npx react-native run-android --variant=debug --deviceId emulator-5554

Debug app in Visual Studio Code from WSL2

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 target

"type": "reactnative",
"target": "emulator-5554"

Start debugging.

@StephenDenisEdwards
Copy link

Why would I want to do that?

@StephenDenisEdwards
Copy link

Can do all directly on Windows.....????

@angelod1as
Copy link
Author

Ok, do it then.

(Why comment something like this on a solution to a problem?)

@StephenDenisEdwards
Copy link

StephenDenisEdwards commented Dec 23, 2020 via email

@angelod1as
Copy link
Author

I read your question in a hostile way. I'll answer you properly now.

Linux is — by far — better than windows for software development. It has functionalities and freedom that windows lacks. In the event of someone needing to use windows (because of graphical softwares or such), WSL is a good answer to that. Keep yourself in windows and still use some of Linux development functionalities.

I came to WSL because I tried to do a lot of stuff on Windows and couldn't do it, things didn't run as smooth as they should. So, after a lot of frustration with windows I tried WSL and liked.

I liked it so much that I transitioned fully to Linux. I'm writing this message on my PopOS, a Linux partition on my PC. It's way better than Windows for my kind of development.

(nonetherless, sincerely, the best platform is still Mac OSx, but for me the cost is impossible)

@StephenDenisEdwards
Copy link

Thanks for you response. I have always developed using Windows so it is interesting for me to look at Linux now that I am looking to develop an Android application. I am also interested in potentially developing with the Android OS itself and as far as I know this can only be built in Linux. One thing I am having trouble with on WSL2 is networking.....WSL1 works fine - but when I switch to WSL2 I have no network connectivity....Any ideas?

@angelod1as
Copy link
Author

I didn't have this problem when I ran WSL2. Sorry, can't help you with that. Maybe StackOverflow?

Anyways, I recommend thinking about moving to a full linux environment. I know it's a bummer to switch from windows to linux, but for me it's been a 10/10 experience.

@danielkv
Copy link

Did you manage to make it work? I'm not getting there...

I have the device attached but when I try to connect it display an error:

Couldn't start project on Android: could not connect to TCP port 5554: Connection refused

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