Instruction 2

Preparing Your Android Device

Android emulators are efficient for cost-effective testing and development. Although emulators can simulate various Android devices and scenarios without physical hardware, you may run into a scenario where you need to run your Android app on a physical device. Testing your Android app on a physical device offers several advantages for a more comprehensive and accurate evaluation of your app.

Unfortunately, you can’t just plug your device into your computer and expect it to communicate with Android Studio immediately. Some initial configuration and setup are required to ensure proper communication between Android Studio and your device. The first thing you’ll need to do is put your device into Developer Mode.

Enabling developer mode on your Android device is a prerequisite for activating USB debugging. USB debugging is essential for connecting an Android device to your computer for app development. It allows you to deploy, test, troubleshoot, debug, and optimize your apps directly from Android Studio.

Developer mode doesn’t cause any issues or harm to the device. It mainly unlocks additional settings and features for development purposes. Enabling developer mode is generally safe, but use caution when changing settings that grant extra access, as they may be misused.

Every device has a slightly different way to enable developer mode. The steps may vary across manufacturers and operating systems. You might need to look at the documentation for your specific device, but in general, these steps should help:

  • Go to the Settings app on your Android device.

Android Settings App
Android Settings App

  • Scroll down and tap the About phone or About tablet section, depending on your type of device. It will take you to the About phone or About tablet screen.

About Phone Screen
About Phone Screen

  • Scroll down to find the Build number or Build version label.
  • Tap the Build number or Build version label seven times. As you get closer to seven taps, you will see a message saying you’re getting close to enabling developer mode. That’s a good sign! Continue tapping the label until the countdown gets to zero.

4 Steps Away From Developer Mode
4 Steps Away From Developer Mode

  • After tapping the Build number or Build version label seven times, you may be prompted to enter your passcode or pattern to unlock your device. Enter your passcode or unlock pattern.
  • You will see the message, You are now a developer!, to let you know it worked.

Developer Mode Enabled
Developer Mode Enabled

Now that you’ve enabled developer mode, you’ll have a new Developer options screen in the settings app of your device. To get to the developer options screen, open the main settings app of your Android device. Tap on System, then scroll down and look for Developer options. Most devices will have developer options under the system screen. If you can’t find the developer options screen, try searching for it in the main settings app screen or retry the steps above to enable developer options. If you can see the developer options screen, you’ve successfully enabled developer mode on your device.

Developer Options Screen
Developer Options Screen

Connecting Your Device Via USB

In the developer options screen, you’ll see a long list of options. These options are there to adjust as needed during your development experience. After being mesmerized by all the settings you can tweak in the developer options screen, look for and enable USB debugging under the Debugging section.

Enable USB Debugging
Enable USB Debugging

You’ll see a dialog box to confirm the action. Tap OK to dismiss the dialog. Enabling USB debugging will allow your device to enter debug mode when the USB is connected. This is a critical step for connecting your device to Android Studio via a USB cable. It enables a secure connection that permits Android Studio or other development tools to transfer and install applications directly onto the device.

Now, the moment you’ve been waiting for. Fire up Android Studio, open the Bullseye project from the earlier lessons, and grab your USB cable. Connect the Android device to your computer with a USB cable. When doing this, keep an eye on your device. A dialog will appear asking you for permission to allow USB debugging. Tap Allow to confirm the connection.

Allow USB Debugging
Allow USB Debugging

You’ll see your device listed in the Android Studio device manager window.

Device Manager
Device Manager

If your device isn’t listed, you can try a few things to troubleshoot:

  • Disconnect the USB cable from the computer.
  • Ensure the device has Developer Mode enabled.
  • In developer options, ensure your device has USB debugging enabled.
  • Tap the option to Revoke USB debugging authorizations, and tap OK. This will reset the USB permissions so that it prompts you to accept the next time you connect.
  • Reconnect the device to the computer using USB.
  • Accept any prompts for USB authorization.

Connecting Your Device Using Wi-Fi

If you don’t want to tether your device to your computer using USB, Android Studio has you covered. You can connect your device to Android Studio using Wi-Fi. The connection setup can be done in the device manager window. One prerequisite to pairing your device through Wi-Fi is that your device and computer are on the same Wi-Fi network.

In Android Studio, open the device manager window. Next to the + button is a button to pair devices using Wi-Fi. Click the Pair Devices Using Wi-Fi button. You’ll see the Pair devices over Wi-Fi screen. There are two ways to pair your Android device: Pair using a QR code, and pair using a pairing code. Both pairing mechanisms require you to go to the developer options screen on your Android device.

Pair New Devices Over Wi-Fi Screen
Pair New Devices Over Wi-Fi Screen

Pairing With QR Code

The first thing you need to do is ensure your Android device and your computer is on the same Wi-Fi network. Once your device and computer are connected to the same Wi-Fi, open the developer options screen in the settings app. To do this, open the Settings app. Tap on the Search settings text field and type in Developer options. Find and tap Developer options in the search results. Once you have opened the Developer Options screen, scroll down and look for the Wireless debugging option.

