Skip to content

Instantly share code, notes, and snippets.

@piouson
Forked from bergmannjg/rearct-native-app-in-wsl2.md
Last active April 26, 2024 09:41
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save piouson/5462853014b6b89f417a1b174254ea19 to your computer and use it in GitHub Desktop.
Save piouson/5462853014b6b89f417a1b174254ea19 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 Studio, see here
  • Install Viusal Studio Code, see here

Install tools in WSL2

Installation instructions below uses Open JDK version. If you want the Official Oracle version see how to install Official JDK 11

  • Install java-8-openjdk in WSL2 (sudo apt-get install openjdk-8-jre or use java11 openjdk-11-jre)
  • Install Android SDK cmdline tools in WSL2, see here
  • Install nodejs in WSL2, see here

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.

Android Studio > Configure > AVD Manager

Launch AVD Manager

Start android virtual device in Windows

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

Launch AVD Device

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

Create react native app in WSL2

npx react-native init testapp

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, see this StackOverFlow Answer

Build app in WSL2

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

-dontwarn okhttp3.internal.platform.*

Edit npm scripts in package.json

  • Run adb devices to get <device-name>
"scripts": {
  "android": "react-native run-android --variant=debug --deviceId <device-name>",
  "start": "react-native start --host 127.0.0.1",
}

First, start metro JavaScript bundler (--host 127.0.0.1 binds bundler to localhost which is forwarded to windows)

yarn start

Then, build and deploy app to device (--deviceId <device-name> specifies target device to deploy to)

yarn android

Happy hacking!

@cmkf01
Copy link

cmkf01 commented Sep 14, 2023

Hi - I am struggling to get android debugging to work with a physical device connected to WSL through USBIPD. Can you provide any advice? Currently i run all ADB commands on the WSL side primarily adb reverse tcp:8081 tcp:8081 but when i try adb -a nodaemon server start on Windows side, all I get it is:

adb F 09-14 14:59:18 83192 83192 main.cpp:144] could not install *smartsocket* listener: Address already in use

I am managing to run the app OK on physical device, just can't get anywhere with debugging.

Any help would be appreciated.

Best wishes

@piouson
Copy link
Author

piouson commented Sep 15, 2023

Hi - I am struggling to get android debugging to work with a physical device connected to WSL through USBIPD. Can you provide any advice? Currently i run all ADB commands on the WSL side primarily adb reverse tcp:8081 tcp:8081 but when i try adb -a nodaemon server start on Windows side, all I get it is:

adb F 09-14 14:59:18 83192 83192 main.cpp:144] could not install *smartsocket* listener: Address already in use

I am managing to run the app OK on physical device, just can't get anywhere with debugging.

Any help would be appreciated.

Best wishes

Wow I haven't setup android development in awhile, but the error seems to suggest port already in use. I hope its as easy as using netstat to find the process ID of the offending program and killing it with taskkill

netstat -ano | grep :$PORT
taskkill /pid $PID /f

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