Extend Legacy Java Android Applications With NativeScript And Angular

As some of you might know, before I started developing cross platform mobile applications using Ionic Framework and NativeScript, I was developing Android applications using nothing more than the Android SDK and Java. I still have one of my first applications, SQLTool, for sale in Google Play, and it was written with Java and Android. When building mobile applications, frameworks like NativeScript are incredibly attractive to web developers because they can use a common set of web design and development skills. However, NativeScript can be equally attractive to development teams writing Android applications with Java.

With NativeScript you can actually build a “polyglot” type application where parts are Java and parts are NativeScript with Angular. To be clear, I’m not talking about rewriting your Java application into modules wrapped in JavaScript like plugins. I’m saying that you can take your 100% Android with Java application and add Activities or Fragments built with NativeScript.

So why is this useful?

Imagine being on a small Android development team with more things to be done than there is time in the day. These Android developers would rather die than use a cross platform framework, but you have plenty of web developers floating around your company. In this scenario, the two worlds can collide and maybe the web developers can create lower (or higher) priority components for the Android application without having to convert the whole application to Android with Java or to NativeScript with Angular.

We’re going to take a look at building, what I’m going to call, a polyglot Android application. This will be an Android with Java application that receives NativeScript components to add features.

If you don’t have any legacy Android applications floating around, that is fine. It is also fine if you don’t know Android with Java or NativeScript with Angular because it is the concepts that we’re after instead. Our project goal is to create a currency exchange tracker with Android and Java that shows the conversion rates that have been gathered from a free and public API.

Android with NativeScript Extension Project

Then we’re going to decide that we also want to show stock ticker information for various publicly traded companies. This part of the application will have been created with NativeScript and Angular. In fact, I actually wrote about this stock ticker application in a previous tutorial titled, Working with RESTful Data in Angular and NativeScript.

The Requirements

There are a few requirements that must be met in order to follow along with the content in this guide.

  • Android Studio 2.3+
  • NativeScript 3.0+

Android Studio is required to build the Java side of our application. It also gives us the Android SDK, a requirement for building NativeScript applications.

Creating a Currency Conversion Application with Android and Java

For simplicity we’re going to create a fresh Android project rather than trying to mess around with old code that can be completely insane. In Android Studio, let’s create a fresh, single Activity, project.

Android Studio Project 1

It doesn’t really matter what you name it, but it does help avoid confusion if you name it something in regards to currency. Otherwise you might get confused when we jump between NativeScript and Android Studio. The next step is to define what versions of Android you wish to support within your application.

Android Studio Project 2

There is a requirement at this step. NativeScript does have a requirement on Android API 17 or higher because of the various libraries that it makes use of. Down the road, this requirement may become more flexible or more strict. The next step is to define some boilerplate Activities or Fragments.

Android Studio Project 3

Since this application is fresh and simple, we’re going to choose to use a Basic Activity, but feel free to use something more complex. Finally, we’ll need to provide information about the Android Activity that we’re creating.

Android Studio Project 4

Again, all this is very basic setup and not necessarily relevant to the merging of our technologies. It is just a starting point to make life a little easier.

Going forward, you could do a lot of the UI related stuff with the WYSIWYG editor, but I’ll be providing the raw XML markup instead. It will easily translate in Android Studio to something that can be understood. Open the project’s app/res/layout/content_main.xml and include the following XML markup:

The above XML will set us up for listing any currency information returned from the API we plan on using. Next, open the project’s app/res/layout/activity_main.xml and include the following XML markup:

The above XML is pretty much what we had initially, however, we’ve gone ahead and removed the floating action button (FAB) as it won’t be used in this example. While not part of the core content, the menu for this application should also be updated. Open the project’s app/res/menu/menu_main.xml and include the following:

To make our HTTP requests that will populate the Android ListView component, we’re going to be using a library called Volley. If you remember correctly, I wrote about using this library in a previous tutorial titled, Using Volley to Make Android HTTP Requests. To use this library, we need to include it in our Gradle file. Open the project’s Gradle Scripts/build.gradle file for the app module and include the following line:

With that line included, sync the project dependencies and get ready to get into some Java. Open the project’s app/java/com/thepolyglotdeveloper/currencyconversion/MainActivity.java and include the following Java code which we’ll break down in a moment:

Most of the code above came with the template that was used when Android Studio created our project. If you look carefully, we have some changes in the onCreate method as follows:

In the above we are obtaining a reference to the list found in the UI. We are also attaching a basic ArrayAdapter which will bind data found in an ArrayList to the list. Essentially the ArrayList data is what we get back from the API.

Jumping down to the onStart method, we have something a little more complex:

In the above we are using Volley to hit the Fixer API. We are taking the JSON result and adding each key-value pair to the ArrayList that is bound to our UI. Again, for more information on using Volley, check out the previous tutorial that I wrote.

Since the Android project uses the internet to gather remote data, we need to add application permissions. Within the project’s AndroidManifest.xml file, include the following permission:

If you try to run the project in a simulator or on your device, it should look something like the following animated image.

Currency Conversion with Fixer

When the application loads we can see the various currencies as well as a menu item. A look into the future will tell us that the menu item will open a NativeScript Activity.

Creating a Stock Quote Tracker with NativeScript and Angular

Like I had previously mentioned, I explored how to get stock quotes via NativeScript and Angular in a tutorial I had written on the Telerik Developer Network. You can download that project or follow the steps that were outlined. However, that project is overkill for what we want to accomplish. Instead, it would benefit us to define a few market symbols rather than doing it via user input. If you really want user input, I suggest you follow the other guide as it doesn’t really move this particular story along.

From the NativeScript CLI, execute the following to create a new NativeScript with Angular project:

The --ng in the above command indicates that we are creating an Angular project rather than a NativeScript Core project.

With a fresh project created, we’re going to pick and pull from the Telerik Developer Network article. Open the project’s app/app.component.ts file and include the following TypeScript code:

In the above TypeScript we are providing a static list of symbols and we are using the Markit On Demand API to get information about them. Any information we retrieve will be added to the stocks variable that is bound to the NativeScript UI. The UI that pairs to this TypeScript can be found in the project’s app/app.component.html file. Open it and include the following HTML markup:

This just loops over our list of quotes and displays them on the screen. Because we’re using the Angular HTTP module, it needs to be included with the other Angular modules. Open the project’s app/app.module.ts file and include the following:

The above TypeScript is a bit overkill as most of it came with our downloaded template. We’ve only added information regarding HTTP to this file.

With the logic, UI, and modules in place, the end result should look something like the following:

NativeScript Stock Ticker in Android

It is nicely formatted and solves our problem when it comes to quote information. So what if your company needs the stock application created by Angular developers, integrated with the currency application created by Java developers? This is where the real magic happens!

Extending the Legacy Android with Java Application to Include the NativeScript Components

When learning how to extend Android with Java applications to support NativeScript, I initially took a look at an old example in the NativeScript documentation. It was enough to get me started, but it had a ton of holes in it. We’re going to get this working without any of the hassle.

For our goals to be accomplished, the NativeScript application must first be built for Android. This can be accomplished by executing the following from within your NativeScript project:

Building the NativeScript project gives us a bunch of great things in the platforms directory of our NativeScript project.

At this point we need to start copying files from our NativeScript project into our Android Studio project. Copy the ns-project/platforms/android/src/main/assets directory into the android-project/app/src/main directory.

Android with NativeScript Monster 1

The assets directory contains all of the compiled TypeScript code which will play an important role in the finished Android project. We also need to copy over the compiled Java classes that were created when we built the NativeScript project for Android.

Now we need to copy the NativeScript ns-project/platforms/android/src/main/java/com/tns directory to the Android Studio android-project/app/src/main/java/com directory.

Android with NativeScript Monster 2

You can see the trend here on how we’re copying NativeScript content. It is aligning with what we have in the Android Studio project.

There are two Android modules that NativeScript uses and they contain various classes among other necessary things. These AAR files must be copied and referenced in our Android Studio project, otherwise the next steps will throw many compile time errors. We can’t just copy and paste like we did in the previous steps. Instead these modules need to be added via Android Studio.

Choose File -> New -> New Module… from the menu in Android Studio.

Android with NativeScript Monster 3

We’re not actually creating a new module, but instead importing an already existing AAR library. This will need to be done for both the NativeScript widgets-release.aar and nativescript.aar files.

Android with NativeScript Monster 4

When prompted, find each of the AAR files. The nativescript.aar file can be found at ns-project/platforms/android/libs/runtime-libs/nativescript.aar and the widgets-release.aar can be found at ns-project/platforms/android/src/F0/widgets-release.aar.

Android with NativeScript Monster 5

While each of the two modules have been added to the Android Studio project, we need to tell Gradle to include them in the compile process to avoid missing class errors.

Open the Android Studio project’s Gradle Scripts/build.gradle file for the app module and add the following:

Try to build the project. We’re not done, but at least we shouldn’t get any errors.

This is where things can get a little weird. We need to create some helper files that link the two projects together. We’re not adding code to be able to navigate between the projects, we’re only adding code to link them.

Create an android-project/app/src/main/java/com/thepolyglotdeveloper/currencyconversion/MyCustomNativeScriptActivity.java file within your Android Studio project. This file should contain the following:

Make sure to match the package at the top to that of your actual package. To be clear, the above file was taken from the GitHub repository used in the official documentation. Make sure to create an android-project/app/src/main/java/com/thepolyglotdeveloper/currencyconversion/MyCustomNativeScriptFragment.java file in the Android Studio project as well, with the following Java code:

Just like with the previous, make sure you have the correct package, otherwise you’ll get class related errors.

You might have noticed that each of these two Java files reference a JavaScript file. We need to copy each of the two referenced files.

Create an android-project/app/src/main/assets/app/MyCustomNativeScriptActivity.js file within the Android Studio project with the following JavaScript code:

The above code was taken from the NativeScript GitHub repository referenced in the documentation. Take note that the Android Activity referenced in JavaProxy was changed to match that of our actual project.

Likewise, you’ll need the matching Fragment file. Create an android-project/app/src/main/assets/app/MyCustomNativeScriptFragment.js file in the Android Studio project with the following JavaScript code:

Just to summarize what we just did. We took four files from the NativeScript documentation. An Android and JavaScript file for Fragments as well as an Android and JavaScript file for Activities. From what I’ve found, these files will be consistent in any Android with NativeScript polyglot project as long as you change the packages and classes to match that of your own.

Like with any Android Activity, it needs to be added to the AndroidManifest.xml file. The AndroidManifest.xml file should include the following line:

Like previously mentioned, we’ve only linked NativeScript with Android as of now. We’re not actually navigating from Android to NativeScript. To do this, consider the following within the Android Studio project’s android-project/app/src/main/java/com/thepolyglotdeveloper/currencyconversion/MainActivity.java file:

The above method will launch our NativeScript Activity from within the Android project. However, we need to call this method first. It makes sense to use our menu item.

Try to compile and run the application now. If all went well, you should have your original Android application, now with NativeScript components.


You just saw how to extend your legacy Android with Java applications to include NativeScript with Angular components. This is incredibly useful if you’ve got a dedicated Android team, but could use some help from your web developers. It saves you the trouble of having to port everything to NativeScript.

Much of this tutorial was inspired by the NativeScript documentation, but it included topics such as Volley and NativeScript with HTTP as seen in previous tutorials that I wrote about.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

  • Brandon S. Brandon

    After I complete the step: to modify the gradle (app) file….adding the following dependencies {

    compile ‘com.android.volley:volley:1.0.0’


    I sync the projects dependencies. Then receive the Android Studio error massage:

    Error:(8, 24) No resource found that matches the given name (at ‘title’ with value ‘@string/action_stocks’).

    I can end the error by changing the 8th line MenuMain.xml from:




    But, I’m bringing this to your attention so that you can help me if this is not the best approach. I considered ignoring the error but I reached a wall when I tried to compile it.

  • Brandon S. Brandon

    After I complete the step: to modify the gradle (app) file….adding the following dependencies {

    compile ‘com.android.volley:volley:1.0.0’


    I sync the projects dependencies. Then receive the Android Studio error massage:

    Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies]
    Error:(8, 24) No resource found that matches the given name (at ‘title’ with value ‘@string/action_stocks’).
    Error:(8, 24) No resource found that matches the given name (at ‘title’ with value ‘@string/action_stocks’).
    Error:Execution failed for task ‘:app:processDebugResources’.

    com.android.ide.common.process.ProcessException: Failed to execute aapt
    Information:BUILD FAILED
    Information:Total time: 4.011 secs
    Information:3 errors
    Information:0 warnings
    Information:See complete output in console

    I can end the error by changing the 8th line MenuMain.xml from:




    But, I’m bringing this to your attention so that you can help me if this is not the best approach. I considered ignoring the error but I reached a wall when I tried to compile it.

    • Good catch.

      There are two ways to solve this. You can convert the variable into a constant like you’ve done, or you can open the strings.xml file in the values directory.

      For cleanliness it is better to use variables.