Android Development

Issue Tracker Android Updates Android Home Home Contact

Introduction to Apache Cordova

Apache Cordova is an open-source framework for building mobile applications using web technologies such as HTML, CSS, and JavaScript. It enables applications to run inside a native WebView component, providing access to native device APIs through a JavaScript-to-native bridge.

What Cordova Means for Android Development

Cordova allows building Android applications using a single codebase written in web technologies. Applications are packaged as native APKs and can access device features such as camera, geolocation, and file system through Cordova plugins.

Main Components of Cordova

  • WebView: The container used to render HTML, CSS, and JavaScript content inside the native app.
  • Plugins: JavaScript interfaces to native device capabilities implemented in Java for Android.
  • Config.xml: The central configuration file that defines app metadata, permissions, and plugin references.
  • CLI Tools: Command-line utilities to create, build, and run projects across platforms.

Integration with Android Build System

Cordova integrates with the Android SDK and Gradle build system. A Cordova project contains a native `platforms/android` folder that includes a full Gradle-based Android project. This allows customization or extension using Android-specific configurations or code if needed.

Code Editing in Cordova Projects

Application logic and UI are written in standard HTML, CSS, and JavaScript files located under the `www/` directory. Any text editor or IDE can be used to edit this content. Native platform code and plugin code are stored in platform-specific directories.

Main Features of Cordova

  • Cross-platform deployment using a single web-based codebase.
  • Plugin-based access to native device APIs such as camera, geolocation, file system, and sensors.
  • Support for custom plugins to extend app capabilities beyond core APIs.
  • Active community and wide plugin ecosystem for common device features.

Communication with Native Android Code

Cordova uses a JavaScript-to-native bridge to communicate between web code and Android code. Plugins expose JavaScript interfaces that internally call Java methods in Android using reflection and JSON-based messaging.


// JavaScript call to Cordova camera plugin
navigator.camera.getPicture(onSuccess, onFail, {
    quality: 50,
    destinationType: Camera.DestinationType.FILE_URI
});

Basic Setup for Cordova on Android

  • Node.js: Required for installing Cordova CLI and managing project dependencies.
  • Android Studio: Required for Android SDK and emulator.
  • Cordova CLI: Used to create and build Cordova projects.
  • Java Development Kit (JDK): Required for Android builds via Gradle.

// Install Cordova CLI
npm install -g cordova

// Create a new project
cordova create myApp com.example.myapp MyApp

// Add Android platform
cd myApp
cordova platform add android

// Build and run the app
cordova run android

Example: Displaying a Message Using JavaScript


// index.html content in www/ folder
<!DOCTYPE html>
<html>
<head><title>Cordova App</title></head>
<body>
    <h1>Cordova Android Example</h1>
    <button onclick="showMessage()">Click Me</button>
    <script>
        function showMessage() {
            alert("Hello from Cordova");
        }
    </script>
</body>
</html>