While much can be achieved by testing applications using an Android Virtual Device (AVD), there is no substitute for performing real-world application testing on a physical Android device, and some Android features are only available on physical Android devices.
Communication with both AVD instances and connected Android devices is handled by the Android Debug Bridge (ADB). This chapter explains how to configure the adb environment to enable application testing on an Android device with macOS, Windows, and Linux-based systems.
An Overview of the Android Debug Bridge (ADB)
The primary purpose of the ADB is to facilitate interaction between a development system, in this case, Android Studio, and both AVD emulators and Android devices to run and debug applications. ADB allows you to connect to devices via WiFi or USB cable.
The ADB consists of a client, a server process running in the background on the development system, and a daemon background process running in either AVDs or real Android devices such as phones and tablets.
The ADB client can take a variety of forms. For example, a client is provided as a command-line tool named adb in the Android SDK platform-tools sub-directory. Similarly, Android Studio also has a built-in client.
A variety of tasks may be performed using the adb command-line tool. For example, active virtual or physical devices may be listed using the devices command-line argument. The following command output indicates the presence of an AVD on the system but no physical devices:
$ adb devices
List of devices attached
emulator-5554 device
Code language: plaintext (plaintext)
Enabling USB Debugging ADB on Android Devices
Before ADB can connect to an Android device, that device must be configured to allow the connection. On phone and tablet devices running Android 6.0 or later, the steps to achieve this are as follows:
- Open the Settings app on the device and select the About tablet or About phone option (on some versions of Android, this can be found on the System page of the Settings app).
- On the About screen, scroll down to the Build number field (Figure 7-1) and tap it seven times until a message indicates that developer mode has been enabled. If the Build number is not listed on the About screen, it may be available via the Software information option. Alternatively, unfold the Advanced section of the list if available.
- Return to the main Settings screen and note the appearance of a new option titled Developer options (on newer versions of Android, this option is listed on the System settings screen). Select this option, and on the resulting screen, locate the USB debugging option as illustrated in Figure 7-2:
- Enable the USB debugging option and tap the Allow button when confirmation is requested.
The device is now configured to accept debugging connections from adb on the development system over a USB connection. All that remains is to configure the development system to detect the device when it is attached. While this is a relatively straightforward process, the steps differ depending on whether the development system runs Windows, macOS, or Linux. Note that the following steps assume that the Android SDK platform-tools directory is included in the operating system PATH environment variable as described in the chapter entitled Installing Android Studio.
macOS ADB Configuration
To configure the ADB environment on a macOS system, connect the device to the computer system using a USB cable, open a terminal window, and execute the following command to restart the adb server:
$ adb kill-server
$ adb start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
Code language: plaintext (plaintext)
Once the server is successfully running, execute the following command to verify that the device has been detected:
$ adb devices
List of devices attached
74CE000600000001 offline
Code language: plaintext (plaintext)
If the device is listed as offline, go to the Android device and check for the dialog shown in Figure 7-3 seeking permission to Allow USB debugging. Enable the checkbox next to the option that reads Always allow from this computer before clicking OK.
Repeating the adb devices command should now list the device as being available:
List of devices attached
015d41d4454bf80c device
Code language: plaintext (plaintext)
If the device is not listed, try logging out and back into the macOS desktop and rebooting the system if the problem persists.
Windows ADB Configuration
The first step in configuring a Windows-based development system to connect to an Android device using ADB is to install the appropriate USB drivers on the system. The USB drivers to install will depend on the model of the Android Device. If you have a Google device such as a Pixel phone, installing and configuring the Google USB Driver package on your Windows system will be necessary. Detailed steps to achieve this are outlined on the following web page:
https://developer.android.com/sdk/win-usb.html
For Android devices not supported by the Google USB driver, it will be necessary to download the drivers provided by the device manufacturer. A listing of drivers, together with download and installation information, can be obtained online at:
https://developer.android.com/tools/extras/oem-usb.html
With the drivers installed and the device now being recognized as the correct device type, open a Command Prompt window and execute the following command:
adb devices
Code language: plaintext (plaintext)
This command should output information about the connected device similar to the following:
List of devices attached
HT4CTJT01906 offline
Code language: plaintext (plaintext)
If the device is listed as offline or unauthorized, go to the device display and check for the dialog shown in Figure 7-3 seeking permission to Allow USB debugging. Enable the checkbox next to the option that reads Always allow from this computer before clicking OK. Repeating the adb devices command should now list the device as being ready:
List of devices attached
HT4CTJT01906 device
Code language: plaintext (plaintext)
If the device is not listed, execute the following commands to restart the ADB server:
adb kill-server
adb start-server
Code language: plaintext (plaintext)
If the device is still not listed, try executing the following command:
android update adb
Code language: plaintext (plaintext)
Note that it may also be necessary to reboot the system.
Linux adb Configuration
For this chapter, we will again use Ubuntu Linux as a reference example in configuring adb on Linux to connect to a physical Android device for application testing.
Physical device testing on Ubuntu Linux requires the installation of a package named android-tools-adb which, in turn, requires the Android Studio user to be a member of the plugdev group. This is the default for user accounts on most Ubuntu versions and can be verified by running the id command. If the plugdev group is not listed, run the following command to add your account to the group:
sudo usermod -aG plugdev $LOGNAME
Code language: plaintext (plaintext)
After the group membership requirement has been met, the android-tools-adb package can be installed by executing the following command:
sudo apt install android-tools-adb
Code language: plaintext (plaintext)
Once the above changes have been made, reboot the Ubuntu system. Once the system has restarted, open a Terminal window, start the adb server, and check the list of attached devices:
$ adb start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
$ adb devices
List of devices attached
015d41d4454bf80c offline
Code language: plaintext (plaintext)
If the device is listed as offline or unauthorized, go to the Android device and check for the dialog shown in Figure 7-3 seeking permission to Allow USB debugging.
Resolving USB Connection Issues
If you are unable to successfully connect to the device using the above steps, display the run target menu (Figure 7-4) and select the Troubleshoot Device Connections option:
The connection assistant will scan for devices and report problems and possible solutions.
Enabling Wireless Debugging on Android Devices
Follow steps 1 through 3 from section 7.2 above, this time enabling the Wireless Debugging option as shown in Figure 7-5:
Next, tap the above Wireless debugging entry to display the screen shown in Figure 7-6:
If your device has a camera, select Pair device with QR code, otherwise, select the Pair device with pairing code option. Depending on your selection, the Settings app will either start a camera session or display a pairing code, as shown in Figure 7-7:
With an option selected, return to Android Studio and select the Pair Devices Using WiFi option from the run target menu as illustrated in Figure 7-8:
In the pairing dialog, select either Pair using QR code or Pair using pairing code depending on your previous selection in the Settings app on the device:
Either scan the QR code using the Android device or enter the pairing code displayed on the device screen into the Android Studio dialog (Figure 7-10) to complete the pairing process:
If the pairing process fails, try rebooting both the development system and the Android device and try again.
Testing the adb Connection
Assuming that the adb configuration has been successful on your chosen development platform, the next step is to try running the test application created in the chapter entitled An Android Studio Tutorial on the device. Launch Android Studio, open the AndroidSample project, and verify that the device appears in the device selection menu as highlighted in Figure 7-11:
Select the device from the list and click the run button to install and run the app.
Device Mirroring
Device mirroring allows you to run an app on a physical device while viewing the display within Android Studio’s Running Devices tool window. In other words, although your app is running on a physical device, it appears within Android Studio in the same way as an AVD instance.
With a device connected to Android Studio, display the Running Devices tool window and click the Device Mirror settings link to display the Settings dialog. Within the Settings dialog, enable the mirroring of physical Android devices and click on OK. On returning to the main window, Android Studio will mirror the display of the physical device in the Running Devices tool window.
Summary
While the Android Virtual Device emulator provides an excellent testing environment, it is essential to remember that there is no real substitute for ensuring an application functions correctly on a physical Android device.
By default, however, the Android Studio environment is not configured to detect Android devices as a target testing device. It is necessary, therefore, to perform some steps to load applications directly onto an Android device from within the Android Studio development environment via a USB cable or over a WiFi network. The exact steps to achieve this goal differ depending on the development platform. In this chapter, we have covered those steps for Linux, macOS, and Windows-based platforms.