title: How to use custom native java classes / functions in nativescript?
body: <p>So my manager seems to think that converting an app from 2 fully native iOS and Android apps, to one cross-platform angular/nativecript app is a straightforward task (it sounds like a terrible idea to me). Im trying to research the effort required to do this. Ive created a helloworld nativescript app and im trying to see what the best way of integrating existing native code into a nativescript app is (basically i want to have access to my native webservice functions). My knowledge on this subject is severely limited.</p>
  
  <p>Ive seen there is a way to <a href="https://www.nativescript.org/blog/plugins-and-jars" rel="nofollow noreferrer">make your own native packages</a> that can be used, but this seems like it would be way to much effort and cumbersome/messy. Is there no way to just access a custom native java object inside nativescript? for example:</p>
  
  <pre><code>//Java
  package com.tns;
  
  public class TestObject {
      public String doSomething () {
          return "hello";
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>//Nativescript
  import { Component } from "@angular/core";
  
  declare var com: any
  
  @Component({
    selector: "my-app",
    template: `
      &lt;ActionBar title="My App"&gt;&lt;/ActionBar&gt;
      &lt;StackLayout orientation="vertical"&gt;
              &lt;Label [text]="message" (tap)="onTap()"&gt;&lt;/Label&gt;
      &lt;/StackLayout&gt;
    `
  })
  export class AppComponent {
      public message: string = "Hello, Angular";
      public onTap() {
          this.message = "Text Changed";
          var test = new com.tns.TestObject();
          console.log(test.doSomething());
      }
  }
  </code></pre>
  
  <p>edit: for future readers, the above code is in fact working now</p>
  
  <p>I know you <a href="https://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript" rel="nofollow noreferrer">can do it</a> with just normal Android classes, but havent been able to do it with my own custom classes, maybe im just missing something though (something to do with imports maybe?).</p>
  
  <p>I am getting this error when trying to run the program</p>
  
  <pre><code>app/app.component.ts(21,24): error TS2304: Cannot find name 'com'.
  </code></pre>
  
  <p>and I have my object just under the main folder in my project</p>
  
  <p><a href="https://i.stack.imgur.com/8UCze.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8UCze.png" alt="screenshot"></a></p>
  
  <p>Basically they want us to convert certain sections of the android app to nativescript at a time while leaving all the old native functionality in tact, so that eventually the whole project will be converted to nativescript, and thus one code base for both ios and android theoretically. Sounds like a really bad way to go about it, should just start from scratch imo.</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>How to use custom java class in nativescript code?</p>
  
  <p>----- Additions from comments ------</p>
  
  <blockquote>
    <p>An uncaught Exception occurred on "main" thread.
    com.tns.NativeScriptException:  Calling js method onTouch failed</p>
    
    <p>[object Object] File:
    "file:///data/data/org.nativescript.HelloWorld/files/app/tns_modules/tns-core-modules/ui/gestures/gestures.js,
    line: 97, column: 40</p>
    
    <p>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</p>
  
  <pre><code>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)
  ...
  </code></pre>
  </blockquote>