1

I'm developing Cordova project using Android Studio. I want to Cordova device plugin to my project. As I understand Cordova plugin consists of device.js and Device.java, which I've found in the folder of plugin. 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. I also included into index.html cordova_plugins.js file, which was automatically created by Cordova. 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. What am I doing wrong?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

2 Answers2

1

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).

The recommended way to add plugins directly into a platform is to use the plugman tool.

If you really want to do everything manually, then the file you need to watch is plugin.xml. In this file you will find files to modify like config.xml, AndroidManifest.xml or 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.

QuickFix
  • 11,661
  • 2
  • 38
  • 50
  • Thanks, have you added plugins manually to the project? I'll check `plugin.xml` later today to see what it can show me. – Max Koretskyi Nov 24 '14 at 13:55
  • No I prefer to use the CLI to add plugins. – QuickFix Nov 24 '14 at 15:57
  • And then I assume that you build app also using `cordova build android` without using Android Studio? – Max Koretskyi Nov 24 '14 at 16:04
  • 1
    Yep. Or sometimes when I have to debug java I also use Eclipse (just take care that cli overwrites any changes made in the platform). But again if you use the plugman tool, it will be the same as doing manually : installed directly in the platform, nothing overwritten, and just doing a refresh in android studio should bring the update. – QuickFix Nov 24 '14 at 16:36
  • Thanks, so as I understand when I run `cordova prepare android` it creates project in `platforms/android` folder, which I open and modify in Android Studio. And `plugman` actually modifies files inside that `platform/android` folder, correct? So the changes will be instantly available in Android Studio and I'll be able to see the correct way the plugin is added to the project? – Max Koretskyi Nov 24 '14 at 16:44
  • 1
    Yep (have to type at least 15 characters to reply...) – QuickFix Nov 24 '14 at 17:11
  • Great, let me try it and I'll get back to you. – Max Koretskyi Nov 24 '14 at 17:24
  • After spending several days I've finally figured out that the only thing I was missing was adding `` declaration to `config.xml`. I've also found [this great answer](http://stackoverflow.com/a/19125861/2545680), but sadly after I've spent so much time on this. Plugman did help a bit, however even after I run `cordova prepare` it still doesn't modify `config.xml`, as I understand it does so only when building project. – Max Koretskyi Nov 30 '14 at 14:08
  • actually prepare does update config.xml. Maybe you were not looking at the good config.xml. The file that's updated is in platform/android/res/xml/config.xml, not the root one. By the way, in my answer I told you that the file to look at was plugin.xml, and the update of config.xml is done following what's written in plugin.xml (` – QuickFix Dec 01 '14 at 13:46
  • ah, I didn't notice that line :(. Yeah, things would've been easier if I had. Thanks again. – Max Koretskyi Dec 01 '14 at 15:02
0

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.

Anyhow, you should call: cordova plugin add org.apache.cordova.device from command prompt (where your top level www folder is).

See cordova documentation here for more details (scroll to bottom).

Please note that, I've never used Android Studio, only Eclipse, but these should be IDE agnostic.

mentat
  • 2,748
  • 1
  • 21
  • 40
  • 1
    thanks, I'm not using cordova project, I'm only using their library (CordovaLib java classes and cordova.js), and I need to manually add plugin to my project – Max Koretskyi Nov 24 '14 at 13:53