Cordova 4.0.0: exec() call to unknown plugin

3k Views Asked by At

I generated the code using the Cordova 4.0.0 CLI.

Now I try to write a Plugin for Android (MyPlugin) to execute native code from the webview. But everytime i try to call the plugin's method I receive the errormessage:

exec() call to unknown plugin

I tried different mappings in the config.xml (and also created a plugin.xml). But nothing works for me. Also the API is not very helpful.

Has anybody an idea how to realize this or what is wrong with my implementation?

Here is my code:

com.cordovaDemo.MyPlugin.java

public class MyPlugin extends CordovaPlugin {
@Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);

    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        //MyCode
        callbackContext.success(action);
        return true;
    }

index.html

<head>
...
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>

<title>Hello World</title>


<script>
    window.func = function(str,callback){
        cordova.exec(callback, function(err){alert(err)},"MyPlugin","echo", [str]);
    }
    function callPlugin(str){
        window.func(str,function(){
            alert("Done!");
        });
    }

</script>

</head>
<body>
    <div class="app">

        <div id="deviceready" class="blink">
            <a onclick="callPlugin('Test')">Click me</a>
        </div>
    </div>

</body>

config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.touristmobile.cordovaDemo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>CordovaDemo</name>
    ...
    <content src="index.html" />
    <access origin="*" />

    <platform name="android">
    <config-file target="config.xml" parent="/*">
        <feature name="MyPlugin">
            <param name="android-package" value="com.cordovaDemo.MyPlugin"/>
        </feature>
    </config-file>
</platform>

Edit:

Ok I think that now I understand the main idea of cordova plugins. A cordova plugin is independent from the "generated" Android Wrapper app.

In my case a cordova Webview should be an extension of my existing Android App, and the Webview should call functions and displayed information from the main app. In short: Is it possible to call functions located in classes on the same level as my CordovaActivity

2

There are 2 best solutions below

0
On BEST ANSWER

Ok! I solved it using Cordova 3.6.4

An embedded Webview is possible: I followed this Guide: http://cordova.apache.org/docs/en/4.0.0/guide_platforms_android_webview.md.html#Android%20WebViews

3
On

In your plugin you should have a , a js file using require that exports a module (which has to match the js-module defined in plugin.xml), java source file, additional resources if needed and most important to let plugman do the magic : plugin.xml file that describes the structure of the plugin in which you define the files to copy (source-file) and the javascript module (js-module).

Then you use cordova plugin add specifying the path to your plugin source to add the plugin to your project.

What I would recommend you is have a look at a simple cordova plugin like vibration to study the structure and make your own by changing file names, module names..., remove everything about platforms you don't want to support (at least that's what worked for me) https://git-wip-us.apache.org/repos/asf?p=cordova-plugin-vibration.git;a=tree

Docs for reference :