Changes

no edit summary
AVDs are essentially emulators that allow Android applications to be tested without the necessity to install the application on a physical Android based device. An AVD may be configured to emulate a variety of hardware features including options such as screen size, memory capacity and the presence or otherwise of features such as a camera, GPS navigation support or an accelerometer. As part of the standard Android Studio installation, a number of emulator templates are installed allowing AVDs to be configured for a range of different devices. Additional templates may be loaded or custom configurations created to match any physical Android device by specifying properties such as processor type, memory capacity and the size and pixel density of the screen. Check the online developer documentation for your device to find out if emulator definitions are available for download and installation into the AVD environment.
When launched, an AVD will appear as a window containing an emulated Android device environment. Figure 35-1, for example, shows an AVD session configured to emulate the Google Nexus 7 device.
New AVDs are created and managed using the Android Virtual Device Manager, which may be used either in command-line mode or with a more user-friendly graphical user interface.
[[Image:android_studio_avd_running.png|An AVD instance running]]
Figure 35-1
== Creating a New AVD ==
</pre>
Once launched, the tool will appear as outlined in Figure 35-2. Assuming a new Android SDK installation, no AVDs will currently be listed:
The “skin with hardware controls” option controls whether or not buttons appear as part of the emulator to simulate the hardware buttons present on the sides of the physical Android device (such as the volume and power buttons).
Note that it may also be possible to speed the performance of the emulator by enabling the Use Host GPU option. In the event that the emulator crashes during startup when this option is selected, edit the virtual device properties and disable this option. Figure 3 5-3 illustrates the dialog with the appropriate settings implemented for a Nexus 7 emulator. Once the configuration settings are complete, click on the OK button.
[[Image:android_studio_create_new_avd.png|Creating a new AVD in Android Studio]]
Figure 35-3
== Running the Application in the AVD ==
With an AVD emulator configured, the example AndroidSample application created in the earlier chapter now can be compiled and run. With the AndroidSample project loaded into Android Studio, simply click on the run button represented by a green triangle located in the Android Studio toolbar as shown in Figure 3 5-4 below, select the Run -> Run… menu option or use the Shift+F10 keyboard shortcut:
[[Image:android_studio_run_app.png|The Android Studio toolbar Run button]]
Figure 35-4
By default, Android Studio will respond to the run request by displaying the Choose Device dialog. This provides the option to execute the application on an AVD instance that is already running, or to launch a new AVD session specifically for this application. Figure 3 5-5 lists the previously created Nexus7 AVD as a running device as a result of the steps performed in the preceding section. With this device selected in the dialog, click on OK to install and run the application on the emulator.
[[Image:android_studio_device_chooser.png|The Android Studio Device Chooser window]]
Figure 35-5
[[Image:android_sample_app_running.png|A sample Android App running in an AVD]]
Figure 35-6
In the event that the activity does not automatically launch, check to see if the launch icon has appeared among the apps on the emulator. If it has, simply click on it to launch the application. Once the run process begins, the Run and Android tool windows will become available. The Run tool window will display diagnostic information as the application package is installed and launched. Figure 3 5-7 shows the Run tool window output from a successful application launch:
[[Image:android_studio_edit_configurations.png|Editing the run configruation of a project in Android Studio]]
Figure 35-8
In the Run/Debug Configurations dialog, the application may be configured to always use a preferred emulator by enabling the Emulator option listed in the Target Device section and selecting the emulator from the drop down menu. Figure 35-9, for example, shows the AndroidSample application configured to run by default on the previously created Nexus7 emulator:
[[Image:android_studio_run_configuration.png|The Android Studio Run/Debug configuration window]]
Figure 35-9
When building and running an application for testing purposes, each time a new revision of the application is compiled and run, the previous instance of the application running on the device or emulator will be terminated automatically and replaced with the new version. It is also possible, however, to manually stop a running application from within Android Studio.
To stop a running application, begin by displaying the Android tool window either using the window bar button, or via the quick access menu (invoked by moving the mouse pointer over the button in the left hand corner of the status bar as shown in Figure 35-10).
[[Image:android_studio_android_tool_window_launch.png|Launching the Android Studio Android tool window using the quick access menu]]
Figure 35-10
Once the Android tool window appears, make sure that the Devices | ADB Logs tab is selected, and that the Emulator Nexus7 entry is selected in the Devices panel. From the list of processes located beneath the device name, find and select the androidsample process as outlined in Figure 35-11:
[[Image:android_studio_device_proceses.png|A list of currently running process in the Android Studio Android tool window]]
Figure 35-11
[[Image:android_studio_terminate_app.png|Terminating a running app in the Android Studio Android tool window]]
Figure 35-12
An alternative to using the Android tool window is to open the Android Debug Monitor. This can be launched via the Tools -> Android -> Monitor (DDMS included) menu option. Once launched, the process may be selected from the list (Figure 35-13) and terminated by clicking on the red Stop button located in the toolbar above the list.
[[Image:android_studio_debug_monitor.png|The Android Studio Debug Monitor window]]
Figure 35-13
== AVD Command-line Creation ==