Android Development using WSL and WSA

Android Development using WSL and WSA

The last two days have been nothing short of an adventure. I aimed to test our React Native application on my Windows computer, but there was a catch. Our codebase is incompatible with Windows. Hence we use Ubuntu in the Windows Subsystem for Linux (WSL).

I owe a huge thanks to the extensive online community, without which I would have struggled to find success eventually.

Before I dive into the correct approach, I'd like to briefly outline my journey, detailing some possibly functional but incorrect methods. I've included references for those interested in the specific challenges I encountered.

I commenced with the React Native documentation for Windows and Linux but didn't make much headway. I realized the Android SDK must be inside WSL, not on my Windows machine.

Setting up the development environment · React Native

After some research, I found a document detailing installing the Android SDK CLI on Ubuntu 20.04 WSL2. However, after going through the process, I discovered it doesn't work with the latest version of Android command-line tools. After some tinkering, I moved on to another guide.

Install Android SDK CLI Ubuntu 20.04 WSL2.md (github.com)

I then considered using Windows Subsystem for Android (WSA) but hit a roadblock as WSA is unavailable outside the US and requires an Amazon US account.

Building a React Native app in WSL2 (github.com)

Without delving too much into the extensive debugging, researching, and experimentation, let's get straight to the correct way of setting it all up from scratch.

Part 1 - Setting Up Your Windows Machine and WSA:

  1. Install the latest version of the android studio from their website
  2. Have WSL installed and configured (obviously).
  3. Change your computer's region to the US and download Amazon Appstore from the Microsoft store. This will also prompt you to download Windows Subsystem for Android (WSA).
  4. Once WSA is downloaded, open its settings, navigate to advanced settings, enable developer mode, and set the subsystem resource to 'continuous' if you want apps to load quicker.

No alt text provided for this image

Part 2 - Installing Necessary Tools in WSL:

  1. Go to Android Studio's website and scroll right to the bottom and look for the command-line tools for Linux. If you want the latest version, you can use that package or the version I used.
  2. Download and setup the packages using the following commands:

wget https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip
mkdir -p Android/cmdline-tools
unzip commandlinetools-linux-8512546_latest.zip -d Android/cmdline-tools
mv Android/cmdline-tools/cmdline-tools Android/cmdline-tools/latest

export ANDROID_HOME=$HOME/Android
# Make sure emulator path comes before tools. Had trouble on Ubuntu with emulator from /tools being loaded
# instead of the one from /emulator
export PATH="$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/cmdline-tools/latest:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$PATH"

# don't need this anymore
# sdkmanager --sdk_root=${ANDROID_HOME} "tools"

sdkmanager --update
sdkmanager --list
# find current version
sdkmanager --list | grep build-tools
# change the version below with whatever version is downloaded
sdkmanager "build-tools;34.0.2" "platform-tools" "platforms;android-34" "tools"
sdkmanager --licenses        

Credit to GitHub user "jason-s-yu" for the above snippet minus a minor tweak.

(Note: React Native doesn't support Gradle 8, so you can't install the latest version. Use SDKMAN to install the correct version.)

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install gradle 7.5.1
# if you have multiple versions you can set the current one
sdk use gradle 7.5.1        

Remember, in the context of a typical React Native project, Gradle is run via the Gradle Wrapper, which uses the version defined in android/gradle/wrapper/gradle-wrapper.properties. Changing the system-wide Gradle version may not affect your project. You might still need to change the distributionUrl in the gradle-wrapper.properties file.

As of the recent update to WSA, it no longer has its IP address but uses the host's IP address. i.e., 192.168.50.3. My antivirus also interfered with this, so I have my antivirus and firewall disabled.

3. Connect to WSA. I am not too sure about the port. Some online resources said to use 5555 or 5554, but for me, it was the port in the WSA settings (58526).

# adb connect <host-ip:port>
adb connect 192.168.50.3:58526        

You should receive a pop-up confirming the device's authenticity, and running "adb devices" should list your device.

From this point, you can open your solution, run Metro, and execute the app. Once installed, your app will launch.


I hope this guide proves helpful and saves others from two days of intensive research. My main takeaway was that if it seems exceedingly complex to get something working in Windows, you're missing something fundamental.

Here's a list of other resources I found helpful, especially for those who don't want to use WSA or are interested in understanding the alternatives better:

Install Android SDK CLI Ubuntu 20.04 WSL2.md (github.com)

How to Install Windows Subsystem for Android (WSA) on Windows 11 (thewindowsclub.com)

(3) Why is WSA released only to the USA? : Windows11 (reddit.com)

android - How can I deploy and execute an application on a device connected to a remote system? - Stack Overflow

How to connect Android Studio running inside WSL2 with connected devices or Android1. Consider breaking up the text into smaller paragraphs to improve readability and make it easier for the reader to follow along with each step of the process.


Remember, no solution is one-size-fits-all. I encourage you to try and find the setup that works best for you. Happy coding!

Abdul Razaq

Director: Disciples Ministries International

1 年

nice work to achievement. God bless

Frederick van Staden

Senior Software Engineer at Asseti

1 年

Nice one! I tried and failed last time ;)

Marylis Ramos

Director, Savills Earth Advisory | Sustainability | Net Zero | ESG | Social Value

1 年

Nice work Solomon Razaq!

要查看或添加评论,请登录

Solomon Razaq的更多文章

社区洞察

其他会员也浏览了