Search code examples
cordovacordova-pluginsvisual-studio-cordova

How to add custom cordova plugin in cordova app


I used the plugman command to create a plugin in cordova

It created all the necessary files that is required. Then I added the android platform in the plugin.

Then I tried to add it in cordova app. I added it successfully but when I am trying to build the app it is giving following errors

enter image description here

What am I doing wrong. How I can add my custom plugin in cordova app.

Following is the code of .java file.

 package cordova-plugin-test-cordova-plugin;

 import org.apache.cordova.CordovaPlugin;
 import org.apache.cordova.CallbackContext;

  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
      /**
      * This class echoes a string called from JavaScript.
     */
   public class TestCordovaPlugin extends CordovaPlugin {

   @Override
   public boolean execute(String action, JSONArray args, CallbackContext     callbackContext) throws JSONException {
    if (action.equals("coolMethod")) {
        String message = args.getString(0);
        this.coolMethod(message, callbackContext);
        return true;
    }
    return false;
}

private void coolMethod(String message, CallbackContext callbackContext) {
    if (message != null && message.length() > 0) {
        callbackContext.success(message);
    } else {
        callbackContext.error("Expected one non-empty string argument.");
    }
}
}

Here is my plugin.xml file :

        <?xml version='1.0' encoding='utf-8'?>
        <plugin id="cordova-plugin-test-cordova-plugin" version="0.0.1"    xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
         <name>TestCordovaPlugin</name><js-module name="TestCordovaPlugin" src="www/TestCordovaPlugin.js">
<clobbers target="cordova.plugins.TestCordovaPlugin" /></js-module><platform name="android">
  <config-file parent="/*" target="res/xml/config.xml"><feature name="TestCordovaPlugin">
    <param name="android-package" value="cordova-plugin-test-cordova-plugin.TestCordovaPlugin" />
  </feature></config-file><config-file parent="/*" target="AndroidManifest.xml"></config-file>
  <source-file src="src/android/TestCordovaPlugin.java" target-dir="src/cordova-plugin-test-cordova-plugin/TestCordovaPlugin" />
</platform>
 </plugin>

Solution

  • I think from your reference you have missing in your java code. To create the custom Cordova plugin please look here.

    Please check your package name conventions in java file for more please check here.

    Here the steps to create plugin and install in ionic application.

    For the source and sample to check here.

    Commands:

    1. plugman create --name Test --plugin_id cordova.plugin.test --plugin_version 0.0.1
    2. plugman platform add --platform_name android
    3. plugman platform add --platform_name ios
    4. ionic start IonicCordovaPluginTestApp blank
    5. (In App Dir) ionic plugin add [Your local test plugin path]

      example : ionic plugin add /Users/Workspace/Test/CordovaPluginSampleTest/Test

    6. ionic platform add android or ios

    Update plugin.xml file in following way

          <?xml version='1.0' encoding='utf-8'?>
          <plugin id="cordova.plugin.test.cordova.plugin" version="0.0.1"    xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
             <name>TestCordovaPlugin</name>
             <js-module name="TestCordovaPlugin" src="www/TestCordovaPlugin.js">
                <clobbers target="cordova.plugins.TestCordovaPlugin" />
             </js-module>
             <platform name="android">
                <config-file parent="/*" target="res/xml/config.xml">
                   <feature name="TestCordovaPlugin">
                      <param name="android-package" value="cordova.plugin.test.cordova.plugin.TestCordovaPlugin" />
                   </feature>
                </config-file>
                <config-file parent="/*" target="AndroidManifest.xml"></config-file>
                <source-file src="src/android/TestCordovaPlugin.java" target-dir="src/cordova/plugin/test/cordova/plugin/TestCordovaPlugin" />
             </platform>
          </plugin>
    

    Hopes this will help you !!!