Expert Android Studio. Dundar Onur

Чтение книги онлайн.

Читать онлайн книгу Expert Android Studio - Dundar Onur страница 9

Expert Android Studio - Dundar Onur

Скачать книгу

to building and running the application. Android Studio offers different ways to compile and run projects. To simply build a project, select Build from the toolbar and then the Make Project option, as shown in Figure 2.13.

Display of Build menu list.

Figure 2.13 Build menu list

Although this option will compile and package your app, it will not execute your app on either a device or the emulator. To run the sample project, select Run from the toolbar and then the Run Application option, as shown in Figure 2.14.

Display of Run menu items in Android Studio.

Figure 2.14 Run menu items in Android Studio

Alternatively, you can click the green Play icon (or Control+R on Mac, Ctrl+R in Windows), as shown in Figure 2.15.

Display of Android Studio Run ‘Application’ button screen.

Figure 2.15 Android Studio Run ‘Application’ button

      Depending on your computer's hardware, Android Studio will spend some time to build the application and later will ask for a target device, which can also be an emulator, to run the application on. You haven't either created a virtual device or connected a real device yet, so you'll do that in the next section.

ANDROID EMULATOR

      Android Emulator is a great tool that is bundled with Android Studio. It enables your computer to emulate Android hardware and operating system to run your apps and provide a preview of how it would behave on a real device. The Android emulator enables you to test your application on a variety of screen sizes, hardware configurations, Android versions, and even different CPU architectures.

      However, the Android emulator has a bad reputation for being very slow. Developers used to make fun of it by saying, “If you optimize your app for the emulator, it will run smoothly on any device.” This may sound exaggerated but was almost true in the past.

      The main performance problem behind the emulation resulted from the ARM CPU emulation on personal computers, which mostly run on x86 CPU architecture. In 2011, Intel introduced HAXM and Google started providing x86-based Android system images, which boosted the emulator performance by using the host CPU instead of emulating a different CPU architecture.

      Installing HAXM

      To start using the emulator, you need to install HAXM and the x86 image of the desired version of Android. HAXM requires a minimum version of Android SDK 17. In most cases, HAXM installation is pretty straightforward. If you already installed the SDK (see Chapter 1), you can start creating your virtual device. If you didn't install it, you can set it up with the SDK Manager, as discussed in the “Android SDK Configuration Inside Android Studio” sidebar earlier in this chapter.

      1. Click Launch Standalone SDK Manager (refer to Figure 2.5) to open the Standalone SDK Manager shown in Figure 2.6.

      2. Scroll down to Extras and select Intel x86 HAXM Emulator Accelerator (HAXM Installer), as in Figure 2.16.

Screenshot of Intel HAXM selection in Windows.

Figure 2.16 Intel HAXM selection in Windows

      3. Once the download of HAXM is complete, you need to manually trigger its installer, from

      sdk\extras\intel\Hardware_Accelerated_Execution_Manager\ on

      Windows, or

      sdk/extras/intel/Hardware_Accelerated_Execution_Manager/ on Mac.

      Creating a New Android Virtual Device

It is very easy to create a new Android device. AVD (Android Virtual Device) Manager is the next icon. It's to the left of the SDK Manager, as shown in Figure 2.17.

Screenshot of Android Studio AVD Manager button.

Figure 2.17 Android Studio AVD Manager button

      1. Click the AVD Manager button. Because you haven't created a virtual device before, AVD Manager is currently empty (see Figure 2.18).

      2. Click the Create Virtual Device button, shown in Figure 2.18, to start creating a virtual Android device.

Screenshot of AVD Manager’s initial appearance.

Figure 2.18 AVD Manager's initial appearance

A list of available devices appears, as shown in Figure 2.19. At the top of the list are Nexus devices, which are the reference devices released by Google. The rest of the list contains common screen sizes and device properties. You are free to modify any device from the list or even create your own for testing purposes. Creating your own device might be a good idea for testing devices that you don't have access to. In addition to creating virtual devices to simulate phones, AVD Manager also supports tablets, wear, and TV.

Display of List of virtual device hardware.

Figure 2.19 List of virtual device hardware

      3. Select Nexus 5X. The Nexus 5x is one of the two reference devices released with Android 6.0. Although you will continue with Nexus 5X here, you can choose any device to create a virtual device.

      4. Select Marshmallow and make sure the target column (see Figure 2.20) includes “(with Google APIs).” In this step, you are free to choose either the ARM or Intel-based Android images listed in Figure 2.20.

System Image selection for AVD screen.

Figure 2.20 System Image selection for AVD

      Nexus 5X is an ARM-based device. Although selecting ARM will provide more accurate device/virtual device testing, emulating ARM on an x86-based laptop will require additional memory and processing power and will result in performance issues. By installing HAXM, you can have your virtual device run an Intel image to provide better performance.

      5. Click the Next button shown in Figure 2.20 to tweak final settings of your virtual device.

      6. Make the final configurations, as shown in Figure 2.21, and click Finish.

AVD configuration screen.

Figure 2.21 AVD configuration window

That is it – you created a virtual device that can run your sample project. To run the emulator, open AVD Manager and click the Play icon shown in the Actions column of the virtual device, as shown in Figure 2.22.

Display of List of created virtual devices.

Figure

Скачать книгу