Introduction to NativeScript: Building Native Apps with JavaScript
— March 11, 2015
NativeScript is a framework that allows developers to create native mobile apps for Android, iOS, and Windows Phone using JavaScript. Unlike other frameworks such as PhoneGap or Cordova, which enable web apps to run like native apps, NativeScript provides a direct approach to native app development. It does this by allowing developers to access the native APIs of each platform, ensuring that the apps created with it are fully native and not just web-based applications masquerading as native ones.
NativeScript provides a way to work with the native capabilities of Android, iOS, and Windows Phone by leveraging standard JavaScript with some enhancements. It enables developers to use familiar JavaScript without being bound to any particular platform or technology. For those familiar with JavaScript, this creates a significant advantage in terms of ease of use and learning curve, particularly in outsourcing and mobile app development. NativeScript can be a powerful tool for mobile application development, offering the potential for developers to build high-quality apps while still leveraging their existing JavaScript skills.
How NativeScript Works
At its core, NativeScript extends JavaScript with a middleware layer that allows the code to interact directly with the native APIs of the mobile platforms. This is different from many other mobile development frameworks that use HTML and CSS to create the user interface and wrap it in a webview. In NativeScript, the JavaScript code runs directly on the device and interacts with the native components, ensuring that the app behaves like a true native application.
One of the key features of NativeScript is its ability to call native functions in Android and iOS directly from JavaScript. For example, when working with Android, you can use a simple command like var file = new java.io.File(path);
, which will call the Java API for working with files. The NativeScript system automatically converts the JavaScript string path
into a java.lang.String
object, making the interaction seamless. Essentially, NativeScript acts as a bridge between the JavaScript code and the native code, allowing JavaScript developers to work directly with native APIs without needing to write platform-specific code.
This approach eliminates the need for complex web views or wrappers, which are typically used by other frameworks. In NativeScript, if you need a button or any other user interface element, you would write JavaScript code that interacts with the native UI components. This ensures that the app is built from native components, not HTML, and behaves exactly like a native app would on each platform.
NativeScript Modules
One of the concerns when developing for multiple platforms is the need to write separate code for each platform. For example, Android uses Java, iOS uses Objective-C or Swift, and Windows Phone uses C#. This means that developers often need to write three versions of the same app, which can be time-consuming and prone to errors. NativeScript addresses this by introducing an abstraction layer through its NativeScript Modules.
The NativeScript Modules layer provides a set of abstractions that map JavaScript code to the correct native API calls for each platform. For example, instead of writing new java.io.File(path)
for Android, you can use a simpler command like var file = File(path);
. The NativeScript system automatically determines which platform is being used and converts the JavaScript code into the appropriate native call. This abstraction layer helps streamline the development process and ensures that developers can write code that works across multiple platforms without needing to write separate implementations for each one.
However, it is important to note that abstraction layers can have limitations. It is difficult to create a perfect abstraction that handles all native features of each platform. In some cases, developers may need to fall back on direct API calls to access features that are not covered by the abstraction layer. Fortunately, NativeScript provides this flexibility, allowing developers to directly interact with the native APIs when needed.
Performance Considerations: The 10% Overhead
One of the common concerns when using a framework like NativeScript is the performance impact of using an abstraction layer. The overhead of translating JavaScript calls into native code can result in slower performance, especially for complex applications. However, NativeScript claims that the performance overhead is minimal, with only about a 10% decrease in speed compared to writing native code directly. This is a reasonable trade-off for the benefits of cross-platform development and the ability to use a single codebase for multiple platforms.
This performance overhead is especially relevant in mobile app development, where performance is often a key concern. However, NativeScript’s efficiency means that the performance impact is unlikely to be significant enough to affect most apps. For example, many mobile apps can run smoothly even with a small performance penalty, making NativeScript a viable option for developers who want to create native apps using JavaScript.
For mobile app development companies in Vietnam, this performance trade-off is crucial. Many companies rely on frameworks like NativeScript to build efficient apps while also reducing development time and costs. By using a single codebase for multiple platforms, developers can significantly speed up the development process and reduce maintenance efforts.
NativeScript and Node.js: A Powerful Combination
NativeScript is built on top of Node.js, a popular JavaScript runtime that is widely used in server-side development. This makes it easier for developers who are familiar with Node.js to transition into mobile app development. NativeScript inherits many of the strengths of Node.js, such as its non-blocking, event-driven architecture, which helps ensure that mobile apps built with NativeScript are responsive and performant.
However, it is important to note that while NativeScript provides a high level of abstraction, developers still need to install parts of the native SDK for each platform. For example, developers need to install XCode for iOS development, Android Studio for Android development, and the appropriate tools for Windows Phone. This means that while NativeScript simplifies the development process, it does not eliminate the need for platform-specific tools and knowledge. Developers still need to be familiar with the native SDKs for each platform in order to fully utilize the capabilities of NativeScript.
This requirement can be a challenge for developers who are new to mobile app development or who are unfamiliar with the native SDKs. However, for experienced JavaScript developers, the ability to use NativeScript with Node.js provides a streamlined path to mobile app development without having to learn multiple programming languages.
NativeScript’s Open Source Nature
NativeScript was developed by Telerik, a company known for its JavaScript-based mobile development tools. Despite its origins in Telerik, NativeScript is open source under the Apache 2.0 license. This means that developers can freely use, modify, and distribute NativeScript without any licensing restrictions. The open-source nature of NativeScript has led to a growing community of developers who contribute to its development and help improve the framework over time.
Telerik also offers additional services, such as the Telerik Platform, which provides cloud-based tools and services for mobile app development. While NativeScript itself is free and open-source, Telerik hopes to generate revenue by offering these additional services to developers who need extra features or support. This business model is similar to that of other open-source projects, where the core framework is free, but developers can pay for premium features or support if they need it.
For developers and companies in the US looking to outsource mobile app development, NativeScript provides a powerful, open-source solution that can be adapted to a wide range of projects. The growing community and the potential for additional services make it an attractive option for businesses looking to build high-quality mobile apps with a relatively low cost of entry.
Conclusion
NativeScript offers a promising solution for JavaScript developers who want to create native mobile apps for Android, iOS, and Windows Phone. By using standard JavaScript and providing a set of abstractions that simplify cross-platform development, NativeScript makes it easier to create high-quality, native apps without having to write platform-specific code. While there may be some performance overhead, the 10% decrease in speed is generally not significant enough to be a concern for most applications.
For mobile app developers, NativeScript offers a powerful tool for creating native apps while leveraging existing JavaScript skills. With its open-source nature and strong community support, NativeScript is a framework worth considering for businesses looking to streamline their mobile app development process.