Simple Steps For Creating Custom Cordova Plugin
by Sanjeev Gupta in Apps/DevOps, General
Building Cordova plugin
Developers use plugins to implement external libraries like Google maps, Push notifications plugins etc. Generally for every framework there are standard plugins are to be used. But what will you do if there is no plugin for your business requirement. In that case developers need to create their own custom plugin. At first it does seem like a daunting task but its not that difficult.
Before diving into the development of cordova plugin let us first understand what a cordova plugin is and what is its significance. Cordova is an open source mobile application development framework that allows the developers to make cross platform applications. A Cordova plugin will comprise all the required native libraries and a javascript file. The javascript file can be said to be an interface that calls the native libraries and thus hiding the native implementation.
I have created a tutorial on how to build a custom cordova plugin and this tutorial will navigate you through all the steps.
To build a plugin you need to install Cordova, Plugman and Android Studio for Android and Xcode for IOS.
Install Plugman
Plugman is a tools which helps us to setup Native Android and IOS platforms.
sudo npm install -g plugman
Building a Plugin
plugman create –name <plugin_name> –plugin_id <cordova-plugin-id> –plugin_version <plugin_version>
Setup Platforms
plugman platform add –platform_name <android or ios>
Building a package.json file for plugin
plugman createpackagejson <plugin path>
After building the plugin, the structure of the plugin will look like this. Once the directory structure created, then we add our plugin into the cordova project. This plugin can be added via a git repository or local path.
Structure of the Plugin
|– src
| |– android
| | |– Demo.java
| |
| |– ios
| | |– Demo.m
|
|– www
| |– Demo.js
|
|– plugin.xml
|– package.json
To add this plugin in your project you need to type the following command in the Command Line Interface.
ionic cordova plugin add <plugin path>
Now,Run this command to build your project according to you native technology like Android or IOS.
cordova build android
Now to goto the project directory –>Platforms —> Select Android for Android Studio and IOS for Xcode.You can also import this folder directly in your IDE.