Friday, November 14, 2014
Configuring the Android emulator for testing Phonegap/Cordova programs
When testing Android applications during development we have the option of running the app on a real device or an emulator. Testing the application on a real device is always wise before releasing the app to the app store because an emulator is not 100% compatible with a real Android phone. But let’s say that you have a team of multiple developers where it would be expensive to buy a device for each one of them. In this case an emulator can be used during development, with the app tested on a shared device after certain milestones are reached for example.
Fortunately the Android emulator that comes with Android Studio is a very powerful one and is rather faithful to what's out there on the market. To set it up, start by running this command on the console:
By the way, avd stands for Android Virtual Device, which you should see in the application that you just launched. If you do not have Android Studio set up, you can refer to the previous post for information on how to install and set it up.
We have no devices set up yet, so let us click the Create button to set up one.
In the screenshot above, I am trying to create a device that is close to a real Samsung Galaxy S5 Mini. Most of the things in the window are quite self-explanatory. However, there is the target option (3rd option from top) that is not so obvious and is somewhat important. If you are testing a standalone application that does not interact or require any other applications on the virtual device, then I guess that any option is fine. But if you need to have your application interact with any Google service, or if you need to install a third-party app from the app store then you need to choose one of the Google APIs options. This will allow you to add a Google account so that you can download stuff from the app store. For example, in one case I needed to have my app interact with the Facebook app, so I needed to download it from the play store first.
After clicking the ok button, the virtual device should be created after a few seconds. You can now start the emulator by clicking the Start button. Another way to start the emulator without having to go through the AVD manager each time is to launch it from command prompt:
>emulator -avd <imagename>
where <imagename> is the name that we gave to the virtual device in the previous step (i.e. Samsung_Galaxy in my case). The first time it may take longer than usual to run the emulator, but it always takes some time. So don’t fret if you see nothing happening in the emulator window, apart from some shiny android text. Google are kind enough to allow you some time for making a cup of coffee before going to the next step.
As for me, I’ll just leave you with a command that we can test the emulator with the app from the previous post. Just open the command prompt and find the cordova project that we did a couple of weeks ago, and enter the following command:
>cordova emulate android
This should build the cordova project, transfer it to the emulator and run it. Magic!