Instruction 1
A Brief History Of Emulators
Before you embark on developing your Android app, you’ll need an Android device to install the app and see what it looks like. Imagine having the power to test and refine your creations on many Android devices without having to buy a single one. That’s where Android emulators come into play. Emulators are your ticket to a more flexible, efficient, cost-effective app development experience.
Android emulators have undergone significant changes since their inception. In the early stages, Android emulators could only show you what your screens would look like and offered basic user interaction. They were rudimentary and lacked the sophistication, performance, and configuration available to emulators today.
Google realized the need for developers to create virtual devices with different configurations for testing their applications in various scenarios. In response to that need, Google created the Android Virtual Device (AVD) as part of the Android SDK. AVDs can be configured to simulate different device characteristics such as screen size, resolution, Android version, and hardware features. Developers use AVDs to ensure their apps function correctly across a range of Android devices and configurations during the development process. The AVD is an Android emulator serving as a virtual representation of an Android device.
The first emulators primarily used CPU-based emulation, resulting in sluggish performance and inadequate responsiveness for app testing and development. In 2013, Intel developed Hardware Acceleration (HAXM) to address the performance limitations of software-based Android emulators. The main problem HAXM solved was the slow execution speed of Android emulators, which relied solely on software-based emulation. By offloading certain emulation tasks to the hardware, HAXM dramatically increased the performance of the Android emulator, making it more responsive and closer to the experience of running applications on physical Android devices.
Emulator improvements continued with the addition of Hyper-V support for Windows-based systems. This allowed for better integration with Windows features and improved overall performance. Hyper-V support provided an alternative to HAXM for those who preferred or were required to use Hyper-V on their Windows machines.
Today, Android emulators have reached a high level of maturity. They offer a range of features, including support for various Android versions, hardware profiles, and advanced configurations. Emulators continue to be an integral part of Android development, providing developers with powerful tools to test and debug their applications across a broad spectrum of virtual devices.
Understanding Emulator Categories
Emulators are categorized according to device type. Device categories represent predefined configurations that simulate various screen sizes, resolutions, and hardware characteristics. These categories help you test your Android apps on a diverse range of virtual devices to ensure compatibility and responsiveness across different form factors.
Android Studio HedgeHog version 2023.1.1 provides seven different device categories.
- Phone: The phone category represents a generic smartphone with a standard screen size and resolution. It provides you with a baseline for testing apps on typical mobile devices.
- Tablet: The tablet category simulates a generic tablet with a larger screen size and higher resolution than a typical smartphone. This category allows you to optimize your apps for larger display areas.
- Wear OS: The Wear OS category emulates devices running Android Wear, the platform for wearable devices like smartwatches. You’ll use this category if you’re developing an Android watch app. It enables testing of apps designed for smaller screens and different interaction patterns.
- Desktop: The desktop category emulates Android devices running on Chromebooks, which are laptops running Chrome OS. It allows you to test compatibility with the Chrome OS environment.
- TV: The TV category represents devices running Android TV, optimized for television screens. It facilitates testing of your apps designed for a TV viewing experience.
- Automotive: The automotive category simulates devices running Android Automotive, an Android variant for in-car entertainment and navigation systems. It allows you to test and optimize your apps for the automotive environment.
- Legacy: The legacy category represents a much older set of smartphones that are less popular than they used to be. If your app needs to be backward compatible with older screen sizes, hardware, and dimensions, you may find emulators for those devices here.
Creating Your First Emulator
The device manager in Android Studio is the tool you’ll use to create emulators. Find the device manager icon on the far right toolbar of Android Studio. Click the Device Manager icon to open the device manager window. (You can also use the Android Studio menu under View ▸ Tool Windows ▸ Device Manager to open the window.)
The device manager displays a list of physical and virtual devices that have been connected or created in Android Studio. The list will be empty if you have not yet created or connected an Android device. To create a new emulator, click the + icon in the device manager, and you’ll see the Virtual Device Configuration screen.
The virtual device configuration screen shows a list of categories on the left and a set of preconfigured devices on the right. Select the Phone category, and you’ll see a list of phones on the right panel. The list is a set of phones that match the hardware profile and the characteristics of devices available in the market.
Choosing A Device Definition
The list of devices contains columns that display the size, resolution, and density that each configuration will come with. One of the columns you should be aware of is the Play Store column. If you see a Google Play icon in the column, it means that the AVD will have the Google Play Store app configured and access to Google Play services. This will be important to know when you need an emulator with Google Play services capability. If you can’t find a device configuration that fits your needs, you can import hardware profiles, create a new custom hardware profile, or clone an existing profile directly from this screen.
To emulate a Pixel 7 device, select the Pixel 7 device and click Next. The system image window will be displayed. It gives you the ability to choose a system image for your emulator.
Selecting A System Image
The system image window displays a list of system images grouped by tabs. System images are composed of an API Level, ABI, and Target. The API Level refers to the Android operating system version. The target refers to the Google APIs and Google Play Services available for that image. ABI stands for Application Binary Interface. In simpler terms, ABI is the architecture of the processor.
The first tab, Recommended, as the name implies, displays a list of recommended system images. The ARM Images tab displays the system images with ARM-based processors and a wider variety of targets. The Other Images tab displays a more extensive list of x86 processors, ARM processors, and target combinations.
Downloading A System Image
Android Studio doesn’t have a default system image, so when you create your first emulator, you won’t have any system images downloaded. Identify which system images are downloaded and ready for use by looking for a download icon next to the release name and by the color of the release name. If the system image is not downloaded, it will have a download icon next to the release name, and the font color will be grey (disabled). If the system image is downloaded and ready to use, there will be no download icon, and the font color will be solid black.
From the Recommended tab, click on the system image with the Release Name UpsideDownCake, API Level 34, ABI arm64-v8a, and Target Android 14 (Google Play). This is the basic Android 14 system image. The details screen on the right side will provide more information about the system image selected. Now, click the Download Icon next to the UpsideDownCake release name. A download dialog box will appear as the system image is installed in your SDK path. Some system images are over 1GB, so downloading might take a while. You only need to download a system image one time. Once you download the system image, you can re-use it for any other emulator you need to create.
When the download is complete, click Finish.
You’ll be brought back to the System Image selection screen. There’s no download icon next to the release name this time, and the font color will be black. You’re ready to continue with creating the emulator. Click to highlight the row with the release name UpsideDownCake, then click Next. You’ll be taken to the Verify Device Configuration screen.
Verifying The Configuration
The final step in the emulator creation process is verifying the selected hardware and system image and giving it a name. Leave the default name for now. The default name gives you a clear description of the type of emulator you created. It will be listed in the device manager window after the emulator is created. If the hardware and system image are incorrect, you can navigate to the previous screen and make the necessary changes.
The verify configuration screen allows you to change a few more settings of your emulator. Click the Show Advanced Settings button and scroll down to see all the modifications you can make.
Click Finish to complete the creation of your emulator. After the emulator creation process, you’ll be brought back to the Android Studio IDE, and your new device will be listed in the device manager window.
Running The Emulator
Now that your emulator is created, you’re ready to run it. In the device manager window, find the emulator you just created with the name Pixel 7 API 34. Click the Play icon to start the emulator.
The Running Devices window will open as the emulator starts to load. Depending on your computer and the emulator configuration being used, the emulator may take some time to load. While the emulator loads, you’ll see a progress indicator at the bottom of the Android Studio IDE with a label that reads Starting AVD. When the emulator is done loading, the running devices window will display the emulator.
The running devices window is where you can interact with the emulator. Click around and see that it behaves just like a real Android device.
Managing the Emulator
Switch back to the device manager to see how to stop and manage the emulator. Notice that the start icon has changed to a stop icon.
To stop the emulator, click the stop icon. The emulator will save its state and will stop. Stopping an emulator is like powering off a physical device. Now that it’s stopped, the emulator won’t be visible on the Running Devices screen.
Each virtual device in the device manager has a vertical kabob icon. That icon opens a context menu with options to manage the virtual device. The most common options are delete, cold boot, wipe data, edit, and duplicate.
Next, you’ll learn how to quickly duplicate an emulator. Click the kabob icon for the Pixel 7 emulator. The context menu will expand and display additional options. In the context menu, click duplicate and the Virtual Device Configuration window will display. The configuration will be an exact copy of the Pixel 7 emulator. The only thing you need to do is give it a unique name. Type in Duplicate Emulator as the name, and click Finish. You’ll see the duplicate emulator appear as another device in the device manager.
Now, you’ll learn to edit an existing emulator. Click the kabob icon of the duplicate emulator, then click edit. The virtual device configuration screen will display. Click the show advanced settings button and scroll to the emulated performance section.
Change the boot option to cold boot. This will force the emulator to clear its state and always start from a clean power-up when you run it. Click finish to save the changes.
In certain cases, you’ll want to wipe all the data from your emulator. To wipe the data from your duplicate emulator, click the kabob icon and then click wipe data option. A dialog box will appear asking you to confirm the data wipe. Click Yes, and your emulator will be wiped of any unnecessary data.
If you no longer need an emulator or created one in error, you can always delete it. Open the context menu of the duplicate emulator one last time and click delete. A dialog box will appear asking you to confirm the deletion. Click Yes, and your emulator will be deleted.
By default, your emulator is set to quick boot mode. That saves the state of the emulator and makes the next startup faster. The drawback is that the state is saved every time and you don’t always start from a clean initialization. On a physical device, you’d typically power off the phone and power it back on. The same can be done to the emulator by triggering a cold start. In the device manager, click the kabob icon of your Pixel 7 emulator. Click cold boot and this will clear the state of the emulator and start it as if it were powered off, and powered back on. Emulators are powerful tools for Android development. You should now be comfortable with running and managing an Android Emulator.
Continue to the Video Demo to see a live demonstration of setting up and configuring an emulator.