简体   繁体   English

手动将Cordova插件添加到Cordova项目的正确方法是什么

[英]what is the correct way to manually add Cordova plugin to Cordova project

I'm developing Cordova project using Android Studio. 我正在使用Android Studio开发Cordova项目。 I want to Cordova device plugin to my project. 我想将Cordova设备插件添加到我的项目中。 As I understand Cordova plugin consists of device.js and Device.java , which I've found in the folder of plugin. 据我了解,Cordova插件由device.jsDevice.java ,它们已在插件文件夹中找到。 I copied Device.java into CordovaLib/src/org/apache/cordova/device/ folder and device.js into assets/www/plugins/org.apache.cordova.device/www folder. 我将Device.java复制到CordovaLib/src/org/apache/cordova/device/文件夹中,并将device.jsassets/www/plugins/org.apache.cordova.device/www文件夹中。 I also included into index.html cordova_plugins.js file, which was automatically created by Cordova. 我还将它包含在由Cordova自动创建的index.html cordova_plugins.js文件中。 It has the following content: 它具有以下内容:

cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/org.apache.cordova.device/www/device.js",
        "id": "org.apache.cordova.device.device",
        "clobbers": [
            "device"
        ]
    }
];
module.exports.metadata = 
// TOP OF METADATA
{
    "org.apache.cordova.device": "0.2.12"
}
// BOTTOM OF METADATA
});

Now when I successfully build my app and run it I get Error initializing Cordova: Class not found error. 现在,当我成功构建我的应用程序并运行它时,我收到Error initializing Cordova: Class not found错误。 What am I doing wrong? 我究竟做错了什么?

If you make your changes directly in the platform/android folder, do not use the CLI to add plugins, you may loose your code (when you use the cli to add a plugin, it is only really added in the platform when you run cordova prepare android which will overwrite the code in platforms/android/assets/www with the code in the root www folder which may not be what you want). 如果直接在platform / android文件夹中进行更改,请勿使用CLI添加插件,则可能会丢失代码(使用cli添加插件时,只有在运行cordova prepare android时,它才会真正添加到平台中cordova prepare android ,它将用根www文件夹中的代码覆盖platform / android / assets / www中的代码(可能不是您想要的)。

The recommended way to add plugins directly into a platform is to use the plugman tool . 将插件直接添加到平台的推荐方法是使用Plugman工具

If you really want to do everything manually, then the file you need to watch is plugin.xml. 如果您真的想手动执行所有操作,那么您需要观看的文件是plugin.xml。 In this file you will find files to modify like config.xml, AndroidManifest.xml or cordova_plugins.js. 在此文件中,您将找到要修改的文件,例如config.xml,AndroidManifest.xml或cordova_plugins.js。

You should not load cordova_plugins.js from index.xml, it is automatically loaded from cordova.js (as well as javascrips from plugins) using require. 您不应该从index.xml加载cordova_plugins.js,而是使用require从cordova.js(以及插件的Javascrips)自动加载。

You should use cordova cli (command line interface) methods in order to add plugins, assuming you are using cordova/phonegap version > 3. As far as I remember, core plugins (maintained by cordova team) are also needed to be added/removed since version 3.2 or 3.4. 假设您使用的是cordova / phonegap版本> 3,则应使用cordova cli(命令行界面)方法来添加插件。据我所记得,还需要添加/删除核心插件(由cordova团队维护)。从3.2或3.4版本开始。

Anyhow, you should call: cordova plugin add org.apache.cordova.device from command prompt (where your top level www folder is). 无论如何,您应该调用: cordova plugin add org.apache.cordova.device从命令提示符(顶级www文件夹所在的位置)中cordova plugin add org.apache.cordova.device

See cordova documentation here for more details (scroll to bottom). 有关更多详细信息,请参见此处的 cordova文档(滚动至底部)。

Please note that, I've never used Android Studio, only Eclipse, but these should be IDE agnostic. 请注意,我从未使用过Android Studio,仅使用过Eclipse,但是它们应该与IDE无关。

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

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