Wireless Android development with a local device

Wireless Android development with a local device

My main laptop is a relatively old Macbook. It can handle everyday workloads with ease. The only part that struggles is with my Android development. It's the only time I can hear the fans screaming as they try to cool down the processor. That's why, under no circumstance, I cannot run an Android emulator while developing.

So you might say I have to develop using a test device always connected. Yes, I know that ADB supports wireless connections over wifi. But what's the benefit of having a wireless test device if I still have to move and pick up the device? The convenience of the emulator is that it's on the screen, next to your code - no effort needed to see the results of your code. Also, it takes "too much" time to  set up ADB over wifi each time you want to develop. You need to figure out the device IP and then run a command.

If I could find a way to set up ADB over WiFi quickly (without too many steps) in combination with a way to stream my device screen to my laptop, I could get an emulator-like experience!

Scrcpy

I was excited to discover scrcpy, an open-source tool that's been around for some time but I discovered it only recently. It's a command-line util that streams your device screen on your computer, available for all major OSes.

After you install it, you can run scrcpy to stream (and interact) with your device screen on your laptop. As simple as that.

In case you run into trouble with streaming over Wi-Fi you can try to decrease the bitrate and resolution: scrcpy --bit-rate 2M --max-size 800

ADB over Wifi

Normally, instead of having your mobile device connected, you can set up ADB to run through Wifi.

  1. Plug your device
  2. Find the IP of your device through settings (multiple clicks involved)
  3. Enable ADB over TCP/IP
    adb tcpip 5555
  4. Connect to your device remotely
    adb connect IP:5555
  5. Unplug your device

Now, these might not seem like many steps, but over time they get repetitive and you will probably just leave your device connected. What I wanted was to automate steps 2-4 so I could get my "remote emulator" ready quickly. Thankfully, I found an one-liner (for Mac/Linux) that could get me exactly this:

  1. Plug your device
  2. Run (for Mac/Linux)
    adb kill-server && adb tcpip 5555 && sleep 5 && adb shell ip route | awk '{print $9}' | xargs adb connect
  3. Unplug your device

Then running scrcpy would get you the device screen ready to interact. Put these into a shell script or something similar and you have a quick emulator-like set up for your Android development.


Note that depending on your device (e.g. different device chipsets provide different network interfaces), you might need to modify the above command to make it work. David contacted me to mentioned that he needed to modify the command like this:

adb kill-server && adb tcpip 5555 && sleep 5 && adb shell ip route | awk 'NR==1{print $9};' | xargs adb connect
where NR represents the line number awk returns, my wlan0 device is on line 1 and on line 2 is my rmnet_data0 which is why it wasn't working for me.

So if you are having trouble with the command above, run adb shell ip route and try to figure out how to extract the device IP address.

Thanks David!


Happy wireless Android development!

Show Comments