In this post, we will learn how to run or debug android app using emulator in android studio. We will see how we can install AVD in android studio using AVD Manager, run android app in emulator and debug android app in emulator etc.
This post can be divided into below sub-sections –
- Install And Manage AVD Using AVD Manager
- Run Android App Using Emulator
- Debug Android App Using Emulator
1. Install And Manage AVD Using AVD Manager
If we want to run our HelloWorld app in emulator in android studio, we need to create an android virtual device (AVD). Later, we use AVD to run our app in emulator.
As per official documentation,
An Android Virtual Device (AVD) is a configuration that defines the characteristics of an Android phone, tablet, Wear OS, Android TV, or Automotive OS device that you want to simulate in the Android Emulator.
You might be confused what is an emulator then ?
As per official documentation,
The Android Emulator simulates Android devices on your computer so that you can test your application on a variety of devices and Android API levels without needing to have each physical device.
So, AVD contains real device configurations that emulator uses to simulate android device where we install and run our application to test and verify features.
1.1 Using AVD Manager to Create AVD
Android Studio provides AVD Manager that helps developers to install and manage AVD in android studio. AVD stands for android virtual device.
Follow steps below to install android virtual device –
Look for below icon in android studio –
Click on AVD Manager icon. It opens new modal –
Click on + Create Virtual Device
Now, we need to select hardware. We can even choose whether we want emulators for watch, TV, Phone or Tablet. Then, we need to select device(version, resolutions etc.) we want to install. We selected Phone => Pixel XL. Then, click Next button.
- Clicking on Next button redirects to next screen to select system image.
If you already have system image downloaded and installed, you can select and proceed. However, please note that there is no system image present in our case. So, we need to download and install one. Here, we are downloading android version R. Click on download link beside R.
Clicking on download link shows download in progress –
Note: It may take several minutes to download based on internet speed.
This is how modal looks when download is complete.
Click on Finish button. It will redirect back to window where we need to select system image.
Notice that there is no download link beside android version R. Now, select R. Then, click on Next button.
- Clicking on download link shows download in progress –
Clicking on Next button redirects to below screen. Here, we need to select AVD Name. Leave other details as default value. For the sake of simplicity of this post, we are not going into that detail. Then, click on Finish button.
Clicking on Finish button redirects to below screen. This screen shows installed virtual devices we have in our system. In our case, we have only one – which we just installed.
Notice that there is a play button under Actions column. Click on this button to launch it.
Clicking on Play button launches this AVD in emulator. It will be shown like below –
Now, we can run or debug our android application in this emulator. This emulator is using real device configuration provided by our installed and launched virtual device (Google Pixel XL, API level 30 etc.). If we run any app in this emulator, it will be like running that app in real device with provided software and hardware details.
2. Run Android Application in Emulator
Till now, we went through how to install and manage android virtual device (AVD) using AVD manager in android studio. Now, we will run our HelloWorld Android Application in this emulator.
Follow steps below –
- Find run icon in Android Studio. It looks like below –
Note that our launched virtual device is shown as pre-selected beside launch icon.
Click on that icon. Now, wait for few seconds. It will launch HelloWorld application in emulator.
- Finally our application in emulator will look like below –
We have successfully run android application in emulator. Our HelloWorld application is running on emulator using Google Pixel XL virtual device.
Now, what if we want to debug our application?
How can we debug our application in emulator ?
3. Debug Android Application in Emulator
Till now, we saw how to run android application in emulator. Now, we will learn how to debug android application in emulator.
Follow steps below to debug –
There is an icon to debug android application. It looks like below –
Now, goto MainActivity.kt file. We want to debug line setContentView(R.layout.activity_main).
Notice that there is line number mentioned on left side of each line. We have to click to right side of the text. When we click on it, it adds a red circle. If red circle is shown, it means we have added a break point. Whenever this line will be executed, application will stop at this line. Check below video
Similarly, we can add break point at any executable line and debug our android application.
Thus, in this post, we learnt how to run and debug our android application in emulator.