Difference between revisions of "Creating an Android Virtual Device (AVD) in Android Studio"

From Techotopia
Jump to: navigation, search
m (Text replacement - "<htmlet>ezoicbottom</htmlet>" to "")
m (Text replacement - "<hr> <table border=" to "<htmlet>ezoicbottom</htmlet> <hr> <table border=")
Line 280: Line 280:
  
  
 +
<htmlet>ezoicbottom</htmlet>
 
<hr>
 
<hr>
 
<table border="0" cellspacing="0">
 
<table border="0" cellspacing="0">

Revision as of 18:13, 11 May 2016

PreviousTable of ContentsNext
A Tour of the Android Studio User InterfaceTesting Android Studio Apps on a Physical Android Device


You are currently reading the Android Studio 1.x - Android 5 Edition of this book.

Purchase the fully updated Android Studio Hedgehog Edition of this publication in eBook ($32.99) or Print ($49.99) format

Android Studio Hedgehog Essentials - Java Edition Print and eBook (PDF) editions contain 87 chapters and over 800 pages
Buy Print Preview Book


In the course of developing Android apps in Android Studio it will be necessary to compile and run an application multiple times. An Android application may be tested by installing and running it either on a physical device or in an Android Virtual Device (AVD) emulator environment. Before an AVD can be used, it must first be created and configured to match the specification of a particular device model. The goal of this chapter, therefore, is to work through the steps involved in creating such a virtual device using the Nexus 7 tablet as a reference example.


Contents


About Android Virtual Devices

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 5-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.


An AVD instance running

Figure 5-1

Creating a New AVD

In order to test the behavior of an application, it will be necessary to create an AVD for a specific Android device configuration.

To create a new AVD, the first step is to launch the AVD Manager. This can be achieved from within the Android Studio environment by selecting the Tools -> Android -> AVD Manager menu option from within the main window. Alternatively, the tool may be launched from a terminal or command-line prompt using the following command:

android avd

Once launched, the tool will appear as outlined in Figure 5-2. Assuming a new Android SDK installation, no AVDs will currently be listed:


The Android Studio Virtual Device Manager window

Figure 5-2


Begin the AVD creation process by clicking on the Create a virtual device button in order to invoke the Virtual Device Configuration dialog:


Android studio create avd.png

Figure 5-3


Within the dialog, perform the following steps to create a first generation Nexus 7 compatible emulator:

1. From the Category panel, select the Tablet option to display the list of available Android tablet AVD templates. 2. Select the Nexus 7 (2012) device option and click Next. 3. On the System Image screen, select the latest version of Android (at time of writing this is Android 5.0.1, Lollipop API level 21) for the armeabi-v7a ABI. Click Next to proceed. 4. Enter a descriptive name (for example Nexus 7) into the name field. 5. Click Finish to create the AVD.

With the AVD created, the AVD Manager may now be closed. If future modifications to the AVD are necessary, simply re-open the AVD Manager, select the AVD from the list and click on the pencil icon in the Actions column of the device row in the AVD Manager.


Starting the Emulator

To perform a test run of the newly created AVD emulator, simply select the emulator from the AVD Manager and click on the launch button (the green triangle in the Actions column) followed by Launch in the resulting Launch Options dialog. The emulator will appear in a new window and, after a short period of time, the “android” logo will appear in the center of the screen. The first time the emulator is run, it can take up to 10 minutes for the emulator to fully load and start. On subsequent invocations, this will typically reduce to a few minutes. In the event that the startup time on your system is considerable, do not hesitate to leave the emulator running. The system will detect that it is already running and attach to it when applications are launched, thereby saving considerable amounts of startup time.

Another option when using the emulator is to enable the Snapshot option in the AVD settings screen. This option, which can only be used when the Use Host GPU option is disabled, enables the state of an AVD instance to be saved and reloaded next time it is launched. This can result in an emulator startup time of just a few seconds.

To enable snapshots, edit the settings for the AVD configuration and click on the Show Advanced Settings button. In the Emulated Performance section of the advanced settings panel, disable the Use Host GPU option and enable the Store a snapshot for faster startup as outlined in Figure 5 4:


Android studio enable avd snapshots.png

Figure 5-4

To save time in the next section of this chapter, leave the emulator running before proceeding.

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 5-5 below, select the Run -> Run… menu option or use the Shift+F10 keyboard shortcut:


The Android Studio toolbar Run button

Figure 5-5


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 5-6 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.

The Android Studio Device Chooser window

Figure 5-6


Once the application is installed and running, the user interface for the AndroidSampleActivity class will appear within the emulator:


A sample Android App running in an AVD

Figure 5-7


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 5-8 shows the Run tool window output from a successful application launch:


The Android Studio Run tool window

Figure 5-8


If problems are encountered during the launch process, the Run tool will provide information that will hopefully help to isolate the cause of the problem.

Assuming that the application loads into the emulator and runs as expected, we have safely verified that the Android development environment is correctly installed and configured.

You are currently reading the Android Studio 1.x - Android 5 Edition of this book.

Purchase the fully updated Android Studio Hedgehog Edition of this publication in eBook ($32.99) or Print ($49.99) format

Android Studio Hedgehog Essentials - Java Edition Print and eBook (PDF) editions contain 87 chapters and over 800 pages
Buy Print Preview Book

Run/Debug Configurations

A particular project can be configured such that a specific device or emulator is used automatically each time it is run from within Android Studio. This avoids the necessity to make a selection from the device chooser each time the application is executed. To review and modify the Run/Debug configuration, click on the button to the left of the run button in the Android Studio toolbar and select the Edit Configurations… option from the resulting menu:


