Android Cordova Plugin
There are many plugins that have been created for Cordova most of which are cross platform. However, here I describe how to create your own Android Cordova Plugin in case you cannot find a suitable plugin to use with your project. This is a step by step guide to building Android Cordova plugins.
Building Android Cordova Plugin
Overview
Cordova plugins allow for embedding native code into a Cordova application, these are platform specific. This post focuses on building an android plugin that uses java to interface with a Cordova Application.
- Overview
- Create Cordova Project
- Enter Created Directory
- Add Android Platform
- Download and Install Plugman
- Create Plugin using Plugman
- Add Android Platform For Plugin
- Creating Package.json
- Adding Plugin to Project
- Build The Project
- Removing The Plugin
1) Create Cordova Project
First, we need a Cordova project to debug the plugin, if you already have a project setup you can go to step 4. Once Node.js and Cordova have been installed execute the following command in a command prompt to create a new Cordova Project.
cordova create %name% %package-name% %appname%
//Example
cordova create ExamplePlugin com.example.plugin ExamplePlugin
2) Enter created directory
Enter the directory created by cordova in this case "ExamplePlugin".
cd ExamplePlugin
3) Add Android Platform
Next we need to add the android platform to the project, this is done by executing the following command in the project directory.
cordova platform add android
4) Download and Install Plugman
Next we need to install plugman, this can be done by executing the command:
npm -g install plugman
5) Create Plugin using Plugman
We can now create a plugin using the command:
Plugman create -name %plugin name% -plugin_id %plugin id% -plugin_version %version%
//Example
plugman create -name MyPlugin -plugin_id com.myplugin.plugin -plugin_version 0.0.1
6) Add Android Platform For Plugin
We can now create a plugin using the command:
plugman platform add -platform_name android
7) Create package.json
We need to create a package.json file, the easiest way is to use the command :
plugman createpackagejson ./
Enter the details when prompted, this will create a package.json file for the plugin.
8) Adding Plugin to Project
Go back to the project directory and add the new plugin to the project using the command.
cordova plugin add %plugin name%
\\Example
cordova plugin add MyPlugin
Any changes to the plugin need to be updated first removing the plugin from the project and then repeat steps 8 and 9.
9) Build The Project
In the project directory execute the following command to compile the project with the new plugin.
cordova build android
10) Remove Plugin From Project
To remove a plugin from a cordova project go to the project directory and execute the command:
cordova plugin rm %plugin id%
\\Example
cordova plugin rm com.myplugin.plugin