You can test your Android application with Android Studio in a few ways.
Let’s say that you are developing an application. To test and debug it, you can generate a testing APK file using Android Studio and then install it on your Android device. However, you would ideally want to see how your application is performing on a phone’s screen as you make small changes. For example, if your application is not yet complete but you are trying some new font styles and you want to see how they look.
Generating a new APK file after every new addition to your application would be way too tedious. So, to check out how your changes are performing on the screen, Android Studio allows you to test your app on the go with its built-in Android Emulator.
It can be tricky to set up Android Emulator, but this tutorial is here to your rescue. Continue reading to learn how to set up an Android emulator, create a virtual device, and run your application on it.
Setting Up the Android Emulator
To use the Android Emulator, you will need to download it first. You can download it from the SDK manager located in the tools.
Select Tools > SDK Manager. Then from the settings window choose Android Emulator. Click Apply and Android Studio will download the emulator for you.
An Android emulator cannot run on your computer without a virtualization tool for hardware acceleration. Fortunately Windows 10 comes preloaded with Hyper-V which is one of the best virtualization tools in the market.
You can discover Hyper-V’s status on your computer by running the
systeminfo.exe command in your command prompt.
If Hyper-V is not installed on your computer, then Android Studio allows you to install HAXM through its SDK manager. HAXM is another hardware acceleration tool. HAXM is downloaded and installed through the same window you used to download the Android emulator.
Wait for the installations to complete and then restart your computer and Android Studio.
Now, select the Tools > AVD Manager (for Android Virtual Device) menu and explore the virtual devices.
Downloading a Virtual Device
There’ll be a default virtual device in your AVD manager when you download the Android emulator. However, you can download the device of your choice that has a different screen size or other specifications to test your application for your specific purpose or on a larger scale.
When you open the AVD manager, look at the bottom left corner and you’ll see a button for creating a new virtual device.
Each virtual device comprises some hardware and software configurations. The system image of a virtual device represents its software components.
When you set out to create a new virtual device, you’ll have to determine its hardware first. This is where you select settings like the screen size, screen resolution, screen pixel density, and RAM. You can define the hardware from scratch or use the default hardware options offered by Android Studio.
In the hardware selection menu, you’ll also get to see a Play Store icon beside some of the hardware choices. The system image of these devices will have a Play Store integrated into their interfaces.
Once you’re done selecting the hardware configurations, click next and you’ll be taken to the system image menu.
A system image comprises the Android version, its API level, and ABI. These selections have to be in accordance with your project.
Android Studio asks you about the minimum SDK requirements when creating a new project. These requirements include the API level and the Android version. Recall this information and select the system image accordingly.
Lastly, the ABI tells you about the processor architecture that the system image supports. Assuming you are a beginning Android developer, I would suggest you opt for the ABI that supports x86 architecture.
Select the most relevant system image, and click Next to download it if it isn’t downloaded already.
Further customizations take place on the last screen for creating a new virtual device. If you look at the bottom, you’ll see a button for the advanced settings. Here you can explore the camera settings, network settings, performance, and storage of your virtual device as per your application’s needs.
The performance section of the settings gives you three options.
- Cold boot: It will start your device as if you’re turning it on for the first time.
- Quick boot: It will remember the last state of your device and the next time you run the Android emulator it will show you the same screen.
- Snapshot: This refers to the state of the Android emulator. You get to save the state yourself, and it’ll kick off from the same page the next time you run the emulator.
With all the settings in place, you’ve successfully created a new virtual device and it should now show up in your AVD manager.
If you want to see how it looks, open the drop-down menu from the actions column and select Cold Boot Now. The emulator will show up on your screen.
Running Your Application in the Android Emulator
If you want to test the code for your application, you will have to build and run it so that it shows up on the Android emulator.
There’s a green hammer on the top toolbar above. This builds your application. If there are no errors during the build, then look for a green play button on the same toolbar. This will run your application!
Sometimes, the emulator pops up showing you the home screen instead of your application. If this happens for you, just hit the play button again without closing the emulator and your application will execute on the virtual device screen.
Running a Sample Project
If you do not have an application to run in the emulator and just want to experiment with it, Android Studio lets you download and run a sample project. To import a sample project, open the file menu and select New. In there, you’ll find an option for importing a sample project.
This way, you can build and run a sample project on your Android emulator.
Choosing From Multiple Virtual Devices
If you’ve downloaded more than one virtual device in your AVD Manager, then you’ll have to choose which device to use for running your application.
In the same toolbar where the green play button is located, you’ll find two dropdown menus. One of those is for virtual devices. If you open the menu, you’ll see a list of your virtual devices. You can select a single device or run the application on multiple devices to compare its responsiveness and performance.
Running your device on an Android emulator is an important part of the development process. It makes it much easier to debug and test your app. Without the emulator, developing your app can become considerably more challenging.
To set up the Android emulator, you need to:
- install the android emulator
- enable virtualization on your computer
- set up a virtual device
- build your code
- and finally, run it!
This tutorial guides you through these steps to make sure you run your emulator smoothly so you can get to the more pressing task of developing your app.