Editing the run configruation of a project in Android Studio

Figure 5-9


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 5-10, for example, shows the AndroidSample application configured to run by default on the previously created Nexus7 emulator:


The Android Studio Run/Debug configuration window

Figure 5-10

Stopping a Running Application

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 5-11).


Launching the Android Studio Android tool window using the quick access menu

Figure 5-11


Once the Android tool window appears, make sure that the Devices | ADB Logs tab is selected, and that the Nexus7 Emulator 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 5-12:


A list of currently running process in the Android Studio Android tool window

Figure 5-12


With the process selected, stop it by clicking on the red Terminate Application button in the vertical toolbar to the left of the process list:


Terminating a running app in the Android Studio Android tool window

Figure 5-13


An alternative to using the Android tool window is to open the Android Debug Monitor. This can be launched via the Tools -> Android -> Android Device Monitor menu option. Once launched, the process may be selected from the list (Figure 5-14) and terminated by clicking on the red Stop button located in the toolbar above the list.


The Android Studio Debug Monitor window

Figure 5-14

AVD Command-line Creation

As previously discussed, in addition to the graphical user interface it is also possible to create a new AVD directly from the command-line. This is achieved using the android tool in conjunction with some command-line options. Once initiated, the tool will prompt for additional information before creating the new AVD.

Assuming that the system has been configured such that the Android SDK tools directory is included in the PATH environment variable, a list of available targets for the new AVD may be obtained by issuing the following command in a terminal or command window:

android list targets

The resulting output from the above command will contain a list of Android SDK versions that are available on the system. For example:

Available Android targets:
----------
id: 1 or "android-21"
     Name: Android 5.0.1
     Type: Platform
     API level: 21
     Revision: 2
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
 Tag/ABIs : default/armeabi-v7a

The syntax for AVD creation is as follows:

android create avd -n <name> -t <targetID> [-<option> <value>]

For example, to create a new AVD named Nexus7 using the target id for the Android 5.0 API level 21 device (in this case id 1), the following command may be used:

android create avd -n Nexus7 -t 1

The android tool will create the new AVD to the specifications required for a basic Android 5.0 device, also providing the option to create a custom configuration to match the specification of a specific device if required. Once a new AVD has been created from the command line, it may not show up in the Android Device Manager tool until the Refresh button is clicked.

In addition to the creation of new AVDs, a number of other tasks may be performed from the command line. For example, a list of currently available AVDs may be obtained using the list avd command line arguments:

android list avd

Available Android Virtual Devices:
    Name: GenericAVD
    Path: C:\Users\Neil\.android\avd\GenericAVD.avd
  Target: Android 5.0.1 (API level 21)
 Tag/ABI: default/armeabi-v7a
    Skin: WVGA800
---------
    Name: Nexus_7_2012_API_21
  Device: Nexus 7 (Google)
    Path: C:\Users\Neil\.android\avd\Nexus_7_2012_API_21.avd
  Target: Android 5.0.1 (API level 21)
 Tag/ABI: default/armeabi-v7a
    Skin: nexus_7
  Sdcard: C:\Users\Neil\.android\avd\Nexus_7_2012_API_21.avd\sdcard.img
Snapshot: yes

Similarly, to delete an existing AVD, simply use the delete option as follows:

android delete avd –n <avd name>

Android Virtual Device Configuration Files

By default, the files associated with an AVD are stored in the .android/avd sub-directory of the user’s home directory, the structure of which is as follows (where <avd name> is replaced by the name assigned to the AVD):

<avd name>.avd/config.ini
<avd name>.avd/userdata.img
<avd name>.ini

The config.ini file contains the device configuration settings such as display dimensions and memory specified during the AVD creation process. These settings may be changed directly within the configuration file and will be adopted by the AVD when it is next invoked.

The <avd name>.ini file contains a reference to the target Android SDK and the path to the AVD files. Note that a change to the image.sysdir value in the config.ini file will also need to be reflected in the target value of this file.

Moving and Renaming an Android Virtual Device

The current name or the location of the AVD files may be altered from the command line using the android tool’s move avd argument. For example, to rename an AVD named Nexus7 to Nexus7B, the following command may be executed:

android move avd -n Nexus7 -r Nexus7B

To physically relocate the files associated with the AVD, the following command syntax should be used:

android move avd -n <avd name> -p <path to new location>

For example, to move an AVD from its current file system location to /tmp/Nexus7Test:

android move avd -n Nexus7 -p /tmp/Nexus7Test

Note that the destination directory must not already exist prior to executing the command to move an AVD.

Summary

A typical application development process follows a cycle of coding, compiling and running in a test environment. Android applications may be tested on either a physical Android device or using an Android Virtual Device (AVD) emulator. AVDs are created and managed using the Android AVD Manager tool which may be used either as a command line tool or using a graphical user interface. When creating an AVD to simulate a specific Android device model it is important that the virtual device be configured with a hardware specification that matches that of the physical device.


You are currently reading the Android Studio 1.x - Android 5 Edition of this book.

Purchase the fully updated Android Studio Hedgehog Edition of this publication in eBook ($32.99) or Print ($49.99) format

Android Studio Hedgehog Essentials - Java Edition Print and eBook (PDF) editions contain 87 chapters and over 800 pages
Buy Print Preview Book



PreviousTable of ContentsNext
A Tour of the Android Studio User InterfaceTesting Android Studio Apps on a Physical Android Device