简体   繁体   English

如何在Cordova应用程序中添加自定义Cordova插件

[英]How to add custom cordova plugin in cordova app

I used the plugman command to create a plugin in cordova 我使用了plugman命令在cordova中创建了一个插件

It created all the necessary files that is required. 它创建了所有必需的文件。 Then I added the android platform in the plugin. 然后我在插件中添加了android平台。

Then I tried to add it in cordova app. 然后,我尝试将其添加到cordova应用程序中。 I added it successfully but when I am trying to build the app it is giving following errors 我已成功添加它,但是当我尝试构建应用程序时却出现以下错误

在此处输入图片说明

What am I doing wrong. 我究竟做错了什么。 How I can add my custom plugin in cordova app. 如何在Cordova应用程序中添加我的自定义插件。

Following is the code of .java file. 以下是.java文件的代码。

 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 : 这是我的plugin.xml文件:

        <?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>

I think from your reference you have missing in your java code. 我认为从您的参考中您缺少Java代码。 To create the custom Cordova plugin please look here . 要创建自定义的Cordova插件,请在此处查看

Please check your package name conventions in java file for more please check here . 请在Java文件中检查您的软件包名称约定,以了解更多信息,请在此处检查。

Here the steps to create plugin and install in ionic application. 这里是创建插件并在ionic应用程序中安装的步骤。

For the source and sample to check here . 有关来源和示例,请在此处检查。

Commands: 命令:

  1. plugman create --name Test --plugin_id cordova.plugin.test --plugin_version 0.0.1 插件创建-名称测试--plugin_id cordova.plugin.test --plugin_version 0.0.1
  2. plugman platform add --platform_name android Plugman Platform添加--platform_name android
  3. plugman platform add --platform_name ios 插件平台添加--platform_name ios
  4. ionic start IonicCordovaPluginTestApp blank 离子启动IonicCordovaPluginTestApp空白
  5. (In App Dir) ionic plugin add [Your local test plugin path] (在App Dir中)ionic插件添加[您的本地测试插件路径]

    example : ionic plugin add /Users/Workspace/Test/CordovaPluginSampleTest/Test 例如:ionic插件添加/ Users / Workspace / Test / CordovaPluginSampleTest / Test

  6. ionic platform add android or ios 离子平台添加android或ios

Update plugin.xml file in following way 通过以下方式更新plugin.xml文件

      <?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 !!! 希望这对您有帮助!

From the first line of error message, I think you have a syntax error in your java source code. 从错误消息的第一行开始,我认为您的Java源代码中存在语法错误。 You missed a semicolon in the first line of TestCordovaPlugin.java file. 您在TestCordovaPlugin.java文件的第一行中错过了分号。
As a simple guess, I think you miss the semicolon after the import statement. 一个简单的猜测,我想您会在import语句后错过分号。
The solution is, you can directly modify the java sourcein the Cordova project first, then sync the modification to the standalone plugin. 解决方案是,您可以先直接在Cordova项目中修改Java源代码,然后将修改同步到独立插件。

you can do one thing .you can create this plugin again with plugman but with different command ie 您可以做一件事。您可以使用plugman再次创建此插件,但使用不同的命令,即

plugman create --name AmILate --plugin_id cordovapluginamilate --plugin_version 0.0.1

and then follow same steps again. 然后再次执行相同的步骤。

I have used this correctly and it works but when i make another function named myMethod in cordovapluginamilate with javascript and java class i got an error as custom plugin error 我已经正确使用了它并且可以正常工作,但是当我在javascript和java类的cordovapluginamilate中创建另一个名为myMethod的函数时,出现了自定义插件错误

i am sharing my code for plugin on github https://github.com/vishuhanda001/cordovaaddcustomplugin 我在github https://github.com/vishuhanda001/cordovaaddcustomplugin上共享我的插件代码

i have commented coolMethod and instead have tried to use myMethod in javascript and java file but got above error while accessing it although if i uncomment coolMethod my plugin for coolmethod function only works.dont know whats happening help please ? 我已经评论了coolMethod,而是尝试在javascript和java文件中使用myMethod,但是在访问它时出现了以上错误,尽管如果我取消注释coolMethod,我的coolmethod函数插件只能正常工作。不知道发生了什么事,请帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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