I built a simple cordova plugin just to see it work before I start trying to fill out the native code, and I can’t seem to get a handle on it in the ionic app. My ionic is 2.0.0, my cordova is 6.3.0. Here’s all the source for the plugin:
myplugin
- src
- android
myplugin.java
- ios
myplugin.h
- windows
myplugin.js
- www
myplugin.js
plugin.xml
There is nothing complex in any of the native files, this is the java file:
public class TravelerSecureServices extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("coolMethod")) {
String message = args.getString(0);
this.coolMethod(message, callbackContext);
return true;
}
return false;
}
private void coolMethod(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
callbackContext.success("Successfully Called");
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
}
Simple, just returning a string. The plugin js file looks like this:
var exec = require('cordova/exec');
var myplugin = {
coolMethod: function(){
exec(success, error, "myplugin", "coolMethod", [arg0]);
}
};
module.exports = myplugin;
The plugin.xml looks like this:
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-myplugin"
version="0.0.1"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>my plugin</name>
<js-module name="myplugin" src="www/myplugin.js">
<clobbers target="myplugin" />
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="myplugin">
<param name="android-package" value="myplugin" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml" />
<source-file src="src/android/myplugin.java" target-dir="src/cordova-plugin-myplugin" />
</platform>
<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="myplugin">
<param name="ios-package" value="myplugin" />
</feature>
</config-file>
<source-file src="src/ios/myplugin.m" />
</platform>
<platform name="windows">
<js-module src="src/windows/myplugin.js" name="myplugin">
<runs target="" />
</js-module>
</platform>
</plugin>
Note that I have a clobber set. I’ve imported the plugin into my ionic app using ionic plugin add /path/to/myplugin
, and the code all shows up in the plugins directory. Everything looks good. When I build and run the app on an android device, I get undefined
for all of the following:
myplugin
window.myplugin
window.myplugin.coolMethod()
window.cordova.plugins.myplugin
I’ve tried every place I could think of or find an example of online, and the plugin isn’t there. I’ve tried it with and without <script src="myplugin.js"></script>
in the index.html file. I’ve removed and re-added the plugin, removed and re-added the platform, I’ve used ionic build
, I’ve used ionic run
, and every possible combination of CLI commands and configurations I can think of. What am I missing? I can add more details if needed, and I’ll try any suggestions.
I’m certain it’s something stupid and simple, but so far I haven’t been able to find a complete tutorial or example that actually covers all of this, so I’m sure there’s a step in there that I’m missing. I’ve beat my head on this until I’m out of ideas.