I'm new to Cordova and trying to create a fairly basic Android plugin.
When I call one of the JavaScript functions, e.g.
window.MyPlugin.getAdvertisingId().then(console.log).catch(console.error);
Nothing happens. Neither the success nor error callbacks are called. I added some logging to MyPlugin.execute and monitored it in logcat. It wasn't logged, which means it's never called.
The callbacks are piling up in the cordova.callbacks object (in JavaScript), suggesting they're pending some response that never arrives.
In logcat, I see this message immediately when I make one of these calls from JavaScript:
CordovaBridge: Ignoring exec() from previous page load.
So it's telling me that it's ignoring these cordova.exec calls, but not why.
This is using the latest Cordova, running on an Amazon Fire TV Stick 4K Max.
plugin.xml:
<?xml version="1.0" encoding="UTF-8"?>
<plugin
xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-mycompany"
version="0.0.0"
>
<name>MyCompany Plugin</name>
<description>MyCompany Android plugin</description>
<license>Apache 2.0</license>
<keywords>cordova,mycompany</keywords>
<js-module src="www/myPlugin.js" name="myPlugin">
<clobbers target="MyPlugin" />
</js-module>
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="myPlugin">
<param name="android-package" value="com.mycompany.cordova.plugin.MyPlugin" />
</feature>
</config-file>
<source-file src="src/android/MyPlugin.java" target-dir="src/com/mycompany/cordova/plugin" />
</platform>
</plugin>
src/android/MyPlugin.java:
package com.mycompany.cordova.plugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
public class MyPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
// Implementation omitted, because this is never called.
}
}
www/myPlugin.js
exec = require('cordova/exec');
function getAdvertisingId() {
return new Promise((resolve, reject) => {
exec(
resolve,
reject,
'myPlugin',
'getAdvertisingId',
[]
);
});
}
module.exports = {
getAdvertisingId,
};