0

I'm looking to create a Nativescript wrapper for a Java library so I can utilize functionalities of it for a Nativescript app. There does not seem to be too many articles that go into this in detail, and there does not seem to be a clear way to do this within the Nativescript app itself which is why I am now making this a plugin wrapper.

The specific Java library I am working to include is Libsignal-protocol-java. I've gone ahead and cloned the Nativescript Plugin Seed and added this Java library as a dependency:

src/platforms/android/include.gradle

android {   
}

dependencies {
  compile 'org.whispersystems:signal-protocol-android:2.3.0+'
}

I then found the particular package that contains the method I am trying ro access within the Java source: KeyHelper.generateRegistrationId(); source. One article mentioned this is required as you'll have to specify the package when instantiating the class and method.

I then setup my libsignal-protocol.common.ts as follows to attempt and use the native method:

src/libsignal-protocol.common.ts

import { Observable } from 'tns-core-modules/data/observable';

export class Common extends Observable {

    constructor() {
      // does not work
      let test1 = new org.whispersystems.libsignal.util.KeyHelper.generateRegistrationId();

      // does not work
      let test2 = org.whispersystems.libsignal.util.KeyHelper.generateRegistrationId();

      console.log(test1);
      console.log(test2);
    }
}

To my dismay, the logger returned this error:

System.err: Error: java.lang.Exception: Failed resolving method generateRegistrationId on class org.whispersystems.libsignal.util.KeyHelper

I am not sure where else to go here now, I wanted to go this route as it seemed safer/cleaner to create a wrapper for this awesome Java library than trying to browserify their javascript library as it requires certain features not available within Nativescript.

Any help or suggestions would be appreciated! For sanity, I will include some articles I have found on this matter that has helped lead me to where I am now.


Sources

1 Answer 1

1

As you see in the source code generateRegistrationId method expects one boolean argument.

public static int generateRegistrationId(boolean extendedRange) {
    try {
      SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
      if (extendedRange) return secureRandom.nextInt(Integer.MAX_VALUE - 1) + 1;
      else               return secureRandom.nextInt(16380) + 1;
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    }
  }

So you must pass a boolean to the method,

let test2 = org.whispersystems.libsignal.util.KeyHelper.generateRegistrationId(false);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Manoj! I guess I didn't think about passing a bool as it looked optional from the codebase. I'm curious why the error was Failed resolving method as it made me assume it could not find the method, not that it couldn't be executed properly.
I think it was the confusion between JS vs Java. When you access a Java method, the runtime maps the method by it's name and number of arguments as Java supports method overloading. You can't skip the arguments like you do with usual JavaScript functions.

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.