Extended java interface can't work with typescript

android

#1

I am using Nativescript+Typescrit, I have a java class and interface in native plugin, and I want to use them directly in typescript, to clearly describe the problem, I made a simple code as follow:

the Java interface is like:

package com.test;

public interface Animal {
    public void eat();
}

The Java class is using this interface like:

package com.test;

public class Box {
    private Animal animal;

    public void setAnimal(Animal a){
        this.animal = a;
    }

    public void startEat(){
        this.animal.eat();
    }
}

In typescript, I defined a class implements Animal like this:

@Interfaces(com.test.Animal)
export class Cat extends java.lang.Object {
  constructor() {
    super();
    return global.__native(this);
  }

  public eat():void {
    console.log("cat is eating");
  }
}

Now I want to use it like this:

import { Cat } from "./cat"

let box = new com.test.Box();
box.setAnimal(new Cat());
box.startEat();

after running in android emulator, I got this error:

05-11 11:01:28.858 24611-24611/org.nativescript.ZeroNSClient E/art: JNI ERROR (app bug): attempt to pass an instance of com.tns.gen.java.lang.Object_cat_6_28_Cat as argument 1 to void com.test.Box.setAnimal(com.test.Animal)
05-11 11:01:28.864 24611-24611/org.nativescript.ZeroNSClient A/art: art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: bad arguments passed to void com.test.Box.setAnimal(com.test.Animal) (see above for details)
    art/runtime/java_vm_ext.cc:470]     from java.lang.Object com.tns.Runtime.callJSMethodNative(int, int, java.lang.String, int, boolean, java.lang.Object[])
    art/runtime/java_vm_ext.cc:470] "main" prio=5 tid=1 Runnable
    art/runtime/java_vm_ext.cc:470]   | group="main" sCount=0 dsCount=0 obj=0x7464e610 self=0xb0c8b400
    art/runtime/java_vm_ext.cc:470]   | sysTid=24611 nice=-10 cgrp=default sched=0/0 handle=0xb5003534
    art/runtime/java_vm_ext.cc:470]   | state=R schedstat=( 0 0 0 ) utm=226 stm=315 core=2 HZ=100
    art/runtime/java_vm_ext.cc:470]   | stack=0xbf30a000-0xbf30c000 stackSize=8MB
    art/runtime/java_vm_ext.cc:470]   | held mutexes= "mutator lock"(shared held)
    art/runtime/java_vm_ext.cc:470]   native: #00 pc 00572f6e  /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+238)
05-11 11:01:28.865 24611-24611/org.nativescript.ZeroNSClient A/art: art/runtime/java_vm_ext.cc:470]   native: #01 pc 0053f35e  /system/lib/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+526)
    art/runtime/java_vm_ext.cc:470]   native: #02 pc 0053c35b  /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+75)
    art/runtime/java_vm_ext.cc:470]   native: #03 pc 00392418  /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1560)
    art/runtime/java_vm_ext.cc:470]   native: #04 pc 00392dcc  /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortFEPKcS2_z+124)
    art/runtime/java_vm_ext.cc:470]   native: #05 pc 00511367  /system/lib/libart.so (_ZN3artL18InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_9ArtMethodEPNS_8ArgArrayEPNS_6JValueEPKc+3575)
    art/runtime/java_vm_ext.cc:470]   native: #06 pc 00511acc  /system/lib/libart.so (_ZN3art35InvokeVirtualOrInterfaceWithJValuesERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectP10_jmethodIDP6jvalue+380)
    art/runtime/java_vm_ext.cc:470]   native: #07 pc 003e3b10  /system/lib/libart.so (_ZN3art3JNI15CallVoidMethodAEP7_JNIEnvP8_jobjectP10_jmethodIDP6jvalue+1056)
    art/runtime/java_vm_ext.cc:470]   native: #08 pc 00142ab5  /system/lib/libart.so (_ZN3art8CheckJNI11CallMethodAEPKcP7_JNIEnvP8_jobjectP7_jclassP10_jmethodIDP6jvalueNS_9Primitive4TypeENS_10InvokeTypeE+2197)
    art/runtime/java_vm_ext.cc:470]   native: #09 pc 001302ac  /system/lib/libart.so (_ZN3art8CheckJNI15CallVoidMethodAEP7_JNIEnvP8_jobjectP10_jmethodIDP6jvalue+108)
    art/runtime/java_vm_ext.cc:470]   native: #10 pc 00088849  /data/app/org.nativescript.ZeroNSClient-1/lib/x86/libNativeScript.so (???)
    art/runtime/java_vm_ext.cc:470]   native: #11 pc 0005f5d1  /data/app/org.nativescript.ZeroNSClient-1/lib/x86/libNativeScript.so (???)
    art/runtime/java_vm_ext.cc:470]   native: #12 pc 000958f1  /data/app/org.nativescript.ZeroNSClient-1/lib/x86/libNativeScript.so (???)
    art/runtime/java_vm_ext.cc:470]   native: #13 pc 0073db31  /data/app/org.nativescript.ZeroNSClient-1/lib/x86/libNativeScript.so (???)
    art/runtime/java_vm_ext.cc:470]   native: #14 pc 007a4842  /data/app/org.nativescript.ZeroNSClient-1/lib/x86/libNativeScript.so (???)
    art/runtime/java_vm_ext.cc:470]   native: #15 pc 007a4d4c  /data/app/org.nativescript.ZeroNSClient-1/lib/x86/libNativeScript.so (???)
    art/runtime/java_vm_ext.cc:470]   native: #16 pc 007a5097  /data/app/org.nativescript.ZeroNSClient-1/lib/x86/libNativeScript.so (???)
    art/runtime/java_vm_ext.cc:470]   native: #17 pc 0000007d   (???)
    art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethodNative(Native method)
    art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
    art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
    art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethod(Runtime.java:957)
    art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethod(Runtime.java:941)

I paid a lot of time checking this problem, but got no idear, the most possibility is I wrongly extended the Animal interface, I did it according to this extend-class-interface.

But it works if I use the interface in as this in typescript:

 import { Cat } from "./cat"

let box = new com.test.Box();
box.setAnimal(new com.test.Animal({
    eat() {
      console.log("I am eatting");
    }
}));
box.startEat();

So I wander why I can’t extend the java interface in typescript, thanks for any helpful answer.


#2

@Interfaces decorator seems to expect an array, try @Interfaces([com.test.Animal])


#3

I have tried already, still the same problem.


#4

Did you try with the implements syntax too?

@Interfaces([com.test.Animal])
export class Cat extends java.lang.Object implements com.test.Animal {
  constructor() {
    super();
    return global.__native(this);
  }

  public eat():void {
    console.log("cat is eating");
  }
}

#5

Not before, just tried still not work, I think the ‘implements’ is just a sugar from typescript, it will not change the generated js file.


#6

It works now without any change, I run it in emulator, maybe an complete restart of emulator or tns run fix the issue.