React native is a tool that allows developers to write apps one and render them anywhere: Android, iOS, web, and more. One does not need a state of the art PC or a mac in order to start learning how to develop things with React Native. While tools such as expo allow for one to test on any smartphone, one does not need to use that and can develop and test on the same PC. Learn how to set up a developer environment for React Native on a PC using Windows Subsystem for Linux (aka WSL). As a bonus, WSL subsystem for Linux is also a fantastic developer environment for more traditional web development work too!
I use a Mac at work and a PC for most of my personal work. There are positives and negatives about each, however PCs are bby more accessible to most. Meaning, folks are far more likely to be own and/or be able to afford a PC. Globally, this is also the case with android devices. Macs do enable one to develop and test iPhone apps, while PCs do not. Meaning to create an iPhone app, one will need a Mac. One does not need a fancy Mac computer to start coding apps! Start like a pro in a unix like environment! Learn and do great things with what you have!
First, one needs to set up Windows Subsystem for Linux, aka WSL. To do this, one should follow Microsoft's own instructions. You can skip this section if you already have WSL installed. A tl;dr summary is the following:
Windows Key + r
winver
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
wsl --set-default-version 2
Once installed, connect to it by opening a shell
(I suggest getting/using Window Terminal)
and just typing in ubuntu
The following sequence of commands will install node and npm. You can skip this
if node -v
npm -v
$ sudo apt update$ sudo apt install nodejs$ node -v$ sudo apt install npm$ npm -v
This is all that is needed to start doing traditional web development in a WSL environment.
The "host" windows installation is going to be what runs the emulated android device as well as an instance of adp (android debugger). Download and install Android Studio.
To make a virtual device, do the following after creating a new project and following the prompts:
Then hit the AVD manager button. Phone with the Android alien head peeking.
Click "Create Virtual Device"
Select a device. I suggest a Pixel 2. Hit next.
Hit download next to the release name "Q." Follow prompts to download. Once highlighted, hit next.
Hit finish.
Now one needs to set up adb:
C:\platform-tools
PATH
C:\platform-tools
The adb
Making a react native app is pretty standard. Run the following from the user
home directory. To get there type cd ~
npx react-native init ProjectName
Next, run the following sequence of commands. One should just copy them and run.
cd ~sudo apt-get install unzipwget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zipunzip sdk-tools-linux-4333796.zip -d Androidrm sdk-tools-linux-4333796.zipsudo apt-get install -y lib32z1 openjdk-8-jdkexport JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64export PATH=$PATH:$JAVA_HOME/binprintf "\n\nexport JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64\nexport PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.bashrccd Android/tools/bin./sdkmanager "platform-tools" "platforms;android-26" "build-tools;26.0.3"export ANDROID_HOME=/home/$USER/Androidexport PATH=$PATH:$ANDROID_HOME/toolsexport PATH=$PATH:$ANDROID_HOME/platform-toolsprintf "\n\n\nexport ANDROID_HOME=/home/$USER/Android\nexport PATH=\$PATH:\$ANDROID_HOME/tools\nexport PATH=\$PATH:\$ANDROID_HOME/platform-tools" >> ~/.bashrcandroid update sdk --no-uiprintf "\n\nexport export WSL_HOST=$(tail -1 /etc/resolv.conf | cut -d' ' -f2)\nexport export ADB_SERVER_SOCKET=tcp:\$WSL_HOST:5037" >> ~/.bashrcsudo apt-get install gradlegradle -v
The output of the final command should be the version of gradle being used.
The app now has everything needed to run.
In Windows,
Go to Android Studio's AVD manager and press the green play button.
In a terminal run as administrator:
adb kill-server
adb -a nodaemon server start
One should see something similar to the following in the output of
adb devices
PS C:\Users\Ryan> adb devicesList of devices attachedemulator-5554 device
In the ProjectName directory:
npx react-native start --host 127.0.0.1
npx react-native run-android --variant=debug --deviceId emulator-5554
emulator-5554
One may save those in their package json as the following:
{..."scripts": {"wsl-server": "npx react-native start --host 127.0.0.1","wsl-android": "npx react-native run-android --variant=debug --deviceId emulator-5554",...}...}
React native should be running on the android device in windows! Happy hacking!
Restart the computer.
Restart the emulator with a cold boot. To do this close the emulator and go to restart it, but instead of clicking the play button, hit the down caret to the right and run a cold boot.
Make sure USB debugging is enabled on android. To do this go to settings in the emulator, hit the build number until it says developer options are enabled, then go to developer options and enable usb debugging.
Check the path variables. Anything that says export
echo $variable
echo $WSL_HOST
172.28.48.1
One's milage may vary, but these instructions also worked with creating an expo app. In addition, the following needed to be run prior to installing the expo cli.
sudo chown -R $USER /usr/local/bin/sudo chown -R $USER /usr/local/lib/
Questions? Tweet me @rjerue!