Advanced Android Development in WSL: Connecting USB Devices

Advanced Android Development in WSL: Connecting USB Devices
2 mn read

Introduction

In our previous post, we set up the basic Android development environment in Windows Subsystem for Linux (WSL). Now, let’s take it a step further by exploring how to connect USB devices, particularly Android phones, to WSL for development and debugging purposes.

Prerequisites:

  • WSL 2 with Ubuntu distro
  • Basic Android development environment set up (as covered in part 1)
  • Windows 10 version 2004 or higher

Step 1: Verify WSL Kernel Version

Before we begin, ensure your WSL kernel supports USB device connectivity:

The kernel version should be 5.10.60.1 or later. If not, update your WSL.

Step 2: Install usbipd-win on Windows

usbipd-win allows sharing USB devices with WSL. Install it on your Windows host

There are two ways to install usbipd-win.

Option 1: Download and run the .msi installer from the usbipd-win releases page.

Option 2: Use Windows Package Manager winget install usbipd

It doesn’t matter how you install it as long as it gets installed at the end, this is up to your personal preference. However, take this note into consideration from their repo:

If you are using a third-party firewall, you may have to reconfigure it to allow incoming connections on TCP port 3240.

Step 3: Set Up WSL for USB Connectivity

Install necessary packages in your WSL Ubuntu distro

sudo apt update
sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*/usbip 20

Step 4: Connecting a USB Device

  1. Plug your Android device into a USB port on your Windows machine.
  2. Open PowerShell as Administrator on Windows and run:

3. Identify your device’s BUSID from the list.

4. Attach the device to WSL

usbipd wsl attach --busid <BUSID>

5. In WSL, verify the device is connected

Step 5: Troubleshooting ADB Detection

If ADB doesn’t detect your device, follow these steps:

  1. Get the VendorID and ProductID from lsusb output.
  2. Create or edit the udev rules file
sudo nano /etc/udev/rules.d/51-android.rules

3. Add this line, replacing the IDs with your device’s

SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="2e81", MODE="0666", GROUP="plugdev", SYMLINK+="android%n"

4. Optionally, add the VendorID to ~/.android/adb_usb.ini

5. Restart the ADB server

adb kill-server
sudo adb start-server
adb devices

Additional Tips

  • Disable Chrome DevTools on Windows if it’s using your device.
  • When done, detach the device
usbipd wsl detach --busid <BUSID>

Conclusion

With these steps, you can now connect USB devices, including Android phones, to your WSL environment for development and debugging. This setup combines the power of Linux development tools with the convenience of Windows, creating a versatile Android development workstation.

Remember to detach devices when you’re finished to make them available to Windows again.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reading is essential for those who seek to rise above the ordinary.

ABOUT US

The internet as we know is powerful. Its underlying technologies are transformative, but also, there’s a plethora of haphazard information out there.We are here to serve you as a reliable and credible source to gain consistent information

© 2024, cloudiafrica
Cloudi Africa