OVERVIEW

Iteration speed is a key factor in building quality Android applications. The faster you can make progress, the more polished the result will be. This article discusses how to optimize deployment time.

DEPLOYMENT 101

With Android Studio 3.5, numerous improvements were made to improve iteration time. Depending on how the code is structured, a developer can now elect to use Apply Changes, which will swap changed code and restart the current Activity. If the code is not tied to the Activity lifecycle, “Apply Code Changes” can be used instead in order to just swap modified code and not restart the running Activity.

Apply Changes introduced two new ways to deploy an app.

On top of Apply Changes, Android Studio 3.5 introduced “Delta Push”, which sends to the device only the changed portion of an APK in a subsequent Run invocation. The app is reconstructed on device by alternatively feeding the new portions and the “old” portions to the Package Manager via sendfile(2), achieving near zero-copy installation.

Together, these dramatically reduced installation time. However, in order to get the best of these optimizations, developers have to be mindful of the quality of the device connection.

WHY USB SPEED IS IMPORTANT

Whether “Delta Push” triggers or not, some data will have to be transmitted to the device. With an emulator, this is done over TCP/IP but in the case of a real device, USB will be used. The faster the USB connection, the less time you wait and the faster you can iterate.

The good news is that USB is getting faster. The first version in 1996 provided a maximum of 12 Mb/s and with the release of v4.0 on August 2019, we are now well into the multi-Gb/s era.

From 1996 to 2019, USB signal rate improved from 1.5MiB/s to 40GiB/s.

If you don’t look too much into the details, USB is a simple technology. As long as two devices are capable of the same USB version, you just grab a USB cable, connect them, and it works. The client layer doesn’t even have to change. It is a “free-lunch”.

As a developer, it is important to be on the fastest side of USB speed. Up to a certain point (a phone’s flash drive cannot write at USB 3.2’s 20 Gb/s but can out-perform USB 2.0) there is a direct correlation between USB transfer rate and iteration speed. If you are working with a 50MiB apk, it is 25% faster to use USB 3.0 over USB 2.0. You save a whole second on each installation which will be adding up at the end of the day.

Installing a 50MiB app with USB 3.0 is 30% faster than with USB 2.0.

The bad news is that it is surprisingly easy to operate at USB 2.0 speed with USB 3.0 devices. Using the wrong cables or a bad hub are the two best ways to make sure things are slower than they should.

AVOID SLOWING DOWN YOUR USB CONNECTION

There are three simple rules you can follow to help avoid slowing down your USB connection.

Look for blue. Understand what USB-C means. Watch out when you chain USB devices.

LOOK FOR BLUE

If you are dealing with USB Type-A or USB Type-B connectors you are in luck. USB 3.0 specification recommends to color-code connectors wired to USB 3.X capable hardware. If you see something like the following pictures, you are on the right path.

USB Type-A:

Left: USB Type-A female. Right: USB Type-A male.

USB Type-B:

Left: USB Type-B female. Right: USB Type-B male.

UNDERSTAND USB-C CONNECTORS

USB-C connectors were introduced around the same time as USB 3.1. Many people assume one means the other or that they are somehow related. They are not.

USB 2.0 cables are made of four wires. Two provide power to the bus, two carry the data.

A USB 2.0 cable contains four wires.

USB 3.X cables are fully backward compatible. They have the two wires necessary to USB-2 signal along with two dedicated twisted pairs for USB-3 upstream and downstream.

A USB 3.+ cable contains eight wires.

USB Type-C connectors do not indicate how many wires are connected. They only refer to the shape of a connector. Since they are cheaper to manufacture, there are many cables out there with USB-C connectors but only USB-2 wiring.

The Type-A/C cable bundled in the Nexus 6P box.

This cable above came bundled in the box of the Nexus 6P. These phones have USB 2.0 hubs connected to USB type-C connector.

Inside these cables are only four wires.

If this is what you have permanently plugged in your workstation as your “go-to” cable and you use it to swap between phones, you are likely downgrading your experience with USB-3 capable devices.

While dealing with USB type-C connectors, make sure to look for the presence of the SS (SuperSpeed) symbols.

USB Super-Speed symbol.

In the case of phone connectors, double check the specifications. Even recent phones such as the Pixel 3a and Pixel 3a XL have USB 2.0 hubs.

CHAINING USB DEVICES

The last mistake to avoid is to use a bad hub. It is equally bad as using the wrong cables. A common configuration is to have all my devices (from keyboard, mouse, and dev phones) plugged into a monitor which acts as a USB hub.

Many hubs’ intended usage is to concentrate “lightweight” peripherals such as keyboard, mouse, and headsets which only require USB 2.0 capability.

Make sure your USB hub is 3.0 capable by checking it has blue connectors or looking up the documentation if it only has USB type-C connectors.

USEFUL TOOLS

If you are in doubt, you can double check what USB speed a host negotiated with a device.