自定义Cordova插件会在应用程序构建时引发错误

时间:2017-01-09 18:46:46

标签: android cordova mobile phonegap-plugins cordova-plugins

在创建和添加自定义插件时无法为Android平台创建Cordova版本。我创建的文件夹结构是:

TestPlugin
-src
--android
---TestPlugin.java
-www
--TestPlugin.js
plugin.xml
project.json

并且每个文件的内容是:

TestPlugin.java

package com.test.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class TestPlugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (action.equals("greet")) {
            String name = data.getString(0);
            String message = "Hello, " + name;
            callbackContext.success(message);

            return true;
        }
        else {
            return false;
        }
    }
}

TestPlugin.js

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

的plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
    xmlns:android="http://schemas.android.com/apk/res/android"
    id="com.test.plugin"
    version="1.0.0">
    <name>TestPlugin</name>
    <description>This is a test</description>
    <license>MIT</license>

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

    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="TestPlugin">
                <param name="android-package" value="com.test.plugin" />
            </feature>
        </config-file>

        <source-file src="src/android/TestPlugin.java" target-dir="src/com/test/plugin/TestPlugin.java" />
    </platform>
</plugin>

的package.json

{
  "name": "TestPlugin",
  "version": "1.0.0",
  "description": "TestPlugin",
  "cordova": {
    "platforms": [
      "android"
    ]
  },
  "keywords": [
    "cordova",
    "test"
  ],
  "engines": [
    {
      "name": "cordova-android",
      "version": ">=4.0.0"
    }
  ],
  "author": "Me",
  "license": "Apache 2.0"
}

当我在命令提示符下运行“cordova build android”时,它会立即返回以下错误:

C:\inetpub\wwwroot\test\mobile>cordova build android
shell.js: internal error
Error: EISDIR: illegal operation on a directory, read
    at Error (native)
    at Object.fs.readSync (fs.js:603:19)
    at Object.fs.readFileSync (fs.js:438:24)
    at C:\inetpub\wwwroot\test\mobile\platforms\android\cordova\node_modules\shelljs\src\grep.js:41:23
    at Array.forEach (native)
    at Object._grep (C:\inetpub\wwwroot\test\mobile\platforms\android\cordova\node_modules\shelljs\src\grep.js:35:9)
    at Object.grep (C:\inetpub\wwwroot\test\mobile\platforms\android\cordova\node_modules\shelljs\src\common.js:186:23)
    at C:\inetpub\wwwroot\test\mobile\platforms\android\cordova\lib\prepare.js:196:22
    at Array.filter (native)
    at updateProjectAccordingTo (C:\inetpub\wwwroot\test\mobile\platforms\android\cordova\lib\prepare.js:195:44)

这是完整输出。我尝试删除Android平台并重新添加,但同样的错误发生。当我删除自定义插件时,构建成功。我也尝试使用Plugman来创建插件但没有成功。目标API级别为24。

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

昨晚在玩弄它之后找到了这个问题的答案。在Plugin.xml中,源文件标记不应该在target-dir属性中具有文件名,因此该行应该来自:

<source-file src="src/android/TestPlugin.java" target-dir="src/com/test/plugin/TestPlugin.java" />

为:

<source-file src="src/android/TestPlugin.java" target-dir="src/com/test/plugin/" />

删除并添加插件,然后在此之后正确创建构建。