Enable Wireless Debugging
Enable Wireless Debugging

Tap the Wireless debugging label to open the Wireless debugging screen.

Enable Wireless Debugging Screen
Enable Wireless Debugging Screen

In the wireless debugging screen, tap the toggle button to enable wireless debugging. A dialog box will prompt you to confirm your action. Tap Allow to allow wireless debugging on the network.

Now, the Wireless Debugging screen will display wireless debugging information about your device.

Wireless Debugging Enabled
Wireless Debugging Enabled

To pair with the QR code, tap the Pair device with QR code row. Your device will enable the camera. Use the camera on your device to scan the QR code in your Android Studio Scan QR code screen.

Scan QR Code
Scan QR Code

The device will pair with Android Studio over Wi-Fi. After a successful connection, the Android Studio Pair new devices over Wi-Fi screen will show a green checkmark indicating that you’re now connected. This process might take some time depending on computer and phone configuration.

QR Code Paired Successfully
QR Code Paired Successfully

Click the Close button on the Pair new devices over Wi-Fi screen in Android Studio. Your device will appear in the Android Studio device manager window.

Pairing Using A Pairing Code

Pairing using a pairing code is very similar to pairing using the QR code. Make sure your Android device and computer are connected to the same Wi-Fi network. In Android Studio, open the device manager window. Next to the + button, is a button to pair devices using Wi-Fi. Click the Pair Devices Using Wi-Fi button. You’ll see the Pair new devices over Wi-Fi screen. Tap the Pair using pairing code tab.

Pair Using Pairing Code
Pair Using Pairing Code

In your Android device, you’re going to get back to the wireless debugging screen. Open the Settings app. Tap the Search settings text field and type in Developer options. Find and tap Developer options in the search results. Once you have opened the developer options screen, scroll down and look for the Wireless debugging option. Tap the Wireless debugging label to open the Wireless Debugging screen. In the wireless debugging screen, tap on the toggle button to enable wireless debugging. A dialog box will prompt you to confirm your action. Tap Allow to allow wireless debugging on the network. Now the Wireless debugging screen will display wireless debugging information about your device.

Wireless Debugging Enabled
Wireless Debugging Enabled

To pair using a pairing code, on your device tap the Pair device with pairing code row. A dialog box will display a code on your Android device. You’ll need this code in the next step.

Device Pairing Code
Device Pairing Code

The Android Studio Pair new devices over Wi-Fi screen will find and display your device on the list. Tap the Pair button on the row where your device is listed.

Code Pairing List of Devices
Code Pairing List of Devices

A new window will prompt you to enter the pairing code from your device.

Enter Pairing Code
Enter Pairing Code

Enter the pairing code, then tap the Pair button. Android Studio will pair with the device and the Enter pairing code window will inform you that the device is connected.

Code Paired Successfully
Code Paired Successfully

Click the Done button. Close the Pair Devices Using Wi-Fi window, and navigate back to the device manager window in Android Studio. Your device will appear in the Android Studio device manager window.

Now that you have your Android device connected to Android Studio, you’re ready to run your first Android app.

Running An App On A Physical Device

Running an Android app on physical devices is no different than running it on a virtual device. Open the Bullseye app you created in Lesson 1. This is the app you’ll run on your device.

The top toolbar of Android Studio provides a convenient way to set the device, choose the app, and run the app. The middle section of the top toolbar provides a drop-down to select the device. To the right of that, it provides a drop-down to choose which app or module to run. Then, immediately to the right of the app selection is the Run app icon. It looks like a play button.

Top Toolbar Run Controls
Top Toolbar Run Controls

Ensure your Android device is connected to Android Studio by opening the device manager window and verifying the device is listed. On the top toolbar of Android Studio, click on the device drop-down. It will display a list of physical and virtual devices that you can deploy the app to. If you don’t see your device listed, reconnect your Android device to Android Studio. Make sure app is selected as the app to run. Click the Run app icon. Android Studio will compile the app and install it onto the device.

The Run app icon will change to an activity indicator spinner until it starts running. You can check the progress of the build and the run at the bottom right of Android Studio. After the app is built, it is installed on your device, and you’ll see the app launch on the device’s screen. The Run window will automatically display across the bottom of Android Studio. The run window displays output generated from your application. You’ll also see notifications from Android Studio on the bottom right when the app is installed and running on your device. Your device will continue to communicate with Android Studio as long as it’s connected.

App Running On Device
App Running On Device

Once your app is running, the top toolbar of Android Studio will show a few different controls. The Run app button now looks like a refresh icon. You use it to re-run your app. Next to it is a Debug app button. You use that button when you want to run your app in debug mode. The button to the right of the debug button is the stop app button. Click the Stop app button and the app will stop running on your Android device.

Understanding The ADB

