简体   繁体   中英

How to create and add cordova plugin to project

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 ?

在这里您可以找到一个非常简单的带有参数和回调的插件https://github.com/amitbindal09/cordova-plugin-alert

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

如果插件位于您的plugins文件夹中,则在添加平台时,该插件将自动包含在内。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM