如何在 nativescript 中使用自定义原生 java 类 / 函数?

How to use custom native java classes / functions in nativescript?

所以我的经理似乎认为将应用程序从 2 个完全原生的 iOS 和 Android 应用程序转换为一个跨平台 angular/nativecript 应用程序是一项简单的任务(听起来对我来说是个糟糕的主意)。我正在尝试研究执行此操作所需的努力。我创建了一个 helloworld nativescript 应用程序,我正在尝试了解将现有本机代码集成到 nativescript 应用程序中的最佳方法是什么(基本上我想访问我的本机 web 服务功能)。我对这个问题的了解非常有限。

我看到有一种方法可以使用 make your own native packages,但这似乎需要很多努力 cumbersome/messy。有没有办法只访问 nativescript 中的自定义本机 java 对象?例如:

//Java
package com.tns;

public class TestObject {
    public String doSomething () {
        return "hello";
    }
}

//Nativescript
import { Component } from "@angular/core";

declare var com: any

@Component({
  selector: "my-app",
  template: `
    <ActionBar title="My App"></ActionBar>
    <StackLayout orientation="vertical">
            <Label [text]="message" (tap)="onTap()"></Label>
    </StackLayout>
  `
})
export class AppComponent {
    public message: string = "Hello, Angular";
    public onTap() {
        this.message = "Text Changed";
        var test = new com.tns.TestObject();
        console.log(test.doSomething());
    }
}

编辑:对于未来的读者,上面的代码实际上现在可以工作了

我知道你 can do it 只是正常的 Android classes,但没能用我自己的自定义 classes 做到这一点,也许我只是想念不过有些事情(也许与进口有关?)。

我在尝试 运行 程序时遇到此错误

app/app.component.ts(21,24): error TS2304: Cannot find name 'com'.

我的对象就在项目的主文件夹下

基本上他们希望我们一次将 android 应用程序的某些部分转换为 nativescript,同时保留所有旧的 native 功能,以便最终将整个项目转换为 nativescript,因此理论上,ios 和 android 的一个代码库。听起来这是一个非常糟糕的方法,我应该从头开始。

TL;DR

如何在 nativescript 代码中使用自定义 java class?

-----评论补充-----

An uncaught Exception occurred on "main" thread. com.tns.NativeScriptException: Calling js method onTouch failed

[object Object] File: "file:///data/data/org.nativescript.HelloWorld/files/app/tns_modules/tns-core-modules/ui/gestures/gestures.js, line: 97, column: 40

StackTrace: Frame: function:'GesturesObserver.androidOnTouchEvent', file:'file:///data/data/org.nativescript.HelloWorld/files/app/tns_modules/tns-core-modules/ui/gestures/gestures.js', line: 97, column: 41 Frame: function:'onTouch', file:'file:///data/data/org.nativescript.HelloWorld/files/app/tns_modules/tns-core-modules/ui/core/view.js', line: 113, column: 37

at com.tns.Runtime.callJSMethodNative(Native Method)
at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1197)
at com.tns.Runtime.callJSMethodImpl(Runtime.java:1061)
at com.tns.Runtime.callJSMethod(Runtime.java:1047)
at com.tns.Runtime.callJSMethod(Runtime.java:1028)
...

尝试排除那些圆括号 :) 同样的事情分配 类 我在插件中使用。

编辑

它应该是那些括号 :D

var test = new com.tns.TestObject();
console.log(test.doSomething());

正如@Brad Martin 在评论中解释的那样,您只需声明 com 是什么,您就可以在您的 nativescript 代码中使用 java 文件,而不会遇到任何麻烦

//Java
package com.tns;

public class TestObject {
    public String doSomething () {
        return "hello";
    }
}

//Nativescript
import { Component } from "@angular/core";

declare var com: any //this is the important part

@Component({
  selector: "my-app",
  template: `
    <ActionBar title="My App"></ActionBar>
    <StackLayout orientation="vertical">
            <Label [text]="message" (tap)="onTap()"></Label>
    </StackLayout>
  `
})
export class AppComponent {
    public message: string = "Hello, Angular";
    public onTap() {
        this.message = "Text Changed";
        var test = new com.tns.TestObject(); //cant use this without the declare above
        console.log(test.doSomething()); //will print "hello" in the console
    }
}

我的 java 文件在 android 项目中的位置: