1

Today I started learning nativescript. I am trying to build simple ng2 app which would use 3rd party java library. I am trying to achieve something like in here

http://developer.telerik.com/featured/using-native-libraries-in-nativescript/

but I keep getting undefined errors.

I have no problem of accessing java within my ts code:

import app = require("application");
import platform = require("platform");
declare var java;
.......
public get message(): string {
        var str = new java.lang.String('Hello world!');
        var result = str.endsWith('world!');
        console.log(result); // true

But I can't access 3rd party java library

import {Component} from "@angular/core";
import app = require("application");
declare var KontaktSDK;

@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
})
export class AppComponent {
    public counter: number = 16;

    public onTap(args) {
        KontaktSDK.initialize("API_KEY");
        this.counter--;
    }
}

It throws reference error "KontaktSDK is not defined"

Thank you for your help!

1 Answer 1

4

When accessing android native methods in nativescript you need to use the full package name and class e.g com.kontakt.sdk.android.common.KontaktSDK.initialize("API_KEY") also a little trick i picked up while developing nativescript plugins is you can use console.dump(com.kontakt.sdk.android.common.KontaktSDK) on a class and it would display all methods contained in that class . When in doubt log it out

Sign up to request clarification or add additional context in comments.

3 Comments

Beat me to it @Osei. Also make sure you have the dependency built into the actual application. If you have previously ran this app, you need to follow the steps here: bradmartin.net/2016/07/20/… this will ensure that you dependency is actually being packaged into the .apk and then run it. Also do what Osei suggests.
Awesome! thanks. Now i need to figure out how to access all of libraries's classes and grand app permissions :)
You can check out github.com/NathanaelA/nativescript-permissions :- permission granting made ez :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.