How to integrate custom Java code with TypeScript?


I have a custom Java code I would like to integrate in my NativeScript application. However, I struggle with the integration of the code.

  1. I placed the Java code in platforms/android/src/main/java/... Is this correct? According to Grocery tutorial, I should treat platforms directory as generated code, however, no other location makes sense to me.

  2. When I try to use the code in TypeScript, I get type errors as the compiler has no clue about classes, methods and interfaces in my Java code. How to provide type definitions? Is there a tool, which can generate .d.ts files? If I have these files, how to properly use them?


Hey @yaqwsx

  1. I recommend that you distribute your Java code in an .aar, or in the worst case - a simple .jar, and then reference it as a local node_module. You could still place your code in platforms/android/src/main for testing purposes, but that directory isn’t persistent, as it’s completely wiped when resetting platforms.
  2. It is normal that TypeScript will have no idea of your Java classes. You can:
  • enable TypeScript compilation to go through even when there are errors in the TS code. noEmitOnErrors: false
  • write declare var myPluginpackagename: any (e.g. if your java package is yaqwsx.classes.MyImplementation you should use declare var yaqwsx: any. This will allow you to access members of the yaqwsx package in TypeScript without the compiler complaining.
  • you could write your own typings in a .d.ts that describe the API that you will be using in the NativeScript code (RECOMMENDED)
  • there’s a tool that is still in experimental stages, that will try to output TypeScript definitions when you feed it with jars ->

From the above it would appear that writing your own definitions will be the quickest way to move forward. Good luck!


Thank you for answer. So basically, there’s no point of integrating the code directly and I should create a NS plugin. So I tried - I built aar from my java code, I put it into platform/android/lib of my plugin, created TS wrapper. When I try to build my application, the build fails:

* Where:
Build file '<app>/platforms/android/build.gradle' line: 704
* What went wrong:
Execution failed for task ':myMergeDex'.
> Could not find matching constructor for:[;,

Which is this line:

def dexMerger = new,

What am I doing wrong in my setup?

EDIT: Clearing platform folder and building the app again helped


I have one more question regarding the modules - how can I restrict my implementation only to certain platforms (as I am currently able to only support Android)? When I renamed my file index.ts to the package stops working.

Is there a specification of the package.json & package structure, which reflects all the NativeScript details, somewhere?


Binaries should be set in the respective platform’s dir -> MyGloriousPlugin/platforms/android/plugin.aar and for ios respectively MyGloriousPlugin/platforms/ios/plugin.framework. Neither of the two is necessary if your JavaScript code doesn’t depend on them.

As for platform-specific scripts - you always want to distribute both .android. and .ios so that your application doesn’t crash. Having said that - if the module doesn’t have an implementation for another platform, or one is not relevant - you should create stub methods.

Suppose I want a nativescript plugin that has android-only support and its public API is the function doStuff
module.exports = { doStuff: function() { ......... } }
module.exports = { doStuff: function() { } }


Thanks a lot! Now it is getting much clearer to me. However, I run into (I hope the last) problem. My package depends on tns-platform-declarations/android.d.ts. So I specified tns-platform-declarations as a developer dependency, put /// <reference path="./node_modules/tns-platform-declarations/android.d.ts" / in my index.ts file and when I build the package, it works nice.

However, when I install the package to my project, during the build of my app I get error message:

error TS6053: File '.../node_modules/my-module/node_modules/tns-platform-declarations/android.d.ts' not found.

That means, it is referring to folder node_modules inside my module, not the one which has all the dependencies. How should I properly refer to referencies from other packages?