Error using an .aar library file with nativescript plugin seed

android

#1

Hi,

I’m creating a plugin for nativescript using this plugin seed https://github.com/NathanWalker/nativescript-plugin-seed .

I’m trying to use a library I exported from Android Studio (.aar file). The library is a very simple example showing a Toast.

When I used it on an Android Studio project it worked as expected. But when I try to use it in my plugin seed project, it throws me an error. I don’t know if I’m using it correctly, so Would somebody help me getting it works, please?

This is the error:

Calling js method onCreateView failed
System.err:
System.err: TypeError: com.example.toaster.Toaster is not a constructor
System.err: File: "file:///data/data/org.nativescript.demo/files/app/tns_modules/nativescript-spark-SDK/spark-SDK.js, line: 11, column: 20
System.err:
System.err: StackTrace:
System.err: 	Frame: function:'SparkSDK.showToast', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/nativescript-spark-SDK/spark-SDK.js', line: 11, column: 21
System.err: 	Frame: function:'HelloWorldModel', file:'file:///data/data/org.nativescript.demo/files/app/main-view-model.js', line: 10, column: 24
System.err: 	Frame: function:'pageLoaded', file:'file:///data/data/org.nativescript.demo/files/app/main-page.js', line: 6, column: 27
System.err: 	Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 110, column: 23
System.err: 	Frame: function:'Observable._emit', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 127, column: 18
System.err: 	Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 236, column: 14
System.err: 	Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 194, column: 35
System.err: 	Frame: function:'Page.onLoaded', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/page/page.js', line: 43, column: 35
System.err: 	Frame: function:'', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 311, column: 90
System.err: 	Frame: function:'ViewBase.callFunctionWithSuper', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 304, column: 9
System.err: 	Frame: function:'ViewBase.callLoaded', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 311, column: 14
System.err: 	Frame: function:'ViewBase.loadView', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 439, column: 18
System.err: 	Frame: function:'ViewBase._addViewCore', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 434, column: 18
System.err: 	Frame: function:'ViewBase._addView', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 420, column: 14
System.err: 	Frame: function:'FragmentCallbacksImplementation.onCreateView', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 576, column: 24
System.err: 	Frame: function:'FragmentClass.onCreateView', file:'file:///data/data/org.nativescript.demo/files/app/tns_modules/tns-core-modules/ui/frame/fragment.js', line: 27, column: 38
System.err:
System.err: 	at com.tns.Runtime.callJSMethodNative(Native Method)
System.err: 	at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
System.err: 	at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
System.err: 	at com.tns.Runtime.callJSMethod(Runtime.java:957)
System.err: 	at com.tns.Runtime.callJSMethod(Runtime.java:941)
System.err: 	at com.tns.Runtime.callJSMethod(Runtime.java:933)
System.err: 	at com.tns.FragmentClass.onCreateView(FragmentClass.java:40)
System.err: 	at android.app.Fragment.performCreateView(Fragment.java:2626)
System.err: 	at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1279)
System.err: 	at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1554)
System.err: 	at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1617)
System.err: 	at android.app.BackStackRecord.executeOps(BackStackRecord.java:813)
System.err: 	at android.app.FragmentManagerImpl.executeOps(FragmentManager.java:2401)
System.err: 	at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2196)
System.err: 	at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2149)
System.err: 	at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2056)
System.err: 	at android.app.FragmentManagerImpl$1.run(FragmentManager.java:719)
System.err: 	at android.os.Handler.handleCallback(Handler.java:808)
System.err: 	at android.os.Handler.dispatchMessage(Handler.java:101)
System.err: 	at android.os.Looper.loop(Looper.java:166)
System.err: 	at android.app.ActivityThread.main(ActivityThread.java:7415)
System.err: 	at java.lang.reflect.Method.invoke(Native Method)
System.err: 	at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
System.err: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
ActivityManager: Process org.nativescript.demo (pid 21559) has died: fore TOP
ActivityManager: cleanUpApplicationRecord app: ProcessRecord{e99300e 21559:org.nativescript.demo/u0a2497}, app.bad: false, restarting: false, allowRestart: true
ActivityManager: setHasOverlayUi called on unknown pid: 21559

This is my include.gradle.

/* Include.gradle configuration: http://docs.nativescript.org/plugins/plugins#includegradle-specification */

android {
    
}

dependencies {
    // Describe plugin native Android dependencies like
	// compile "groupName:pluginName:ver" 
    // EXAMPLE: compile "com.facebook.fresco:fresco:0.9.0+"
   
}

My android.ts

import { Common } from './spark-SDK.common';
import application = require("tns-core-modules/application");

declare var com: any;

export class SparkSDK extends Common {

    
    public showToast(){
        var toast = new com.example.toaster.Toaster();
        toast.shortToast(application.android.context,"Hello World");
    }
    

}

My project structure

My library class:

package com.example.toaster;

import android.content.Context;
import android.widget.Toast;

public class Toaster {

    public void shortToast(Context context, String string){

        Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
    }
}

#2

Hi, @gsalazar This error happens because the project was not built well, remove the platform folder from your demo and rerun the command npm run demo.android and tell me.


#3

Hi, @nericode .

Thanks you for your reply!
It works! I’ve done this before with a nativescript project (remove the platforms), but I never thought that in this case that was really the solution.

Thank you so much!! :slight_smile:

Kind regards!


#4

Good thing it worked for you, you can mark it as a solution :grin: