

Create|Migrate|Publish native script plugins and consume in an MDK App
source link: https://blogs.sap.com/2022/05/17/createmigratepublish-native-script-plugins-and-consume-in-an-mdk-app/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction
In one of the recent customer projects, as we were assessing the potential changes for migrating to MDK(Mobile Development Kit) 6.x version, it came to our notice that the project used few native script plugins from npm registry some of which weren’t maintained proactively making it incompatible with the latest native script 8 version.
In this blog post, I would like to share my experience with
- Creating a hello world Native Script plugin
- Migrating an existing Native Script plugin
- Publishing Native Script plugin to npm registry
- Using a Native Script plugin in MDK application
Creating a simple Native Script plugin
Native script provides detailed documentation on how to create a native script plugin to create a native script plugin which can be published to npm repository and install it by
npm i <plugin-name> (or in case of MDK, referencing it in MDKProject.json)
https://github.com/NativeScript/plugin-seed provides a powerful template. It comes with the entire project structure configured making it very easy to create, test and migrate plugins. So let’s begin.
Let’s create a hello world plugin which logs hello world to the console.
- Click on “Use this template” button in https://github.com/NativeScript/plugin-seed
- Follow GitHub instructions (set repo name, visibility, description) & clone your new repo
- Now that we have the template in our local, execute the command
npm run setup
to set up the workspace
- Configure your npm scope by executing the command,
npm run config
- Since we are planning to publish it as a user scope package, name the scope with your npm repository user name
- Provide the rest of the details and we will get our workspace configured with the new scope.
- Now we need to add a new package, i.e the folder within which our actual plugin will reside
- Execute the command,
npm run add
- Let us name the package hello-world-toast.We can see that a new folder named hello-world-toast is created within the packages folder.
- Now let’s add a generic function in common.ts which can be used by Android or iOS platform, wherever we have the plugin used
export class HelloWorldToastCommon extends Observable { static sayHello(){ console.log("hello world!") } }
Note: You may also refer https://v7.docs.nativescript.org/plugins/plugin-reference to have a look at the basic structure of a plugin.
- Now that we have the functionality set for our plugin, let’s test it. The template has already taken care of the basic framework for tests. Within the tools/demo/hello-world-toast folder we have a file named index.ts where our plugin is already imported.
- Replace the contents of the testit() function within index.ts file as below
import { DemoSharedBase } from '../utils'; import {HelloWorldToast } from '@adilek/hello-world-toast'; export class DemoSharedHelloWorldToast extends DemoSharedBase { testIt() { HelloWorldToast.sayHello(); } }
- Now, we can test the implementation. On the plugin root folder runnpm start
-
Select apps.demo.android (since we are testing on an android emulator)
- On click of “test hello world toast” the console logs hello world
- Now that we have the plugin, we can deploy it
- Navigate to the hello-world-toast folder in packages
- Execute npm publish –access public
- This will ask you to login with your npm user id
- Once logged in, the package will get published
Migrating an existing nativescript plugin
Some of the steps below might be specific to this plugin, but it will give an overall idea about the process and hurdles on the way.
We used nativescript-background-fetch plugin (https://github.com/transistorsoft/nativescript-background-fetch/tree/master/src) for some of the tasks within our custom MDK project. But unfortunately since this plugin wasn’t migrated to latest native script /java versions, we decided to give it a try manually.
We used the native script-seed template as in the above hello-world example to bootstrap the process. Once the initial set up was done and the scope configured with the npm user name, we followed the below steps.
- Create a new package fetch-task
- Copy the below files from within the src folder of the native script-background-fetch git repo to the src folder of fetch-task package.

- Copy the below files from within the src folder of the native script-background-fetch git repo to the src folder of fetch-task package.
- Execute the command,
ns migrate (this command helps us migrate the library dependencies)
- Open the aar file in the previous version of the plugin in android studio and change the java versions and targetsdk versions, build and create a new aar file
- Add the aar file in platforms/android/libs folder in the fetch-task package

- Change the two entries in package.json as below

- Make changes as in the original native script-background-fetch plugin demo folder in the apps/demo/src folder
- Execute npm run start and run the package in android to check if the plugin is working properly
- Navigate to the fetch-task folder in packages
Publish NativeScript plugin to npm registry
- Execute npm publish –access public
- This will ask you to login with your npm user id
- Once logged in, the package will get published
You will be able to install it as @npmusername/package-name as in https://www.npmjs.com/package/@adilek/fetch-task
in any native script projects and for MDK, we can have it add adding it in NSPlugins section of MDKProject.json
Use NativeScript plugin in MDK application
Detailed blogs are available around adding native script extensions in MDK such as https://developers.sap.com/tutorials/cp-mobile-dev-kit-nativescript-geolocation.html
Summary
This is one such experience to create native script plugin based on our business logic in-case the provided MDK controls does not support your business requirements and UX designs.
Also you can try integrate any such view extension into MDK application. Please leave questions and suggestions, if any
Recommend
-
47
Consume less, create more 这是一篇 1600 字的...
-
41
迁移云打印服务我们将于 2020 年 12 月弃用 Google 云打印服务。 建议您在 2020 年年底前寻求其他打印解决方案,并完成打印服务迁移。 从 2021 年 1 月 1 日开始,我们将不再支持 Google 云打印服务,运行任何操作系统的设...
-
14
Technical Articles
-
6
In his final letter to shareholders, Amazon CEO Jeff Bezos shares great advice on company building, being distinctive and being atypical. Here are some of the key highlights of the letter. Create More Than You Consume
-
10
Technical Articles
-
12
Table Of Contents In software development in general, we always tend to reuse some code and extract parts of it that we need in multiple places. Front-end development nor React are not an exception. Therefore, sometimes we...
-
6
Responses (1)
-
5
Consume a SAP Sales and Service Core (formerly SAP C4C) API to create Sales Orders using an OData receiver adapter in SAP Cloud Integration Hello! This blog post belongs to a
-
4
In C++/WinRT, how do I create or consume an IReference<T> that wraps a particular value?
-
18
PALLAB HALDAR January 6, 2023 1 minute read
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK