'How do we use Android package JAR in NativeScript
I'm following the tutorial below but getting issues using the JAR file in nativescript.
https://nativescript.org/blog/plugins-and-jars/
I was able to generate a JAR file in Android studio with the code below.
package com.testing.toaster;
import android.content.Context;
import android.widget.Toast;
public class Toaster {
public void show(Context context) {
CharSequence text ="Hello NativeScript!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
Then I created a nativscript project using the plugin seed. The plugin is working fine after creation.
I added the ToastPlugin.jar under src/platforms/android/libs folder.
Here's the code in nativescript plugin using the JAR file.
import { Observable } from 'tns-core-modules/data/observable';
import * as app from 'tns-core-modules/application';
import * as dialogs from 'tns-core-modules/ui/dialogs';
declare var com:any;
export class Common extends Observable {
public message: string;
constructor() {
super();
this.message = Utils.SUCCESS_MSG();
}
public greet() {
return "Hello, NS";
}
}
export class Utils {
public static SUCCESS_MSG(): string {
let msg = `Your plugin is working on ${app.android ? 'Android' : 'iOS'}.`;
console.log("Ryan1:" + app.android.context);
var toaster = new com.testing.toaster.Toaster();
//toaster.show(app.android.context);
return msg;
}
}
Here's the error I'm getting.
Restarting application on device emulator-5554...
Successfully synced application org.nativescript.demo on device emulator-5554.
JS: HMR: Hot Module Replacement Enabled. Waiting for signal.
JS: Ryan1:com.tns.NativeScriptApplication@129d41d
System.err: An uncaught Exception occurred on "main" thread.
System.err: Unable to start activity ComponentInfo{org.nativescript.demo/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err: > Cannot read property 'toaster' of undefined
System.err:
System.err: StackTrace:
System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.demo/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err: > Cannot read property 'toaster' of undefined
System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
System.err: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
System.err: at android.os.Looper.loop(Looper.java:193)
System.err: at android.app.ActivityThread.main(ActivityThread.java:6669)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
System.err: Caused by: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err: > Cannot read property 'toaster' of undefined
System.err: at com.tns.Runtime.callJSMethodNative(Native Method)
System.err: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286)
System.err: at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1160)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1138)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1134)
System.err: at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:19)
System.err: at android.app.Activity.performCreate(Activity.java:7136)
System.err: at android.app.Activity.performCreate(Activity.java:7127)
System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
System.err: ... 11 more
Any idea what I'm missing? Please bear with me as I'm new to nativescript.
Thanks, Ryan
Solution 1:[1]
You can put Java JAR files in App_Resources/Android/libs
and text Java source code files in App_Resources/Android/src/main/java
App_Resources
??? Android
? ??? libs
? ??? src
? ??? main
? ??? AndroidManifest.xml
? ??? java
...
Solution 2:[2]
one of the issues you’ll have is that the tutorial you’re following is from 2015, and the platform has significantly changed since then.
Generally the way you’d include native code is via plugins. So it would be useful to know what you’re trying to achieve. If you want to show toasts check out the existing plugins for showing toasts (nativescript-toast and nativescript-toasty will give you an idea of how this can be achieved).
If you’re trying to learn to interact more with the native layer my advice would be to check out the Building Plugins guide here: https://docs.nativescript.org/plugins/building-plugins
If you walk through that tutorial you’ll find the process a bit more straight forward and it should be up to date. Then there’s some useful info on how to talk to other devs about plugin development you may find helpful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | isapir |
Solution 2 | James Macmillan |