且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费

更新时间:2022-06-10 08:02:54

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消费

Detail steps to create plugin

(1) Install plugman via npm:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费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消费Copy the following source code to this Adder.java:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费(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.

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费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消费(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消费

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:

如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费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 )


————————————————

版权声明:本文为CSDN博主「汪子熙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/i042416/article/details/108298450如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费