Android debugging in VS Code and Windows Subsystem for Linux (WSL)
The Windows Subsystem for Linux (WSL) has been a game-changer for software development on Windows. Paired with VS Code and its remote development feature, it is a powerful alternative to the traditional MacOS and Linux development environments. Having all my projects in VS Code within WSL has been a great development experience.
Recently, working with BookSloth on their Android app, I wanted to run and debug the app locally. One option was using Android Studio, but that meant giving up the uniformity of my development process. So I started setting up the project and the Android SDK in the WSL only. The problem came when running the emulator. WSL does not support virtualization out-of-the-box, and adding virtualization required experimental configurations. The solution was to run the emulator on the Windows host and bridging it to the WSL setup.
With this configuration, you’ll be able to do all your development inside the WSL with VS Code and debug your application on an emulator running on the Windows host.
- Android SDK Command Line Tools
- Android VS Code extension
- Code project is cloned inside the WSL file system
Setup is required for both the Windows host and the WSL in order to make this work. This was set up using the Ubuntu 20.04 for the WSL machine and probably works for other Linux distros. Commands for Windows are being run in PowerShell and bash in the WSL.
Feel free to customize names or directory locations in the following steps where necessarily to meet your requirements or accommodate them to your existing development environment.
- WSL and Windows — Create a directory called
androidwherever you specify your SDK files
- WSL and Windows — Download the latest Command Line Tools from the link above and extract the .zip
- WSL and Windows — Move
- WSL and Windows — Move contents of
cmdline-toolsinto a sub-folder called
latest(assuming you’re using the latest version of the command line tools)
- WSL and Windows — Configure the PATH location of the
sdkmanagerand other SDK tools:
- WSL — Install the build and platform tools for the target version. Check the app/build.gradle project file for the
sdkmanager "build-tools;<TARGET_VERSION>" "platforms;android-<TARGET_VERSION>" "platform-tools"
- Windows — Install the platform and emulator tools and packages for the target version
sdkmanager "platforms;android-<TARGET_VERSION>" "platform-tools" "emulator"
- WSL and Windows — Accept SDK licenses
sdkmanager — licenses
- Windows — Download an Android Virtual Device (AVD) image
- Windows — Create a new AVD using the avdmanager
avdmanager create avd -name [AVDName] -package "[PACKAGE]" -device "[DEVICENAME]"
- Windows — Add Firewall Rules to allow port traffic to and from WSL. Click here and here for more information on how to do so.
- WSL — Install the Android VS Code extension by adelphes
- WSL — Change the host IP address in the
adbSocketproperty in the .vscode/launch.json to the WSL host IP. This can be obtained using
cat /etc/resolv.conf | grep nameserver | cut -d' '-f2
- WSL — Build your project
- Windows — Start the Android emulator
emulator -avd [AVDName]
- Windows — Open port 5037 communication from Windows host to WSL
adb -a -P 5037 nodaemon server
- WSL — Start debugging in VS Code by running the debug configuration created by the Android extension. Happy debugging!
Emulator — Dev computer keyboard not working in emulator
- Go to path\to\.android\avd\AVDName.avd
- In the config.ini file, change
Debug — Launch failed: ADBSocket Socket connect failed.
- Check your debug launcher and emulator device are using the same port (defaulted to 5037)
Debug — Launch failed: No devices are connected
- Check emulator is running
- Check adb server is running in WSL
- Check you installed the SAME version of adb on both Windows and WSL using adb - - version
- Check you are using the
adbcommand installed in the WSL. To avoid PATH mix-ups in WSL, disable importing your Windows PATH into WSL by creating the file /etc/wsl.conf/ and adding the lines
adb -a -P 5037 nodaemon serveris running in Windows host
- Check in Windows host Firewall is not blocking traffic from adb or WSL
- Check the launch configuration has the correct WSL host IP and port in the