Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova 3.5 embedded webview with plugins

I have an existing native Android app to which I'm adding a cordova webview. This is in the new gradle project format with Intellij. I've successfully gotten the cordova activity loaded, and opens as expected. I added the webview by following the cordova docs.

Now I'd like to use some of the plugins available but can't find a way to add them to the project. Since this isn't a cordova generated project, plugman doesn't work. I've tried copying the JS and Java files for the plugins I'm interested in to my source dir but that results in various errors.

e.g. require is undefined, when this happened, I added requirejs to the webview's index.html and it moved on to

Uncaught Error: Module name "cordova/exec" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded cordova.js:8
Failed to load resource file:///android_asset/www/scripts/cordova_plugins.js
deviceready has not fired after 5 seconds. cordova.js:1191
Channel not fired: onDOMContentLoaded 

How do I get a plugin working with this?

EDIT:

I am manually moving stuff around.

  1. This has to be put into an existing project which uses gradle for building.
  2. Using cordova create generates a project in the old ANT format.
  3. I followed the docs on embedding a WebView in an existing project (see the link above), but it doesn't mention any of the JavaScript files. So I just copied cordova.js from the android platform directory.
  4. Like wise, I download the plugins I wanted, copied their JavaScript files, added the feature to build.xml and copied their Java files to my src directory.

I didn't know about cordova_plugins.js or that cordova prepare generates them.

like image 862
zcourts Avatar asked Nov 23 '25 05:11

zcourts


1 Answers

You have to use Plugman to manage your plugins in the custom built Cordova WebView.

Please remove all your existing plugins and read them using plugman.

plugman install --platform android --project <proj> --plugin org.apache.cordova.battery-status --www <proj>/assets/www/scripts

where the <proj> is the folder where you main project is created, Alternatively you could use --www parameter which allows you to specify where root of web application is stored. Example:

plugman install --platform android --project <proj> --plugin org.apache.cordova.battery-status --www <proj>/assets/www/scripts

Plugman reference

EDIT

If you have highly customized project, you could create empty Cordova project where you add plugins, and copy cordova_plugins.js from this project to assets\www\scripts, Cordova should pickup these files. If you have your plugins JS files not in the assets\www\scripts\plugins\ folder, but instead of that in assets\www\plugins you have to modify content of cordova_plugins.js like that

module.exports = [
.....
{
    "file": "../plugins/org.apache.cordova.inappbrowser/www/InAppBrowser.js",
    "id": "org.apache.cordova.inappbrowser.InAppBrowser",
    "clobbers": [
        "window.open"
    ]
},
...
]
like image 68
codevision Avatar answered Nov 25 '25 19:11

codevision



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!