Android OS is used on a large number of devices with different screen sizes, processing power, memory and sensors etc. There is also a high possibility that your intended user base will have different versions of Android installed on their devices. This means that you will have to be very careful when developing android apps and test them thoroughly for a great user experience.
There are only so many physical devices on which you can test your apps. Fortunately, Android Studio provides us a great alternative in the form of an emulator which can create virtual devices with your specified constraints.
In this tutorial, I will show how to add virtual and physical devices to your list of devices in Android Studio.
You can open the Device Manager in Android Studio either from the right-side Tool Window Bar or from the main menu bar by navigating to View > Tool Windows > Device Manager.
Once the Device Manager tool window is open you will see a list of virtual and physical devices listed under the tabs Virtual and Physical respectively.
With the Device Manager tool window open, you will find a Create Device button under the Virtual tab. Clicking on the button will open a new window which will ask you to select the hardware for your virtual device.
The devices are divided into multiple categories like Phone, Tablet, Wear OS, TV, Automotive etc. We will be creating a new virtual phone here.
The pop up window for virtual device configuration lists some characteristics of the devices that we are creating such as the size, resolution and display density etc. Go ahead and select any device you like, I am selecting Pixel 6 Pro for this tutorial.
There is also an experimental phone device with resizable screen size that can resize to four different sizes covering a phone, a foldable device, a tablet and a desktop. Click the Next button after you have chosen your device. You should now see the following screen where you have to select a system image to install on your device.
Select an image from under the Recommended tab for better compatibility and optimum performance. We will go ahead with the Android 13 image with release name Tiramisu as you can see in the above image. The Next button might be disabled for your selected system image. In that case, you can click the download icon besides the name of the system image to download it first.
Once the image has been downloaded, you will be able to click the Next button to install it. You will be able to specify a name for your AVD on the next screen and verify other information such as the chosen device and system image. You can also set up values for startup orientation and hardware or software based rendering.
We will leave everything to default values for now and click Finish. You will then see the virtual device listed in the Device Manager tool window as shown below.
You can click on the launch icon or button to to launch the AVD or Android Virtual Device in the emulator. You can even launch multiple AVDs together. They will be loaded under different tabs. Once launched, you will see the AVD in your emulator window as shown below.
We have a tutorial on Tuts+ where we learn how to use the proximity and gyroscope sensors in Android devices. We wrote the code for a simple activity that will detect how close we are to the device using the data from the proximity sensor and then change the screen to red if we are too close.
In that tutorial, we installed the app on a physical device but Android Studio gives us the option to simulate the information of these sensors on its AVDs. You can install the same proximity sensor code on our AVD in this tutorial.
Once you have created the app, you can install and run it on your emulator by simply pressing the Run App icon in the toolbar or by navigating to Run > Run App. The keyboard shortcut Shift + F10 also works.
After the installed app has launched successfully, you will se a bunch of buttons at the top of the emulator window. Click the Extended Controls button as shown below.
The will open a new window where you can simulate a lot of functionality from real devices such as receiving a call or SMS as well as device position and proximity sensor data. In this case, we are simply interested in the proximity data. Click the Virtual Sensors option in the Extended Control window. You would see the following options under the Additional Sensors label.
Change the value of proximity sensor to something below 1cm and you will see that the screen turns red. Similarly, use any value above 1 and you will see that the screen turns green.
Some behaviors like making a call etc. can only be properly tested on a physical device. Similarly, it is a lot easier to test Bluetooth capabilities on actual physical devices for the time being.
This means that it becomes absolutely necessary to be able to test your apps on physical devices. Physical device connections can either be made with a USB or wirelessly by using WiFi. You can connect Android Studio to physical devices by following the steps in this tutorial. The following image shows two paired devices connected using USB as well as WiFi pairing.
You can run and install your app on any of the devices by selecting them from the dropdown shown in the above image. The currently selected device is Xiaomi M2101K6P. You can either select an AVD from the dropdown or any other connected physical device.
You also have the option to choose Select Multiple Devices in the dropdown and then run your app on multiple devices at the same time. Select the devices where you want to run the app and then press OK. After that, you still have to click the Run App button or press Shift + F10.
In this tutorial, we learned how to manage both virtual as well as physical devices in Android Studio. One of the advantages of using virtual devices is that they will allow you to test your app on a large variety of screen sizes as well as android versions. However, they are not meant for you to completely forego testing on physical devices. These virtual devices will also take up significant amount of hard disk space so that's something worth keeping in mind.
As you saw in the tutorial, it isn't necessary to keep all your physical devices connected through a USB. Pairing devices over WiFi allows you to do any testing much more easily.
You should always use a combination of physical and virtual devices to achieve the best results.