How to Run Your Application on a Physical Device

Most Android phones and tablets can be connected to a computer using a USB cable. However, by default, the USB connection established between an Android device and a computer is limited to file transfer only.

Therefore, if you want to use your device for Android application development, you have to make a few configuration changes on both your device and your computer. In this quick tip, I show you how to make those changes.

Prerequisites

To follow along, you need:

  • the latest version of the Android SDK
  • an Android device running Android 4.2 or higher

1. Configuring Your Android Device

Because most Android users are not app developers, on devices running Android 4.2 or higher, all the settings meant for app developers are hidden by default. To show these settings, open the Settings app on your device and navigate to the About phone screen. Next, scroll down to Build number and click on it seven times.

When you do this, you should be able to see the Developer options menu. Open it and make sure that the USB debugging option is checked.

Additionally, I suggest that you check the Strict mode enabled and Show CPU usage options as well. With these options enabled, it is easier for you to tell if you’ve deviated from the recommended coding practices.

At this point, your device can be used for app development. Use its USB cable to connect it to your computer.

2. Configuring Your Computer

The configuration changes you need to make on your computer depend on the operating system it is running. In this quick tip, we focus on macOS, Windows, and Ubuntu.

macOS

On macOS, you don’t have to make any changes at all.

Windows

On Windows 7 or higher, you have to download and install an Original Equipment Manufacturer USB driver for your Android device. Usually, such a driver can be found on the website of the device manufacturer. If you are using any of the Google Nexus phones or tablets, however, you must install the Google USB Driver.

Ubuntu

On most flavors of Ubuntu, the configuration is slightly more involved. First, you must determine the USB vendor ID of your device. To do so, you can use the lsusb command.

You are now able to see the USB-related details of all the devices that are connected to your computer over USB. In the Device description section, look for the name of the company that manufactured your device and make a note of the value of the idVendor field. The value should be a 4-digit hexadecimal number.

Next, as a superuser, create a new file and name it /etc/udev/rules.d/51-android.rules.

Add the following udev rule to this file:

Finally, use the chmod command to allow all system users to read 51-android.rules.

3. Establishing a Local Connection

Now that both your Android device and your computer have been configured, you can start the Android Debug Bridge server, or ADB for short, to automatically establish a connection between them.

Navigate to the platform-tools directory of the Android SDK and use the adb start-server command to start ADB.

As soon the server is ready, you see a dialog appear on your device’s screen asking you to confirm if you want to allow USB debugging. The dialog also contains an RSA key fingerprint of your computer. Press OK to establish the USB connection.

From now on, you can use your device instead of the Android emulator while developing apps. If you are using Android Studio, on pressing the Run button in the toolbar, you are able to see your device in the list of running devices.

your device in the list of running devices.your device in the list of running devices.your device in the list of running devices.

4. Establishing a Connection Over Wi-Fi

A lot of Android developers own multiple Android phones and tablets in order to see how their apps look and behave on different screen sizes and Android versions. Keeping all those devices connected to a computer with USB cables can be awkward. Therefore, ADB also allows developers to connect to their devices over Wi-Fi.

There are a few requirements that you have to fulfil in order to debug and deploy your app wirelessly:

  1. Make sure that your device and your workstation are connected to the same wireless network.
  2. The device you want to connect wirelessly should have Android 11 or higher running on it.
  3. The Android Studio version also needs to be Bumblebee Canary or higher. Download the latest version if that’s not the case.
  4. Also make sure that you have the latest version of SDK platform tools installed.

The connection steps are incredibly easy to follow if you meet all the basic requirements.

Click on the run configurations button and you will see a dropdown menu. Now, click on the Pair Devices Using Wi-Fi option.

Configuration Options Dropdown MenuConfiguration Options Dropdown MenuConfiguration Options Dropdown Menu

This will open a new pop-up window which tells you that you can pair using a QR code on camera-enabled devices and using a six-digit pairing code on other devices. You can find both the QR scanner and pairing code by navigating to Developer Options > Wireless Debugging.

Android Studio Pair Over Wi-FiAndroid Studio Pair Over Wi-FiAndroid Studio Pair Over Wi-Fi

Click on the Pair device with QR code option on your device to open a QR scanner and scan the QR code being displayed on the screen. You will see the following screen upon a successful scan.

Android Studio Device Paired SuccessfullyAndroid Studio Device Paired SuccessfullyAndroid Studio Device Paired Successfully

You will now be able to deploy your app on multiple devices at once by choosing from the list of available devices when you click on Select Multiple Devices in the run configuration dropdown. You can use the device for Android app development just as you would use one connected over USB.

Selecting Multiple Devices for DeploymentSelecting Multiple Devices for DeploymentSelecting Multiple Devices for Deployment

Sometimes, the device might not pair even if you meet all the requirements. This happened with one of my devices. In such cases, you can open the adb command-line tool and run the following command to pair the device.

The values for host, port, and code will be available from the Pair device with pairing code menu option under Wireless debugging. The screenshot below shows an example:

Android adb Command Line ToolAndroid adb Command Line ToolAndroid adb Command Line Tool

Therefore, the full pairing command in my case would be:

Once the device has successfully paired, you can run the following command to make a connection:

The values of host and port will be taken from the IP address and port label shown above. In my case, the full command would be:

You will now be able to run the apps you develop directly on your phone, without the need to make a connection over USB.

Conclusion

In this quick tip, you learned how to configure both your Android device and your computer for USB debugging. You also learned how to set up an ADB connection over Wi-Fi.

It is very important that you see how your app behaves on as many physical devices as possible, especially if you plan to publish your app on Google Play. Why is that? Android devices tend to have quirks and limitations which, if left unaccounted for, can cause your app to behave in an odd manner, or even crash.

If you don’t own all the Android devices you want to support, you may want to consider using Google’s Cloud Test Lab, which allows you to easily run and test your app on almost all popular Android devices.

This post has been updated with contributions from Nitish Kumar. Nitish is a web developer with experience in creating eCommerce websites on various platforms. He spends his free time working on personal projects that make his everyday life easier or taking long evening walks with friends.