Suppose we have packaged our UI5 application into mobile platform and need to consume native API provided by mobile platform, and it is time for Cordova plugin to come on the stage. A Cordova plugin is a package of injected code that allows the Cordova webview within which the app renders to communicate with the native platform on which it runs.

In this blog, I still use the UI5 application mentioned in my previous blog Step by step to package a Fiori application into your Android device using Cordova for demo.

I will show the steps how to create a dummy plugin for Android platform, which simply performs the calculation between two integers implemented in Java, and then could be consumed by JavaScript code in UI5.
There is indeed a page in Cordova website talking about how to create a new plugin, however I fail to create a plugin just simply by following it.

In my opinion some information is missing in the document, so it is the reason why I decide to document my detailed step here for future reference.

Note

The steps might vary with different version of Cordova. This blog is made based on version 7.0.1:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第1张图片

Detail steps to create plugin

(1) Install plugman via npm:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第2张图片

Then create a new plugin via the following command:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第3张图片

Once done, in the root folder of project there is a new folder generated, whose name is equal to the option specified via command -name:

Some artifacts are automatically generated and stored within the plugin folder. We don’t need to touch them at the moment.

(2) Since I need to develop a plugin used for Android platform, so enable the created plugin with Android platform via command: “plugman platform add –platform_name android”

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第4张图片

Once done, a .java source file is created, which is used to implement the logic for example performing some native API call.

Copy the following source code to this Adder.java:

package jerry.adder;import org.apache.cordova.CordovaPlugin;import org.apache.cordova.CallbackContext;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class Adder extends CordovaPlugin {    @Override    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {        if (action.equals("performAdd")) {            int arg1 = args.getInt(0);            int arg2 = args.getInt(1);            /* Indicating success is failure is done by calling the appropriate method on the             callbackContext.*/            int result = arg1 + arg2;            callbackContext.success("result calculated in Java: " + result);            return true;        }        return false;    }}

(3) Perform the following command to automatically generate a descriptor file, package.json for created plugin. Just press enter key again and again to simply use the default value, which are enough for this exercise.

Once done, the package.json file is generated within plugin folder.

(4) Install this plugin via command “cordova plugin add Adder”. If everything works fine, you should see message “BUILD SUCCESSFUL”.

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第5张图片

Now just perform cordova compile, and the plugin will be built into the final APK file.

A list of artifacts generated for the created plugin

Let’s review what artifacts / configuration finally we have regarding this created plugin:

(1) in config.xml in the root folder, our plugin is added:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第6张图片

(2) The implementation Adder.java file for plugin in plugin folder.

(3) In the plugin folder there is a plugin.xml file, which defines the path the plugin implementation will be located in the platform specific folder.

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第7张图片

In my project the target-dir points to this folder as below:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第8张图片

Consume the plugin in UI5 application

In order to test this plugin, paste the following source code to index.js in path: /platforms/android/assets/www/js:

var app = {    initialize: function() {        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);    },    // deviceready Event Handler    //    // Bind any cordova events here. Common events are:    // 'pause', 'resume', etc.    onDeviceReady: function() {        this.receivedEvent('deviceready');    },    receivedEvent: function(id) {        function success(result){            debugger;            alert("Jerry plugin result: " + result);        };        setTimeout( function(){            debugger;            Cordova.exec(success, null, "Adder", "performAdd", [10,20]);        }, 10000);     }};app.initialize();

The consumption is done in line 38, here I use a 10 seconds delay just in order to ease my debugging. ( Java Plugin debugging is worth another blog )

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第9张图片

Execute the application in my mobile phone, the plugin will be executed in Java layer and result is passed back to my UI5 application:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费_第10张图片

If you would like to know how JavaScript code is passed to Java code, please read this blog: How is JavaScript code in OData offline plugin delegated to native Java code in Android.

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

更多相关文章

  1. android 层叠图片形成一张图片
  2. Android中的ListView实现图片文字和按钮
  3. android 在SDCard获取图片
  4. android 从系统相册获取一张图片
  5. android WebView加载URL不显示图片
  6. android 使用randerScript实现图片模糊效果
  7. android 给图片加水印
  8. Android: 利用Bimap,canvas处理图片(画直线)

随机推荐

  1. Android中的文件读写操作
  2. 总结了近百个Android优秀开源项目,覆盖And
  3. Android客户端SQLite数据库升级方案
  4. 关于android 使用bitmap的OOM心得和解决
  5. Android(安卓)应用程序签名
  6. 玩转pandaboard之linaro对于Android的编
  7. Android 仿今日头条评论输入框
  8. android中的跨进程通信的实现(一)——远程
  9. 【源码】android新闻日报源码、android
  10. 【Android Studio快捷键】之导入相应包声