While developing applications with Android Studio, we need to test them to check how it works or any bugs etc. But where to test out put and how. Well.. There is Android Emulator available with Android SDK and you test your application with it. i.e. Virtual Device. Even you can test your app on your Physical Device too.
In the previous chapter we learnt, how to create new project with Android Studio. In this chapter we we learn how we can create virtual device and run application with it. As well as we will learn how to connect physical device to Android Studio on your PC and run application on it.
Creating Virtual Device
With the few steps you can create Virtual Device with Android Studio
Step 1: Open Android Studio
Step 2: Open AVD Manager from the top toolbar of the Android studio.
Step 3: Once AVD manager will get opened. Click on “Create Virtual Device” Button.
Step 4: Choose Device definition and click next. In my case, I am going to choose Nexus 4 definition.
You can create new profile by clicking on New Hardware Profile if you are not ok with existing definitions or import saved definitions too.
Step 5: Now choose system image and click next.
I would like to choose Kitkat x86. Only choose x86 images, If you have installed HAXM(Hardware Accelerated Execution Manager). It accelerate emulator to run faster. To know more about HAXM click here..
Step 6: Now click on “Show Advanced Settings” to view more configuration settings.
Step 7: Here you can set RAM size and Internal Storage you want. In my case I am only setting RAM size to 1024MB for device I am creating. Once you done with all settings click on Finish.
Once you finish you will see the device is created and now its ready to test application.
Running App with Virtual Device
To run your application on Virtual device while development time, you just need to click on Run button from the toolbar on top.
Once you click on Run button, you will see the dialog box to choose devices.
If any Virtual Device is already running on your pc it will show you under Running devices list. Otherwise you can choose device from Android virtual device list to launch emulator and run app on it.
To run application on same device again, enable “Use same device for future launches” option bottom of the dialog box and click OK button.
Once click on OK button you can see the emulator will start launching and run your app. If you emulator is already running and you chosen it from running devices, the application will run on already running virtual device.
Running App on Physical Device
Before run your app from Android Studio on physical device, you need to do few things.
Make sure your ADB(Android Debugging Bridge) drivers are properly installed on your pc.
Step 1: Now connect your device to your PC via USB Cable.
Step 2: Go to Settings>Developer Options and enable USB debugging.
Step 3: From the Android Studio run the application.
Once you click on Run button the Choose Device dialog will appear. Where you must see your physical device in the list of running device list.
Step 4: Choose your device from the list.
And here you can see your application is runting on your phone which you connected to pc via USB Cable.