Join David Gassner for an in-depth discussion in this video Connect a physical device for testing, part of Android App Development Essential Training (2015).
- Whenever possible, I recommend testing your apps with physical Android device. You'll get a more realistic sense of how your app will behave for your actual users. And there are many things a physical device can do that are difficult to emulate with a virtual device. For most of this course, I'll be demonstrating my sample apps with this Nexus 5X cell phone running Android 6 Marshmallow. I've connected it to my development computer with a USB cable. This device requires using a USB-A to USB-C cable, the type of cable that's also used on the Nexus 6P and on a few other newer mobile devices.
If you have an older Android device, you'll need the more common USB-A to micro USB cable. But the two cables do exactly the same thing, sending messages and data between the Android device and your development computer. To make it easier to see what I'm doing on the device, I'm projecting a live image of its screen to my computer using Android casting. On the device, I'll go to the Cast feature. And then, I'll find my computer and display the device.
And I'm receiving and displaying the image on the computer with Reflector 2. So you're seeing the actual physical device and not a virtual device running in the emulator. When I manipulate the app on the device, you're also seeing it on the computer screen. After connecting your device to your computer using a USB cable, you should check to make sure that it's properly connected. On Mac, the easiest way to do this is to go to your launcher screen, then look at your notifications by dragging down from the top.
Now these steps might differ depending on what kind of device you have and what version of Android you're using. I'm working with a Nexus 5X running Marshmallow. On my device, I have two notifications, one for Chromecast, indicating that I'm casting my screen to my computer screen, and the other for my USB mode. The default USB mode on Marshmallow is charging. I'm going to change that to photo transfer, and that results in opening the Photos application.
Now, this all depends on certain default settings. The Photos application needs to be set to open automatically when you connect a camera. If it doesn't open, try opening the Photos application and then click Import and see if the device opens then. If none of that is working for you, then your device might not be connected properly. You should check the cable and do some other troubleshooting. If you're working on Microsoft Windows, you'll need to have a driver installed to connect to your physical device.
When you connect the device, the driver might be installed automatically depending on which version of Windows you're using. To find out whether it's connected correctly, go to your Control Panel, and then go to the Device Manager. I'll type the word "device," and then choose the Device Manager link. On my computer, I have a category labeled Portable Devices, and I see my device correctly attached. If your device isn't showing up, then, you'll need to install a driver.
And depending on the particular device and your version of Windows, it'll show up in some category here. It might be under something called Mobile Devices or in some other category. If you're unable to find it, disconnect the device's USB cable, and then with the Device Manager screen open, reconnect it. You might see a message indicating that new hardware has been installed, or you might see a new item appear on this list. Once you get to the item, go into the link to update the driver software.
And then, you'll need to browse your computer for the driver software. Now, you'll need to have downloaded the driver software. And if you're working with a Nexus device, you can download it through the SDK Manager. In the Android Studio welcome screen, I'll click configure, and then SDK Manager. Then, I'll click the tab for SDK tools. I'm looking for this item labeled Google USB Driver. Now again, this is only for Nexus devices.
If you have a device for a Motorola or HTC or Samsung, you'll need to get the driver from that vendor. But I'll select this option and click Apply and confirm. And then after just a few moments, the device driver should be installed. I'll click Finish and OK and then minimize my Android Studio window. Then I can browse my computer for my driver software. And I'm going to start in the folder Android\SDK underneath my home directory.
From there, I'll choose the extras subdirectory and then, under that folder, the USB driver folder. I'll click OK and Next. And again, my device is already connected, so I see the message to that effect. But you might see a message indicating that the driver software is being installed. After you close that dialog, you should now see the device listed. Once you verified that your device is properly connected to your computer with the USB cable, the next step is to turn on a feature called USB debugging.
On my device, I'll go to my launcher screen. Then I'll go to settings. On Marshmallow or Lollipop, just drag down twice. And then touch the gear icon to go to the settings app. Then scroll all the way down to the bottom. You're looking for an option named Developer Options. On a new Android device, this won't be visible. But on a Nexus device, you can turn it on by touching About Phone, then scrolling down to the bottom, and then touching Build Number at the bottom seven times.
The third time I touch it, I get the message that I'm four steps away. And then, I'll touch it a few more times, and I get the message, "You are now a developer." Then I'll go back a step, and down at the bottom I see Developer Options. I'll touch that option, and then I'll turn on two options. Stay awake means that the device won't go to sleep while it's connected with the USB cable. That's just a convenience, but the important one is USB debugging shown here at the bottom of the list.
When I touch that icon, I get a confirmation message. And then I'll touch OK. There's another option you need to know about called USB Debugging Authorizations. I'm going to revoke any existing authorizations so you can see what'll happen on your device. Then, I'll go to a command prompt. On Windows, I'll just go to the Command Prompt option. And on Mac, you can go to Terminal. Now the next step requires access to your command line tools.
If you've added your command line tools to your path, you should be able to type adb and then see some output. If you can't find the application, you'll need to switch to the actual directory containing the adb command. In my computer, I'll go to the SDK directory, which is under Android\SDK. Then, I'll go to the Platform Tools directory. And once again, I'll type adb, and press Enter or Return.
On Mac, type .\adb. Now I'll type adb devices. And if the device is correctly attached, I'll see a unique identifying string. Then, I'll verify the device in one other way. I'll go back to Android Studio where I've opened my Hello World application. Then I'll click Android Monitor, and I see my device listed here. If you can see your device correctly, then you're ready to continue working with Android Studio debugging and testing your application.
You might also see a request for authorization when you plug in your phone. On my device, I'll go back to the launcher screen. Then I'm unplugging USB cable. And the icon for USB Debugging disappeared from the top left. Then I'll plug it back in again. And this time, because I revoked authorizations, I'm asked if I want to allow USB debugging. I'll touch the option to always allow this from this computer, and then OK, and then once again, USB debugging is turned on.
So once you have USB debugging turned on, you're ready for the full development cycle. You'll be able to build your applications and then test them immediately on your device. And using USB Debugging, you'll be able to debug and trace everything that's going on on the device.
- Installing and configuring Android Studio
- Creating virtual devices and connecting physical devices for testing
- Working with project files such as the app manifest and Gradle scripts
- Defining the user interface
- Modifying material design themes and styles
- Adding views
- Displaying messages
- Handling events and changes in screen orientation
- Displaying images
- Managing navigation with activities and layouts
- Supporting different screen sizes
- Working with data