Search code examples
cordovaionic2phonegap-pluginscordova-pluginsphonegap

Creating a Cordova plugin for Ionic based on Android SDK


I need to create an Ionic app that uses a native Android SDK in order to perform the functionality I want. The SDK is an AAR and allows me to develop an Android app that handles things like web services, bluetooth, etc. The problem is that I need to develop the app cross platform using Ionic and that means ultimately creating a Cordova plugin. I've looked around the net for information on Cordova plugins but I'm not quite sure how to develop a plugin larger than an echo because thats all anyone ever seems to show.

So my question is, where do I begin? Do I develop the Android app first and then copy the java code into my Cordova plugin or do I develop the Cordova plugin at the same time as creating the java code based on the Android SDK I've been provided with?

If I build the Cordova plugin without the Android application first I won't be able to test, so maybe I should build the Android app first and then port it into a Cordova plugin?

Thanks


Solution

  • where do I begin?

    Use an existing (real) Cordova plugin as reference.

    Depending how your SDK is packaged, depends how you'll need to install it from your plugin.

    If your SDK is a JAR file, you'll need to place it in your plugin folder and add an entry to your plugin.xml to deploy it. For example cordova-plugin-cipherlab-rs30 does this.

    Or if your SDK is available via Maven, you can use <framework> tags to satisfy the dependency via Gradle: e.g. cordova-plugin-facebook.

    Do I develop the Android app first and then copy the java code into my Cordova plugin

    I wouldn't recommend this if you're ultimately developing a Cordova plugin, otherwise you will create more work than necessary.

    If I build the Cordova plugin without the Android application first I won't be able to test

    This is not true:

    • create your plugin locally e.g. /path/to/my/plugin
    • then create a Cordova test app project which you'll use to test it, e.g. cordova create myplugintest
    • add your plugin to your test project: cd myplugintest && cordova plugin add /path/to/my/plugin
    • add the Android platform: cordova platform add android
    • Open the native Android project in Android Studio: /path/to/myplugintest/platforms/android
    • Set breakpoints and debug your plugin by running your test app in Android studio.
    • Any code changes you make to your plugin Java in Android Studio will need to be copied back to your plugin source in /path/to/my/plugin/