When your device is connected to your computer, quite a bit of communication happens in the background for Android Studio. Android Studio uses the Android Debug Bridge (ADB) to enable communication with Android devices. ADB is a command-line tool that serves as a communication bridge between your computer and the Android device or emulator.

The ADB plays a crucial role in Android app development and debugging. It’s integrated into Android Studio, and many of its features can be accessed through the IDE’s interface. Most of the time, you won’t even know that Android Studio uses the ADB. In addition to being integrated with the IDE, ADB is commonly used directly through the command line interface in a terminal. Whether using a physical device connected through a USB cable or Wi-Fi or using emulators to run your Android apps, it’s a good idea to understand how to use the ADB.

Using The ADB

ADB commands are used for various tasks related to Android development and debugging. Android Studio provides a terminal window where you can interact with the ADB. To interact with the ADB, open the Android Studio terminal window by clicking the terminal icon on the bottom left toolbar.

Terminal Window Open
Terminal Window Open

Before using the ADB command line interface, you need to add the path of the ADB tool to your environment path. Typically, the ADB tool can be found in /Users/[username]/Library/Android/sdk/platform-tools/ (on Windows C:\Users\[username]\AppData\Local\Android\sdk\platform-tools\ ). Adding the ADB path into your environment path allows you to use the ADB command anywhere in a terminal session.

In the Android Studio terminal window, enter the command export PATH=”$PATH:/Users/[username]/Library/Android/sdk/platform-tools/” (on Windows set PATH=%PATH%;C:\Users\[username]\AppData\Local\Android\sdk\platform-tools) replacing [username] with the username on your computer.

Your terminal is now ready to use the ADB command. In the terminal window, type the command adb devices. The terminal output will list all the devices that are currently connected to Android Studio.

ADB Commands
ADB Commands

Setting the ADB Path Properly

Setting the path to ADB in your terminal makes it easy to execute the ADB command. The problem with setting the path in your terminal comes when you close your terminal session. The export command won’t retain the ADB path variable across terminal sessions. When you close and reopen a new terminal session, it won’t be able to find the ADB command line utility. To avoid adding the path to ADB in the terminal session every time, you can have your computer operating system add the ADB path every time you start a new session.

To do this on a Mac, add the path to your .zshrc file. This file is located in your user root directory, and all commands in this file are executed every time you start a new session. You’ll need to open the file, modify it with the ADB path and save it.

  • To open the .zshrc file, enter the following command in your Android Studio terminal window: nano ~/.zshrc. The nano command will open the file in the Nano editor. Use a different text editor to make the changes if you prefer.
  • Paste the following command into the file export PATH=”$PATH:/Users/[username]/Library/Android/sdk/platform-tools/” replacing [username] with the username on your computer.
  • Use the control+o keyboard command to save the file. The nano editor will prompt you to confirm the file name. The file should be /Users/[username]/.zshrc. Replace [username] with the username on your computer.
  • Press Return to confirm and save the file.
  • Use Control-X to exit editing.

To set the ADB path properly in Windows, add it as an environment variable to your system path. Your system path is the system variable your operating system uses to locate needed executables from the terminal window. Environment variables can be accessed from advanced system settings in the control panel. The steps to access the environment variables depend on the version of Windows you’re using. For Windows 8 and 10, the steps are as follows:

  • Right-click the Start button.
  • Select System from the context menu. That opens the system screen in the control panel.
  • Click the Advanced system settings link on the left. The system properties window will open.
  • In the Advanced tab of the system properties window, click Environment Variables.
  • Under the system variables section, click the variable named Path and click Edit. This is where you will have the ability to add the ADB path. (If path doesn’t exist, you can create it by clicking the New button and setting the variable name to Path)
  • Append the following to the end of the existing variable value ;C:\Users\[username]\AppData\Local\Android\sdk\platform-tools Include the “;” and replace [username] with the username on your computer.
  • Click OK and close all remaining windows.

You may need to restart the terminal for the changes to take effect. The next time you open a terminal session, the ADB path will be retained, and you can use the ADB command as needed.

Discovering other ADB Commands

There are several useful ADB commands. The help parameter shows you all the available ADB commands. In the terminal, type adb help to see a complete list of commands available. Getting familiar with ADB ensures you have the tools necessary to control Android devices during your development process. Below are a few more common commands you should be aware of:

  • adb install : Installs an Android application on the connected device.
  • adb uninstall : Uninstalls an application from the connected device.
  • adb push : Copies a file or directory from the local machine to the device.
  • adb pull : Copies a file or directory from the device to the local machine.
  • adb shell: Opens a remote shell on the device, allowing you to execute commands directly on the device.
  • adb logcat: Displays the device’s logcat, showing system logs, debug messages, and application-specific logs.
  • adb reboot: Reboots the connected device.

Continue to the Video Demo to see a live demonstration of connecting a physical device and running an app on it.

See forum comments
Download course materials from Github
Previous: Demo 01 Next: Demo 02