Developing apps with Apache Cordova
Hello Worlds!
To create a new project, use the Cordova tools in a working directory, which I here dubbed LinuxMagazin
:
cordova create LinuxMagazin
When first called, Cordova asks whether it can transmit telemetry data to its developers. You say no in this dialog by pressing n. For the next step, switch to the project directory created by Cordova and add the platform on which the app will run. In this example, I began with the browser:
cd LinuxMagazin cordova platform add browser
You can add more platforms with the same command, such as Android (cordova platform add android
). The cordova platform
command spits out a list of all possible platform names. The configuration file config.xml
collects all the project's basic settings. It is located in the project directory and, among other things, contains the name and a (short) description of the app.
If the project has been set up, the developer should still use cordova requirements
to check whether the computer really fulfills all the necessary SDKs and software requirements (Figure 1).
Old Friends
The cordova create
command automatically creates a simple sample app, which can be seen in the image for Figure 2. Like any Cordova app, this consists of a normal website that bears the index.html
name by default and is found in the www
subdirectory of the project directory – in this example, under LinuxMagazin/www/index.html
. You can adapt and expand this at will. The cordova create
command also creates the css
directories for all style sheets, as well as img
for pictures and js
for JavaScript code. You can change this directory structure as you like, as long as all the app content remains in the www
directory.
An HTML rendering engine supplied by Cordova (Figure 3) displays finished websites on the smartphone. WebView offers several special JavaScript functions that allow developers to access the smartphone hardware, as well as other items (see the "Functional and Productive" box).
Functional and Productive
The smartphone is accessed on the app via the relevant JavaScript functions. For example, the following command will make the phone vibrate for one second:
navigator.vibrate(1000);
Cordova provides this JavaScript function.
Additionally, the framework triggers a JavaScript event in certain situations. For example, the menubutton
event occurs if you press the menu button on your smartphone, which you intercept with a suitable event listener:
document.addEventListener("menubutton", onMenuKeyDown, false);function onMenuKeyDown() { // Button pressed! }
In this example, the code calls onMenuKeyDown()
as soon as the smartphone user presses the menu button. Some events, however, occur only on specific operating systems. The menubutton
event is absent from iOS and Windows Phone, for instance. All the Cordova events and functions are listed in the Cordova documentation [8].
The WebView provides only a few core functions, which plugins can then expand (Figure 4). A plugin makes it possible, for instance, to query contacts, whereas another allows access to the camera. Thanks to the plugin option, you can flexibly add further functions and address new hardware, but the app only carries programming code for the functions it really requires. The Cordova project itself is developing some plugins for the most important functions, including access to the camera, contacts, or the geolocation API.
In a newly created Cordova project, the programmer's first step must be to activate the required plugins individually:
cordova plugin add cordova-plugin-vibration
In this example, Cordova added the plugin for a vibrating alarm. The Apache Foundation has a directory of available plugins on its site [9], all of which can be added with the cordova plugin add
command. Technically speaking, it runs npm
in the background on the hard drive.
If you attach the --save
parameter to the command, Cordova enters information, including the version number of the plugin, in the config.xml
file. To return to a known state with precisely the plugin versions originally used, you enter the cordova prepare
command.
Test Run
The cordova run <browser>
command starts the finished app. In place of <browser>
, you use the name of the platform on which the app is intended to run. If browser
is used, Cordova automatically boots a minimalistic web server in the background that awaits requests by default on localhost
port 8000
. You then access your app by typing http://localhost:8000/index.html into the URL bar of the browser.
When the web server runs, Cordova starts the Chrome browser (Figure 2), unless it is not installed, in which case Cordova does not run the app, and the web server is promptly stopped. As a result, your only option as an app developer is to install Chrome or do without the browser as a possible platform. If you close Chrome, you must then force the web server to shut down by pressing Ctrl+C.
If the browser is not the target, Cordova starts the app on the appropriate platform, which runs on either a plugged-in device or an emulator. Running
cordova run --list
lists all the existing devices and emulators. You can explicitly choose one of the devices with --target
; in the following case, this would be MyAndroidVM
:
cordova run android --target=MyAndroidVM
To start it on an emulator, a shortcut boots a virtual Android platform,
cordova emulate android
This command also works for other platforms, with a --list
displaying their respective version numbers in detail.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.
-
Linux Foundation Adopts OMI to Foster Ethical LLMs
The Open Model Initiative hopes to create community LLMs that rival proprietary models but avoid restrictive licensing that limits usage.
-
Ubuntu 24.10 to Include the Latest Linux Kernel
Ubuntu users have grown accustomed to their favorite distribution shipping with a kernel that's not quite as up-to-date as other distros but that changes with 24.10.
-
Plasma Desktop 6.1.4 Release Includes Improvements and Bug Fixes
The latest release from the KDE team improves the KWin window and composite managers and plenty of fixes.
-
Manjaro Team Tests Immutable Version of its Arch-Based Distribution
If you're a fan of immutable operating systems, you'll be thrilled to know that the Manjaro team is working on an immutable spin that is now available for testing.