Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
AthiraLekshmi
Associate
Associate

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.




  1. Click on  "Use this template" button in https://github.com/NativeScript/plugin-seed

  2. Follow GitHub instructions (set repo name, visibility, description) & clone your new repo

  3. Now that we have the template in our local, execute the command


           npm run setup

to set up the workspace

  1. Configure your npm scope by executing the command,


           npm run config

  1. Since we are planning to publish it as a user scope package, name the scope with your npm repository user name

  2. Provide the rest of the details and we will get our workspace configured with the new scope.

  3. Now we need to add a new package, i.e the folder within which our actual plugin will reside

  4. Execute the command,


           npm run add

  1. 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.

  2. 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.

  3. 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.

  4. 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();
    }
    }


  5. Now, we can test the implementation. On the plugin root folder runnpm start


  6. Select apps.demo.android (since we are testing on an android emulator) 



  7. On click of “test hello world toast” the console logs hello world

  8. Now that we have the plugin, we can deploy it

  9. Navigate to the hello-world-toast folder in packages

  10. Execute npm publish --access public

  11. This will ask you to login with your npm user id

  12. 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.




  1. Create a new package fetch-task

  2. 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.


 



 

  1. 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.

  2. Execute the command,


           ns migrate  (this command helps us migrate the library dependencies)

  1. 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

  2. Add the aar file in platforms/android/libs folder in the fetch-task package





  1. Change the two entries in package.json as below





  1. Make changes as in the original native script-background-fetch plugin demo folder in the apps/demo/src folder

  2. Execute npm run start and run the package in android to check if the plugin is working properly

  3. Navigate to the fetch-task folder in packages


 

Publish NativeScript plugin to npm registry



  1. Execute npm publish --access public

  2. This will ask you to login with your npm user id

  3. 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 🙂