Search code examples
javaangularjscordovapluginscordova-plugins

Making a plugin for Ionic from Java


I'm working with a SDK for developing in Android. I have made some tests and everything seems to be OK in Android (Java). After testing, I want to work with Ionic, and make this Java-SDK usable.

For that, I think I will need to convert or to make a kind of "bridge" betwen Java and Ionic with these posibilities:

  • Java --> Cordova
  • Java --> Angular

How to do this? I have read about GWT but I think that it is not a good solution because the original SDK is not mine and I can't re-write it.

Thanks.


Solution

  • The easier way:

    Ionic is a framework that lies on top of Angular, which itself lies on top of Cordova's webview or Crosswalk's webview (if you are using that one).

    The easiest way for you to bridge the Java code and the JavaScript one is to create a Cordova plugin. Basically you all you have to do (from the Java side) is to extend the CordovaPlugin class and override its exec method.

    You should read the documentation here, because you would need a bit more than just Java code: https://cordova.apache.org/docs/en/latest/guide/platforms/android/plugin.html

    The harder way:

    There exists a second option by using WebView.addJavascriptInterface, but there you would have handle more things than the first approach.

    https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)