This video will demonstrate two ways to connect a physical Android device to Android Studio. Your Android device can connect to Android Studio using a USB cable plugged into your computer or through Wi-Fi. After connecting the Android device to Android Studio, you’ll see how to run your Android app on the device. Finally, you’ll learn what the Android Debug Bridge (ADB) is and how to use it to control your Android device.
Some initial configuration is 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. Do this by going to the Settings app on your Android device. Scroll down and tap the About phone or About tablet section, depending on your type of device. From the About screen, scroll down and tap seven times on the Build number or Build version label. As you get closer to seven taps, you’ll start seeing messages stating that you’re getting close to enabling developer mode. Continue tapping the label until the countdown gets to zero. If prompted, enter your passcode or pattern to unlock your device. You will see the message “You are now a developer!” to let you know it worked.
Now that you’ve enabled developer mode on your device, 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 System, then scroll down and look for Developer Options. Most devices will have developer options under the system screen. If you can’t find it, try searching in the main settings app screen, or retry the steps above to enable developer options.
In the Developer Options screen, scroll down and enable USB debugging under the Debugging section. 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.
In Android Studio, open the Bullseye project you created in Lesson 1. Connect your device to the 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. In the Android Studio device manager window, you’ll see your device listed.
To connect your device to Android Studio using Wi-Fi, use 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, there’s 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. There are two ways to pair your Android device with Wi-Fi. Pair using a QR code, or pair using a pairing code. Both pairing mechanisms require you to go to the developer options screen on your Android device.
On your Android device, first make sure you’re connected to the same Wi-Fi that your computer is connected to. Open the Developer Options screen in the settings app. Once you’re in 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 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. The Wireless Debugging screen will display wireless debugging information about your device.
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 pair new devices over Wi-Fi screen. The device will pair with Android Studio over Wi-Fi, and after a successful connection, the Android Studio Pair new devices over Wi-Fi screen will show a green checkmark indicating that you are now connected. 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 is very similar to pairing using the QR code. In Android Studio, open the pair new devices over Wi-Fi screen again. This time, tap the Pair using a pairing code tab. In your Android device, enable wireless debugging in Developer options again. This time, tap on 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.
The Android Studio Pair new devices over Wi-Fi screen will find and display your device on the list. Tap the Pair button in the row where your device is listed. A new window will prompt you to enter the pairing code from your device. Enter the pairing code, and 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. Click the Done button. Close the Pair Devices Using Wi-Fi window and navigate back to the device manager window in Android Studio.
Now that you have your Android device connected to Android Studio, you’re ready to run your first Android app.
Open the Bullseye app you created in Lesson 1. This is the app you’ll run on your device. Ensure your Android device is connected to Android Studio. On the top toolbar of Android Studio, click 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 that “app” is selected as the app to run. Click the Run app icon. You might face “The Application could not be installed” error and this is because you haven’t given the permission to install apps via USB. So, go to Developer Options screen and enable Install via USB option via the toggle button. Now, 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. Check the progress of the build and run at the bottom right of Android Studio. After the app is built, it is installed on your device, and you will see the app launch on your device’s screen. Your device will continue to communicate with Android Studio as long as it is connected.
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.
When your device is connected to your computer, there’s a lot that happens in the background for Android Studio to communicate with it. 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.
ADB is commonly used directly through the command line interface in a terminal. Understanding how to use the ADB will help you along the way, so it’s a good idea to understand how to use the ADB.
To interact with the ADB, open the Android Studio terminal window by clicking on the terminal icon on the bottom left toolbar. 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, use 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.
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 into 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. To open the .zshrc file, enter the command nano ~/.zshrc in your Android Studio terminal window. 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/. Press Control-O to save the file. The nano editor will prompt you to confirm the file name. The file should be /Users/[username]/.zshrc. Press Return to confirm and save the file. Press Control-X to exit editing.
You may need to restart your computer or 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 ABD command as needed.
You’ll learn several other commands as you continue your development journey. To display a list of commands, type in the adb help command.
That ends this demo. Continue with the lesson for a summary.