Running Your App on a Device
To run an Android application we need a device—either a hardware device or a virtual device. Virtual devices are powered by the Android emulator, which ships with the developer tools.
The Android Emulator simulates Android devices on the computer so that we can test our application on a variety of devices and Android API levels without needing to have each physical device.
The emulator has almost all of the capabilities of a real Android device. We can simulate user gestures, incoming phone calls and text messages, specify the GPS location of the device, simulate different network speeds, simulate rotation and other hardware sensors, access the Google Play Store, and more.
Navigating the Emulator Screen
Swipe the screen: Point to the screen, press and hold the primary mouse button, swipe across the screen, and then release.
Drag an item: Point to an item on the screen, press and hold the primary mouse button, move the item, and then release.
Tap (touch): Point to the screen, press the primary mouse button, then release. For example, you could click a text field to start typing in it, select an app, or press a button.
Double-tap: Point to the screen, press the primary mouse button quickly twice, and then release.
Touch and hold: Point to an item on the screen, press the primary mouse button, hold, and then release. For example, you could open options for an item.
Type: You can type in the emulator by using your computer keyboard, or using a keyboard that pops up on the emulator screen. For example, you could type in a text field after you selected it.
Pinch and spread: Pressing Control (Command on Mac) brings up a pinch gesture multi-touch interface. The mouse acts as the first finger, and across the anchor point is the second finger. Drag the cursor to move the first point. Clicking the left mouse button acts like touching down both points, and releasing acts like picking both up.
Vertical swipe: Open a vertical menu on the screen and use the scroll wheel (mouse wheel) to scroll through the menu items until you see the one you want. Click the menu item to select it.
Run Apps on a Hardware Device
Android Studio also makes it convenient to test our apps on real physical devices. Running an Android app for testing on a real device is faster than running an emulator. We will look at how to set up our development environment and Android device for testing and debugging over an Android Debug Bridge (ADB) connection.
Steps to Enable USB Debugging
- Plug-in your Android Device to Computer via USB
- Open the “Settings” App on the Device
- Scroll down to the bottom to find “About phone” item
- Scroll down to the bottom to find “Build number” section
- Tap on “Build Number” 7 times in quick succession
- You should see the message “You are now a developer!”
- Go back to main “Settings” page
- Scroll down the bottom to find “Developer options” item
- Turn on “USB Debugging” switch and hit “OK”
- Unplug and re-plug the device
- The dialog appears “Allow USB Debugging?”
- Check “Always allow from this computer” then hit “OK”
Running Your App:
- Select one of your projects and click “Run” from the toolbar.
- In the “Choose Device” window that appears, select the “Choose a running device” radio button, select the device, and click OK.
- Once Gradle finishes building, Android Studio should install the app on your connected device and start it.