0
votes

I'm developing a Cordova plugin for an Android application, I read all documentation on Oracle website but I don't understand how i can create custom plugin and use it.

1) I have created a Cordova project which I insert a custom library (jar), this library allow to me to use some customized functions.

My cordova project, that contains a custom library 2) Now i have to create a plugin in cordova that "call" a function inside my library, to do this i have create a new folder in plugins "cordova-plugin-ldm" inside two new folder "src/Android/" and "www".

In src/android i created my java file:

public class MYCLASS extends CordovaPlugin {
  protected void pluginInitialize() {
  }

  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) 
      throws JSONException {
    if (action.equals("alert")) {


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        TestConnection ts  = new TestConnection();
        JSONObject result = ts.TestNow();

        callbackContext.success(myString);


        //callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));

      return true;
    }
    return false;
  }
}

In www i have created my js :

module.exports = {
    greet: function (name, successCallback, errorCallback) {
        cordova.exec(successCallback, errorCallback, "Hello", "greet", [name]);
    }
};

And in a file "plugin.xml":

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        id="com.acme.plugin.alert"
        version="0.0.1">

  <name>LDM Plugin</name>
  <description>A Cordova plugin for LDM</description>

  <engines>
    <engine name="cordova" version=">=3.6.0" />
  </engines>

  <js-module src="www/MYJAVASCRIPT.js" name="MYJAVASCRIPT">
    <clobbers target="MYJAVASCRIPT" />
  </js-module>

  <!-- Android -->
  <platform name="android">
    <config-file target="res/xml/config.xml" parent="/*">
      <feature name="MYNAMEAPP">
        <param name="android-package" value="package.ldm" />
      </feature>
    </config-file>
    <source-file src="src/android/MYCLASS.java" target-dir="src/PACKAGE/ldm/plugin/ldm" />
  </platform>

Now (i don't know if is it correct, and if it works) but, how i can add (automatically or manually) to my project ?

3

3 Answers

0
votes

As far as i know you gotta create a plugin folder as per the specifications and ensure that the plugin is referred in the fetch.json file under plugins folder. Then removing and re-adding the platform should take care of plugin installation in respective folder. You can refer any of the existing plugins to replicate the same folder structure.

The detailed info on custom plugin creation is available in cordova official documentation.Hope it helps

1
votes

Here you can find a very simple plugin with parameters and callbacks https://github.com/amitbindal09/cordova-plugin-alert

0
votes

If the plugin is in your plugins folder, when you add the platform, the plugin is auto included.