How to use *.jar file in native script

plugins

#1

I have a third party .jar file(for printing) built in JAVA and I want to include that .jar file in ‘angular 5 + Native Script’ project and use that .jar file classes in Native Script Project. where to and how to place that .jar file in angular5native script


#2

Just place the jar file inside app/App_Resources/android folder and rebuild your project.
The classes of that jar file will be available to your nativescript app.

Another way would be to build a plugin that uses that jar file.


#3

Thanks for the help. I am new to native script. can you just tell me how to rebuild the project


#4

use the cli command for building:
$ tns build android


#5

thank you @jpedraza. I imported the package like this.

const leopardPkg = com.leopard.api;

Now I am getting the error like cannot find the name com . can you tell me how to import the package correctly.


#6

these articles can help you:

https://docs.nativescript.org/runtimes/android/plugins/plugins


#7

with out creating the package can’t we directly import the *.jar file in the angular component


#8

Hi @janardhan, did you find a solution?
I’m in a similar situation, I have a *.jar file and I want to use it in a nativescript-ng project; if you have any suggestion it will be much appreciated.


#9

It must be possible since 4.x


#10

Hi @manojdcoder, thanks for the tip. Still I don’t get how to use the classes/functions from the library/jar file that I added. After the build this is what I got:

Gradle build...
         + setting applicationId
         + applying user-defined configuration from /Users/MYPATH/Garmin/app/App_Resources/Android/app.gradle
         + adding nativescript runtime package dependency: nativescript-optimized-with-inspector
         + adding aar plugin dependency: /Users/MYPATH/Garmin/node_modules/tns-core-modules-widgets/platforms/android/widgets-release.aar
         + adding jar plugin dependency: /Users/MYPATH/Garmin/app/App_Resources/Android/libs/garmin-fleet-api.jar

How can I call the lib? Thanks for any further advice


#11

Access your class / functions with fully qualified names (for example - new com.gramin.some.Classname())

https://docs.nativescript.org/core-concepts/android-runtime/marshalling/java-to-js


#12

Thanks for your reply @manojdcoder.
I’ve read the link you posted. This is what I’ve tried:

  • installed the tns-platform-declarations
  • create the references.d.ts file to call the tns-platform.declaration for android
  • tried to import the library in different way in the top of my *.ts file, like:

// import { garmin_fleet_api } from "../App_Resources/Android/libs";
// import * as garmin from "../App_Resources/Android/libs";
// var garmin = require("../App_Resources/Android/libs");
  • tried what you suggest

let Serial = new com.garmin.android.comm.SerialPort();

this above gave me the error “error TS2304: Cannot find name ‘com’.”

  • tried also something like this

var garmin = new java.io.File("../App_Resources/Android/libs");

Nothing is working, any suggestion or article?


#13

This is right syntax if your package & class name are correct.

let Serial = new com.garmin.android.comm.SerialPort();

Typescript will obviously throw error as it won’t know what is com.garmin..., simply declare

declare var com: any;

at top of the file. If you like to get type declarations for JAR, use this.


#14

Thanks for your quick reply; I’ll have a look! cheers!


#15

I’ve tried different way and path to access the library without success.

Attached here the link to the app with the jar file I’m trying to use.

jar source

I’ve also opened it (added to the *.jar .zip so it’s possible to read the content) and the path is right. trying this:

let serial = new com.garmin.android.comm.SerialPort();

reply with error:
ERROR TypeError: Cannot read property 'android' of undefined

also tried a simple class:
let test = new com.garmin.dashcam.VideoProperties("aa", 1, 1, 1);

ERROR TypeError: Cannot read property 'dashcam' of undefined

with or without passing the parameters, same result; it looks like it does not read/access the path or something.


#16

I just cloned your repo and run the project, it works just fine on my end.

Note: I’m using the latest version of CLI. Make sure you run a clean build.


#17

It works, thank you very much for your help @manojdcoder! I forgot that sometimes in this “world” is necessary to delete the famous folders from scratch.