Running an App

Running in a Browser

You can build the app at this stage to see if there are any syntax errors with

C:\Web\Starter cordova build

However, to fully debug the logic of your app it has to be run, or executed. For this operation we need to add a target in which it is to run. There are three options,

You can use any browser but Google Chrome has some additional diagnostic functions. If it is not already installed on your computer you have to install it. Then you have to add the browser platform to your project. . A browser is not an Android platform so it has to be added separately to your project with

C:\Web\Starter cordova platform add browser

If you have already added an Android platform the app will then run in both platforms one after the other. So if you are only testing in the browser it is best to remove the Android platform with

C:\Web|Starter cordova platform remove android

Then you can run the app in the browser with

C:\Web\Starter cordova run --browser

And it will build the app, open your browser, and run it there.

This is much the quickest way to test mobile software so it is often used as the first stage of development. However, it may declare an error before running the app in a browser. This is because it fails to find an expected favicon.ico icon file in the default HTML. Favicons are small graphics shown alongside the entry of your web page in a search engine listing. You can ignore this but you will probably want to stop the command action which is now hung up, with Crtl+C as usual.

 

Running in an Emulator

The next option is to use an Android emulator. This opens a virtual application running Android. It should run your app in that operating system. It is described as an Android Virtual Device or AVD. You can keep the browser platform but it is probably better to remove it and keep just the android platform.

We can run it with

C:\Web\Starter cordova run --emulator

But first it is best to choose your emulator. Otherwise it will default to a Google Nexus emulator which may not be ideal.

You can choose an emulator from the tools window by running the AVD manager. This lists all the AVDs installed in your system and also lets you create new ones. Clicking the Create button opens another window in which you specify the properties of the new AVD.

Then it will open the chosen emulator and try to run the app in that. Running on an emulator requires a powerful computer so you may find yourself waiting a long time for something to happen. At this point you have three options – keep waiting, give up by closing the emulator window and stopping the action with Ctrl+C then try running in a device or in a third party emulator..

There are several third party emulators available on the internet. Some can be installed as plugins in your project or they may have to be used like real devices. That means you have to find the project’s application file and copy it to the virtual device and run it there. The application file is the file android-debug.apk and it should be in Platforms/Build/Outputs/APK/.

Some emulators based on the x86 chip can be accelerated by installing another package called HAXM. This can be installed from the Android tool.

 

Running in a Device

The easiest way to test all the functionality of a new Cordova app is to run it in a real Android device. To do this the device should be connected to the computer on which the development is taking place. The connection is by USB (except in PhoneGap Desktop which uses the local web server).

The device has to be set up to allow apps to be installed from your computer. Different Android devices tend to do this in different ways so you will have to explore the settings on your Android phone or tablet. Essentially, you need to enable USB debugging by finding the control and ticking the checkbox. In some devices you first have to enable the Developer options, which usually means tapping the screen containing this option 4 or 5 times to open the access to the options. Then in some devices each time you connect to the computer you may have to respond to a screen prompt by allowing USB debugging. 

Setting up the device is quite a complicated routine and it does vary from one device to another. You may have to Google for specific instructions for your own device. However, it is an essential part of all mobile development since you really should test your software on a number of devices before submitting it to an Android store.

The project is run in the device with

C:\Web\Starter cordova run --device

The result of running the app in an attached device is that a debug version will be installed in the device. It will be represented by a standard Cordova icon and can be rerun at any time, without being connected to the computer, by tapping that icon, like all the other apps in your device. You can even distribute it to other users. It will be displayed as a standard Cordova icon over the name Starter – unless you have not changed the label from the default HelloCordova. It is called android-debug.apk and the .apk extension is that of an Android application file.

One final note is that if you make mistakes in going through the procedures described her and want to repeat them you simply have to delete the Starter folder from the Web folder in Windows and start again.