MASARYK UNIVERSITY FACULTY OF INFORMATICS Developing a mobile application using multiplatform frameworks with a native output BACHELOR'S THESIS Filip Drgon Brno, Fall 2017 MASARYK UNIVERSITY FACULTY OF INFORMATICS Developing a mobile application using multiplatform frameworks with a native output BACHELOR'S THESIS Filip Drgon Brno, Fall 2017 This is where a copy of the official signed thesis assignment and a copy of the Statement ofan Author is located in the printed version of the document. Declaration Hereby I declare that this paper is my original authorial work, which I have worked out on my own. A l l sources, references, and literature used or excerpted during elaboration of this work are properly cited and listed in complete reference to the due source. Filip Drgoň Advisor: RNDr. Jaroslav Škrabálek i Acknowledgement I would like to thank the supervisor of my bachelor's thesis, RNDr. Jaroslav Skrabalek, for his helpful attitude, valuable insights, and his contribution to my thesis. iii Abstract The objective of this bachelor's thesis is to research current available options in multiplatform application development, evaluate their qualities and shortcomings and present React Native as the currently best available solution. iv Keywords Multiplatform application, React Native, Android, iOS, Javascript, Xamarin, NativeScript, Native application,... v Contents 1 Introduction 1 1.1 Thesis structure 2 2 A mobile application 3 2.1 Defining a mobile application 3 2.2 Basic requirements of a mobile application 3 2.2.1 User interface requirements 3 2.2.2 Cost-related requirements 4 2.2.3 Further requirements 6 2.3 Mobile application development types 7 2.3.1 Native development 8 2.3.2 Web development 9 2.3.3 Hybrid development 10 2.3.4 Comparison of different methods 11 3 A multiplatform approach 13 3.1 Multiplatform development types 13 3.2 A native multiplatform application 14 3.2.1 Justifying the native multiplatform approach . . 14 3.3 Most notable multiplatform frameworks 14 3.3.1 Xamarin 14 3.3.2 Ionic 2 16 3.3.3 NativeScript 17 3.3.4 React Native 18 3.4 A comparison 20 3.4.1 Developer's perspective 20 3.4.2 Pricing perspective 22 3.4.3 User's perspective 23 3.4.4 Conclusion 24 4 React Native - an in-depth look 25 4.1 JavaScript 25 4.1.1 About JavaScript 25 4.1.2 A short look on history 26 4.1.3 Advantages & shortcomings of JavaScript . . . . 28 4.1.4 Transpilers 30 vii 4.2 React 30 4.2.1 JSX 32 4.3 Under React Native's hood 34 5 A demonstrative application 37 5.1 Motivation & problem definition 37 5.2 Analysis & structure design 38 5.3 Implementation 41 5.3.1 Implementation structure 43 5.3.2 User scenarios 45 5.4 Results & What's next 47 6 Conclusion 49 Bibliography 51 viii List of Tables 2.1 Comparison of the application development methods 12 3.1 Comparing the frameworks from a developer's perspective 21 3.2 Comparing the frameworks from a pricing perspective 22 3.3 Comparing the frameworks from a user's perspective 23 ix List of Figures 2.1 Average application price on iOS store, June 2017[3] 5 2.2 Free and paid application distribution on iOS and Android, January 2017[4]. 6 2.3 Architectures of native, hybrid and web applications[5]. 8 3.1 Comparing traditional Xamarin and Xamarin.forms [ 9 ] 1 5 3.2 A n example of TypeScript's strong type enforcing 18 3.3 A schema of Hot Module Reloading in React Native 19 4.1 Netscape's advocacy on JavaScript's genesis[26]. 27 4.2 A schema depicting the sources behind JavaScript's features[28]. 27 4.3 A simplified schema of Read's DOM Reconciliation[33] 31 4.4 A schema of the threading system React Native implements. 34 5.1 A schema of the proposed pricing regions for visitor parking in Brno[36]. 38 5.2 A proposed use case diagram of the resulting application. 39 5.3 A proposed deployment diagram of the resulting application. 40 5.4 A schema of the navigation structure in the resulting application 41 5.5 A screenshot depicting the project structure of the application. 44 5.6 Photographs of the sign in and sign up screens. 45 5.7 Photographs of the map screen and the modal for ticket purchase confirmation. 46 5.8 Screenshots of the settings screen, and the billing screen. 47 xi 1 Introduction Nowadays, smart devices are seeing a steady increase in market share worldwide, having surpassed desktop share only recently (in midOctober 2016) [1]. With this in mind, it is crucial to optimize services heavily both for desktop and smart device usage, as not doing so results in heavy repercussions [2]. While developing an application aimed at the public, it is important to consider developing multiple access points for the application, such as a web interface and native applications specifically for the most popular mobile platforms (namely Google Android and Apple iOS). Focusing on services targeted at mobile devices, these are mainly distributed utilizing either responsive web applications, or native mobile applications. To minimize the expenses and effort required for developing a native application, utilizing multiplatform development comes into play as a valid choice. The goal of this thesis is to present the current options in developing an application utilizing multiplatform development, specifically focusing on tools for developing native applications, for both major platforms, seamlessly and concurrently. The thesis takes into evaluation the reusability of code blocks and components for ease-of-use and comfortable development process, resulting in faster, cheaper and easier development process. 1 i . INTRODUCTION 1.1 Thesis structure In its first part, the thesis focuses on defining a mobile application & its basic requirements, and comparing different approaches to mobile application development, including their advantages and detrimental effects. In the second part, the thesis aims at establishing a definition of multiplatform mobile application development. Furthermore, the thesis discusses the multiplatform development types & multiplatform framework types. In the end, a profound comparison of several multiplatform frameworks is made, concluding which one would be best suited for modern multiplatform development. In the next part, the focus of the thesis is to provide an in-depth look on React Native and the technologies utilized with React Native. The thesis further discusses in detail their functionality, advantages, shortcomings and the reasoning behind choosing the technologies for the practical application. Finally, the thesis presents a demonstrative application written in React Native, with the motivation & problem definition, an analysis with a structure design, and an implementation showcase with photographs and use cases of the application. A n evaluation of the development process is made, with a discussion on possible directions the resulting application could evolve into. 2 2 A mobile application This chapter is focused on defining the term mobile application and discussing the basic needs for developing a mobile application. It will also introduce the different approaches in mobile application development, and the conveniences and imperfections of each development approach. 2.1 Defining a mobile application The term mobile application is understood to be a type of executable software designed to run on a mobile device, e.g. a smartphone, or a tablet. Mobile applications were generally considered to be small, individual software units with limited functionality. Nowadays, mobile applications are functionally-wise comparative to applications for personal computers. Unlike applications for personal computers, mobile applications are typically obtained from a corresponding application store. 2.2 Basic requirements of a mobile application Depending on the use case, an application's requirements may differ heavily, with the difference mainly being the required system resources & the device's special functions. 2.2.1 User interface requirements A basic requirement for a mobile application is to be easily usable. A n average PC is usually controlled using a keyboard and a mouse, in comparison to an average smart device, which is typically controlled using solely a touch screen spanning only several inches. A n overwhelming majority of these devices is controlled using a finger, with the - formerly popular - stylus now being a disfavoured option. The limited screen size of a smart device is the main reason behind the user interface requirements - the control elements need to be sufficiently sized, allow for easy usage on a small-diameter device, and be reasonably placed. This would mean that the application's user 3 2. A MOBILE APPLICATION interface needs to conform to the platform specific Human Interface Guidelines1 , using native components wherever possible - for example, a native touch keyboard, native icon set or a native menu. The reasoning behind this requirement is that users are generally already used to using these components, so if they are identical across several applications, the usage of the application feels less confusing. 2.2.2 Cost-related requirements A mobile application developer also has to consider the economic side of developing a mobile application. Financially-wise, mobile application development has a few characteristics divergent from web or personal computer development. Beneficial to the developer, several platforms limit the ways an application can be distributed - iOS or Windows Phone commonly allow installing an application only through the official store of the platform's manufacturer2 . In effect, it is much harder to obtain an illegal copy of an application as compared to a personal computer. On the other hand, it is typical to pay considerably more for a personal computer application than for a mobile application. This requires the developer to either be capable of selling a great amount of copies3 , or use a different monetization method.. The figure 2.1 shows the average prices of an application in the Apple A p p Store on June 2017. The source[3] mentions the average price to be approximately $1.02. 1. Called User Interface Guidelines on Android 2. There are ways to surpass these limitations, although rarely used by an average user. 3. To be able to pay for the application development and upkeep costs. 4 2. A MOBILE APPLICATION Average prices for apps in the Apple App Store as of July 2017 (in U.S. dollars) 1.25 1.02 Figure 2.1: Average application price on iOS store, June 2017[3] Mobile applications also affect the user's view on software financing. While the model of a one-time purchase of an application is still prevailing on personal computers, mobile applications tend to be either very cheap, or free, with financing being solved typically by a freemium model. This includes in-app purchases, or a subscription model. This trend is shown in both figure 2.1 and figure 2.2. The former shows the cheap average pricing of mobile applications, while the latter displays the trend of developers releasing either free or very cheap applications. 5 2. A MOBILE APPLICATION Distribution Free vs. PaidApps (i OSvs. Android) Figure 2.2: Free and paid application distribution on iOS and Android, January 2017[4]. 2.2.3 Further requirements It is important for the mobile application to be fluid and smooth, so the user experience is comfortable. On personal computers, the users tend to be used to the response times of the applications, whereas on mobile devices, most users expect a fluid, quick and stable experience. Hence a mobile application has to react without delay to the user's input, and cannot, in any way, affect other functions of the device (e.g. an application blocks the user from accepting a phone call). A mobile device is typically powered by a battery with a limited capacity. Therefore an application running on the device should aim to use the least amount of battery possible - for example by using system resources only when absolutely necessary, avoiding unneces- 6 2. A MOBILE APPLICATION sary operations and calculations, or keeping a network connection for longer than necessary. A n application has to be able to respond intelligently to a myriad of outside effects, like a battery discharge, a device turn-off, a force-quit, unstable data connection or inaccurate data from a GPS sensor. This would mean - in the case of the first three mentioned - that the user does not lose any data, or is not denied entry after a later re-launch of the application. 2.3 Mobile application development types While developing a mobile application, it is good practice to split the resulting application into smaller partitions. Initially, the main two partitions are: » Back-End (server part) - typically implements an API (Application Programming Interface) for easier access towards server services, e.g. editing and accessing data, authentication, or complex operations unfit to be performed on the client's device. » Front-End (client part) - typically presents a set of User Interfaces, which implement the services provided by the server, through its API. This part is usually specific to each particular client device type, as opposed to the server part. It is important to mention that a mobile application does not necessarily have to have a back-end service, e.g. a simple game application would have no use of a server. The main focus of this thesis is on the Front-End part of the application, particularly on how to develop one using multiplatform development. The Front-End segment is considered as either a web interface, or a mobile application for Android & iOS, while the BackEnd segment is perceived as an already finished product, providing an API for the Front-End segment to utilize. While developing the web interface, it is good practice to choose a development method to follow prior to starting the mobile application development. Currently, there are three different approaches in mobile application development - web, native, and hybrid development. 7 2. A MOBILE APPLICATION Mobile App Technology Stacks Your App 4, t Operating System & Device Your App Embeddedl Native I H 3 " f „ Web Views I Ul Views I ^ o t k Native Wrapper App 4- t Operating System & Device Your App 4- t Web Browser J, ? Operating System & Device Native App "Hybrid" App Web App Figure 2.3: Architectures of native, hybrid and web applications[5]. 2.3.1 Native development Developing an application natively is recognized as developing a separate application for each platform independently. While natively developing an application, a developer is forced to use the tools required for the specific platform, e.g. a specific programming language (Java, Kotlin, Swift,...), or a specific development environment (like Xcode). Advantages » Optimized User Interface - The User Interface is optimized for each platform, according to its specific design guidelines. » High fluidity and responsiveness - The application is substantially more fluid and responsive than a web application, which contributes positively towards user experience. » No Internet connection required - While only applying to some applications, native applications by default do not enforce a necessary access to the Internet while in use. 8 2. A MOBILE APPLICATION Drawbacks » Separate development - Native applications require separate development for each platform. » Costly development - By having to maintain several implementations across all supported platforms, the development and maintenance costs suffer. » Complicated maintaining - Being typically deployed on an application store, the developer cannot confide in the user updating after every update, which makes maintaining older versions a necessity. Although the developer could force the user to update by denying access to the application until the update is finished, this forced approach is not very liked among users. 2.3.2 Web development The mobile application is constituted of a web page, optimized for being used both on touch-enabled mobile devices and desktop computers. The application adapts according to the device it is accessed by, and it is accessed solely by the web browser of the device. This development approach usually utilizes HTML, CSS and JavaScript, and is also labeled as responsive web design. Advantages » Single application for all platforms - Developing a single web application to meet all the required conditions, instead of several, similar, versions across all platforms. » Lower development costs - Since there is no need to develop a specific application for each platform, the development cost is lowered significantly. » Simple deployment and maintaining - It is necessary to maintain only one source code. Updating the application is performed by deploying the new application on the server, which is arguably much simpler, and economical, than having to distribute the application on several platforms. The update is instantaneous and without user interaction. 9 2. A MOBILE APPLICATION Drawbacks » User Interface is unoptimized - The user interface differs considerably on iOS and on Android, which is usually unaccounted for in web applications. » Lower fluidity and speed - A web application cannot utilize the full potential of the device, possibly resulting in a mediocre user experience, with fluidity and performance issues. » Required Internet connection - To connect to a web application, an Internet connection is required. 2.3.3 Hybrid development Hybrid applications are considered as a meeting point between native and web development approaches. They are a combination of these two methods, focused on solving the shortcomings presented by both aforementioned techniques. From the end user's point of view, the application acts identically to a native application, meaning that it is available on an application store, installs itself in the device and updates through the application store. Looking at the application from the developer's point of view, the application is developed using web technologies - HTML, CSS and JavaScript, while being contained in a native wrapper. On application start-up, the wrapper creates a WebView - a full-screen minimal web browser, and loads the web application inside the browser. This approach allows re-use of source code across different applications and platforms, reducing development costs. While developing hybridly, it is still necessary to develop an application separately for each targeted platform. Facilitated by reusing the majority of code base across all platforms, the application still requires to be maintained on several platforms at once. To help with simultaneous development on multiple platforms at once, several tools, such as Marmalade or Phonegap, provide help with developing and distributing the resulting application. 10 2. A MOBILE APPLICATION Advantages » Platform-specific UI - A simple UI is adaptable depending on the platform the application is deployed on (through the native wrapper) » Code reuse - Code reuse helps with smoother and faster development process » Lower development costs - By reusing a majority of the code base, the development costs are smaller in comparison to native development. Drawbacks » Lower fluidity and speed - A hybrid application cannot utilize the full potential of the device, typically resulting in a deteriorated user experience, with fluidity and performance issues. » Complicated maintaining and deployment - The application needs to be deployed on platform-specific application stores, which complicates the updating and deploying processes. » Complicated debugging - By having to use varying WebViews for each platform, debugging the application might prove problematic. Their implementations differ heavily platform-to-platform, and version-to-version4 . » Potentially higher energy consumption - Not being able to micro-optimize the application on low level could potentially lead to unwanted battery drainage. 2.3.4 Comparison of different methods There are several main traits shared by all methods of application development. The table 2.1 provides a concise comparison of how well the methods succeeded at each specific point. 4. A possible workaround is using Cordova Crosswalk, which implements an identical WebView on every platform, but increases the application size by approximately 50MB. 11 2. A MOBILE APPLICATION Features Native Web Hybrid User Interface Great Average Good User Experience Great Average Average Development Time High Low Average Costs High Low Average Deployment Complicated Simple Complicated Maintaining Varies Varies Varies Off-line Mode Yes No Yes* Off-line mode support varies framework-to-framework Table 2.1: Comparison of the application development methods Before summarizing the table's results, it is necessary to reason why maintaining is said to vary on each singular platform. It is very difficult to specify the complexity of application maintaining based singularly on the development approach; a well-designed native application could be considerably easier to maintain than a web or a hybrid application. O n the other hand, a poorly thought-out native application would suffer from complicated maintaining procedure in comparison to a hybrid or a web application. To summarize the table, it is evident that native approach's main strength comes from a great user interface and user experience, at the cost of higher development costs. Each development approach serves a different purpose, and outshines the others in specific situations. 12 3 A multiplatform approach This chapter focuses on justifying a multiplatform approach to mobile application development. First, a distinction between similar multiplatform approaches is made. Following, the chapter suggests several popular frameworks for this type of mobile application development. Finally, a profound comparison is made, concluded with deciding which framework will be used for the practical section of the thesis. The term multiplatform application is understood as an application containing multiple entry points across different platforms. A platform could be, for example, a specific operating system of a mobile or desktop device, or a web interface. In other words, a multiplatform application is an application available from all the targeted platforms with only minor modifications[6]. 3.1 Multiplatform development types We essentially distinguish two multiplatform development approaches; one being a hybrid multiplatform approach, while the other is a native multiplatform approach. A hybrid multiplatform approach is basically the hybrid approach mentioned in the previous chapter. This approach is utilized, for example, by Apache Cordova/Phonegap. On the other hand, a native multiplatform approach differs from the hybrid in that the resulting application is a fully native application, utilizing both native API calls to the device's functionalities and native components. It is important to mention that results of various native multiplatform frameworks diverge heavily in their "nativity". This approach is utilized, for example, by Xamarin, or React Native. 13 3. A MULTIPLATFORM APPROACH 3.2 A native multiplatform application A native multiplatform application is an application that is not implemented in the platform's native language, rather using another programming language that is generally unsupported by the platform [7]. It is common that a development environment handling the native application bundling is provided by a third party. The resulting product is an application using native API, with near-native performance. 3.2.1 Justifying the native multiplatform approach A native multiplatform development approach shares most advantages with hybrid development approach, but also expands on these advantages by adding some of its own. Primarily, native multiplatform approach solves the biggest issue of hybrid applications, the lowered fluidity and responsiveness, it also resolves the WebView issue of either being forced to rely on a good WebView implementation, or use a plugin like Cordova Crosswalk for a separate implementation - increasing size in return. Native multiplatform applications avoid the WebView altogether, instead relying on the usage of only native components. 3.3 Most notable multiplatform frameworks The ensuing frameworks were picked mainly for their overall popularity, and for their well-executed approaches. 3.3.1 Xamarin Xamarin is a set of tools for application development, created by the Xamarin company. It is based on Mono, which is an open source project implementing several tools used in .NET for multiplatform development. Because of its Mono roots, Xamarin utilizes C# as its main language. Although the Xamarin toolbox includes tools aimed at specific platforms, like the Xamarin.iOS orXamarin.Android, the main spotlight will be aimed at Xamarin.forms. While tools for specific platforms like Xamarin.iOS usually achieve around 70% of code sharing, Xamarinforms is said to be able to achieve up to 100% shared code base[8]. 14 3- A MULTIPLATFORM APPROACH A simple comparison of Xamarin's different tools is shown in figure 3.1. Traditional Xamarin approach * • • With Xamarin.Forms: more code-sharing, native controls Figure 3.1: Comparing traditional Xamarin and Xamarin.forms [9] In the past, Xamarin utilized its own IDE1 , Xamarin Studio for application development and deployment. It also had a limited starter edition, which allowed full usage up to a certain size of compiled code, which limited the developer's capabilities. The starter edition also did not allow usage of Xamarin.Forms. Recently, Xamarin has been acquired by Microsoft Corp., resulting in several major changes. Biggest changes would be the full integration of Xamarin tools with Visual Studio, and the removal of some starter edition limitations, instead making Xamarin open-source[10]. Among the few services still only available to premium members is Xamarin Test Cloud, a cloud service which allows the developer to perform automated tests on thousands of various devices[ll]. Xamarin Test Cloud's pricing model starts at $99 per month with one simultaneous device and one device hour per day, with growth in both as the pricing increases. A Xamarin application is compiled into a native binary, instead of being interpreted. O n Android, Xamarin ships the Mono runtime bundled with the application, resulting in JIT2 compilation typical for 1. Integrated Development Environment 2. Just In Time 15 3. A MULTIPLATFORM APPROACH Android applications. On iOS, Mono is used again, but instead compiles the application AOT3 into a native executable [12]. 3.3.2 Ionic 2 Ionic is an open-source framework created by Drifty Co. for mobile application development using only HTML5, CSS and JavaScript. With over 32,000 stars on GitHub[13], a vibrant, active community and well-made documentation with clear examples[14], Ionic is being called the most popular hybrid mobile development framework. Ionic's power comes from utilizing the Angular framework, the Apache Cordova framework, and SASS. Angular is an open-source JavaScript framework, sponsored and maintained by Google LLC, that utilizes the M V C 4 architecture model. Angular's purpose was to simplify development and testing of web applications, using the M V C architectural model. Angular extends the basic H T M L syntax by adding custom attributes, classes and elements. These customized markers, called Directives, result in Angular's compiler attaching custom behaviours to the relevant D O M 5 nodes. Apache Cordova is an open-source JavaScript mobile application development framework, based on the original PhoneGap. After Adobe's acquisition of the original PhoneGap[15], they have donated PhoneGap to The Apache Software Foundation under the name Cordova. Currently, Adobe is developing its PhoneGap ecosystem, which is based on the Cordova engine, but implements additional functionality. Cordova implements a set of APIs that create a bridge from JavaScript to native code, used for accessing the smart-phone's native resources directly. Cordova has a set of pre-made plugins for accessing - for example - the microphone or the accelerometer by default. The out-of-the-box available APIs are consistent across several platforms, including Android, iOS, Windows Phone, and even Blackberry. 3. Ahead of Time 4. Model-View-Controller - https://msdn.microsoft.com/en-us/library/ ff649643.aspx 5. Document Object Model - A tree-like structure of all H T M L elements of a web-page 16 3. A MULTIPLATFORM APPROACH SASS (Syntactically Awesome Style Sheets) is a CSS pre-processor that extends CSS's features. SASS, popular because of its simple syntax and powerful features, compiles SASS (. scss) files into CSS files. Among its major innovations are things like importing & extending of other .scss files, HTML-like nesting of CSS selectors, variables, mixins6 , and math operators. Recently, Drifty Co. has released a newer version of Ionic, labeled Ionic 2, to mark the transition from the Angular framework to its newest iteration, Angular 2+. The transition resulted in a major performance increase and code complexity decrease. To conclude, Ionic implements the Angular framework for its powerful approach to creating web applications, the Cordova framework for its powerful native APIs, and the SASS pre-processor for its many quality-of-life additions to basic CSS. 3.3.3 NativeScript NativeScript is an open-source, native multiplatform framework. It is centered around utilizing JavaScript, CSS and XML, with an option to use the Angular framework already mentioned above. It was created and is currently supported by Telerik AD. NativeScript allows the developer to create native Android & iOS applications, with Windows UWP support being planned. The resulting application has native-like performance and user experience[16]. A major feature of NativeScript is the native API reflection. Instead of using a separate bridging layer between NativeScript and specific mobile platforms, NativeScript uses reflection to build up a mapping of the entire ecosystem[17]. Using this reflection, NativeScript is able to invoke native code easily - for example - instantiating a Java object and calling its methods (seen in listing 3.1) [18]. l e t t i m e = new a n d r o i d . t e x t . f o r m a t . T i m e ( ) ; t i m e . s e t ( 1 , 0 , 2 0 1 5 ) ; c o n s o l e . l o g ( t i m e . f o r m a t ( " % D " ) ) ; Listing 3.1: A n example of NativeScript's reflected API usage. 6. Reusable blocks of style, used, for example, to assert all vendor prefixes 17 3. A MULTIPLATFORM APPROACH Declaring a user interface in NativeScript is usually done using X M L files, which the NativeScript compiler then uses to call upon native UI elements for each specific platform. NativeScript supports utilizing either pure JavaScript, or using TypeScript with the Angular framework. TypeScript is an open-source superset7 of the JavaScript language developed by Microsoft Corp., providing many additional features to the basic language. Among these features are interfaces, compiletime type-checking (as seen in figure 3.2), generic types, and type annotations - JavaScript is loosely typed by default. TypeScript has gained favor amongst developers used to working with high level Object Oriented Languages, mainly because of strong type enforcing, interfaces & and many other object-oriented quality-of-life additions. c o n s t add = ( a d d l : number, add2: number): number => { r e t u r n a d d l + add2; }; c o n s o l e . l o q ( a d d ( l , " t w o " ) ) ; | TS2345:Argument of type "two"' is not assignable to parameter of type 'number'. Figure 3.2: A n example of TypeScript'% strong type enforcing 3.3.4 React Native React Native is an open-source, multiplatform framework for building native applications with JavaScript & React. It has been created and is supported by Facebook, Inc. React Native allows the developer to produce native Android & iOS applications, with an extension enabling development for Windows UWP8 . React Native utilizes its own specific language - JSX - an XML-like syntax extension to ECMAScript without any defined semantics[19]. It is typically transpiled9 into JavaScript 7. Any JavaScript code is also compilable in TypeScript 8. https://github.com/Microsoft/react-native-windows 9. Transformed into another language with a similar level of abstraction 18 3- A MULTIPLATFORM APPROACH using Babel10 . With JSX and React Native, the developer uses declarative UI components which compile into native elements for each specific platform. One of React Native's prominent features is Hot Reloading - a way to reload the application quickly1 1 while retaining the application's state. This obviously only benefits the developers, by allowing them much faster interaction with the developed application, which in turn results in a faster production cycle. Previously, Live Reload has been used in React Native, which allowed similarly quick application reloading, but did not retain application state. This could be an irritation while developing a - for example - feature present only multiple screens away from the application's entry point, or while tweaking minor details in UI design. Instead, Hot Reloading injects new versions of the edited files into the running application. A simple schema of the concept of Hot Reloading is displayed in figure 3.3. MODULE SYSTEM , PACKAGER ;'• Patches module system WATCHMAN Notifies file change HMR PLUGIN Sends IIMR upca:e HMR RUNTIME Figure 3.3: A schema of Hot Module Reloading in React Native Directly tied to the Hot Reloading feature is React Native's easy error handling & debugging. On a non-standard action occurrence within the application, developer is promptly - thanks to the Hot Reloading feature) - provided with an error screen displayed as a full screen report with both the error message and the relevant stack trace. Accessing 10. https://babeljs.io/ 11. Under 1 second[20]. 19 3. A MULTIPLATFORM APPROACH these JavaScript logs is generally possible by using the react-native log-android or react-native log-ios commands, respectively on Android and iOS. Another option would be utilizing a web browser that supports Chrome Developer Tools. First, the developer enables Remote JavaScript debugging in the application, which in turn allows for connecting to the application through the web browser. In the browser, we can view the application's logs and use all standard debugging features native to web development. These features include viewing current values of variables, utilizing break-points to pause the application at critical points, or stepping through a method's execution line-by-line. It is also important to mention that React Native boasts a very large, ever-growing, community compared to the other mentioned frameworks. This results in more contributors, more relevant questions with proper answers on Stack Overflow12 , and more open-source libraries to simplify the development process. 3.4 A comparison To better differentiate between separate features of the aforementioned frameworks, a division of these features into several sections is necessary. The attributes are split based on viewpoints to help discern between their specific qualities. These viewpoints are, namely, the developer's viewpoint, the pricing viewpoint, and the user's viewpoint. 3.4.1 Developer's perspective Before presenting a concise comparison of the developer's perspective in the form of a table, it is critical to first establish the evaluated points and their meanings. » Ease-of-Use conveys the overhead necessary before, and after, the actual development. This includes things like IDE setup, project setup, application building & application (re)deployment. 12. https://stackoverflow.com/ 20 3- A MULTIPLATFORM APPROACH » Quality-of-Life signifies the improvements and advantages the specific framework has developer-wise, as compared to other frameworks. This could mean the specific programming approach, the difficulty of integration with native components or applications, or various improvements that reduce downtime and speed up the developer's work - like Hot Reload in React Native, or Live Reload in Ionic 2. » Community indicates the overall size of the community around the framework. The community size would be directly proportional to things like the volume of third-party libraries, the number of related questions (and solutions) on websites like Stack Overflow, or the momentum of the framework. » Extras represent any extra features the framework has compared to the others, be it Xamarin's Test Cloud feature, or any other feature that facilitates the development cycle. Framework Ease-Of- Use Quality-of- Life Community Extras Xamarin Difficult Medium Large Test Cloud Ionic 2 Easy Medium Medium Codepusha NativeScript Medium Low Medium Sidekickb React Easy Native High Large Codepush a h t t p s : / / m i c r o s o f t . g i t h u b . i o / c o d e - p u s h / b h t t p s : / / w w w . n a t i v e s c r i p t . o r g / n a t i v e s c r i p t - s i d e k i c k Table 3.1: Comparing the frameworks from a developer's perspective 21 3- A MULTIPLATFORM APPROACH 3.4.2 Pricing perspective Before presenting a concise comparison of the pricing perspective in the form of a table, it is critical to first establish the evaluated points and their meanings. » Developing Costs denote the relative development costs, which are affected by the framework's specific approach to multiplatform development, or by the re-usability of code base across multiple platforms. » Framework Pricing hint at whether the framework is paid or free. » Deployment Costs symbolize the costs incurred by deploying the application on several platforms, updating the application, and application maintaining. Although the costs of deployment should generally be framework agnostic, the difference comes mostly from the costs caused by updating & maintaining the application. Framework Developing Costs Framework Pricing Deployment Costs Xamarin Medium Free* High Ionic 2 Low Free Low NativeScript Low Free* High React Native Medium Free Low With paid modules. Table 3.2: Comparing the frameworks from a pricing perspective 22 3- A MULTIPLATFORM APPROACH 3.4.3 User's perspective Before presenting a concise comparison of the user's perspective in the form of a table, it is critical to first establish the evaluated points and their meanings. » Performance represents how well the framework performs computation-heavy or animation-heavy operations. The main metric here would be the frame rate of the resulting application whilst performing complicated operations. » Nativity denotes how well the framework's output application complies to the set of features and design approaches specific to every platform. » Size indicates the relative size of the application, compared to a native application. The most important factor here would be how small the necessary framework-specific code is. Framework Performance Nativity Size Xamarin High High Big Ionic 2 Low Low Medium NativeScript Medium High Big React Native High High Medium Table 3.3: Comparing the frameworks from a user's perspective 23 3. A MULTIPLATFORM APPROACH 3.4.4 Conclusion Concerning the developer's perspective, React Native comes out as a winner, mainly due to its simple setup1 3 , well-made quality-of-life additions (like Hot Reload), and arguably the biggest community out of the presented frameworks. It's important to note that while Xamarin's Test Cloud feature does help with testing the application heavily, the Codepush feature usable both by Ionic 2 and React Native simplifies the deployment of updates. Both of these features help the developer with very different problems, hence they are difficult to compare. Regarding the pricing perspective of the framework, Ionic 2 comes out ahead, with its low development costs (caused by a virtually 100% shared code-base), free framework pricing and low deployment costs (mostly affected by Codepush). A close follow-up would be both React Native and NativeScript, with the former having less shared code, but simpler deployment and the latter having a larger shared code-base, but more costly deploying of updates. It's important to note that discussing shared code-base is specific on an application-to-application basis. A l l of these frameworks are capable to achieve an almost-100% shared code-base, in an ideal situation. As to the user's perspective on the framework, React Native comes out as the winner, with very good performance, great nativity and a relatively small size (in comparison to the other frameworks). While Xamarin's compilation model could be considered superiour, React Native achieves at least identical performance with the usage of asynchronous threading. To sum up the conclusion, it is clear that React Native comes out as a superior framework, with both great developer & user experiences, and coming in as a close second in the pricing perspective. While Ionic 2 does come out as a potentially cheaper framework to develop with, its other shortcomings make it difficult to use in high-quality applications. 13. https://github.com/react-community/create-react-native-app 24 4 React Native - an in-depth look Following the results of the comparison made in the previous chapter's finale, this chapter is focused on presenting an in-depth look on React Native. Initially, the chapter presents an overview of JavaScript as a language, looking at its history, current state, advantages & shortcomings. Afterwards, the chapter talks about the technology powering React Native - React. In the end, the chapter discusses React Native's under-the-hood specifics. 4.1 JavaScript 4.1.1 About JavaScript JavaScript is a high-level, multi-paradigm language with a dynamic type system. JavaScript is considered to be one of the three core technologies behind web development, present on more that 94% of all websites [21]. JavaScript is based on the ECMAScript standard, with its current version being ECMA-262[22]. A n important trait of a programming language is it being either a compiled, or an interpreted language. This characterization was purposefully omitted in the previous section; JavaScript is a language that can be both compiled and interpreted. JavaScript engines - like V8 or Rhino - utilize a concept known as JIT Compilation. When a piece of JavaScript code is interpreted several times, the engine marks this code as warm. As the code gets warmer (meaning that the code is interpreted repeatedly), the engine compiles the code, and utilizes this compiled code instead of re-interpreting the same code repeatedly. As explained in listing 4.1, the main aim of the JIT Compiling is to enhance the performance. 25 4. REACT NATIVE - A N IN-DEPTH LOOK f o r ( i = 0 ; i < 1 0 0 0 ; i + + ) { sum += i ; } // A J I T C o m p i l e r n o t i c e s t h a t t h e a d d i t i o n c o d e w i l l b e r u n r e p e a t e d l y , s o i n s t e a d o f i n t e r p r e t i n g , t h e c o d e g e t s c o m p i l e d d i r e c t l y i n t o m a c h i n e c o d e ; r e m o v i n g t h e n e c e s s i t y t o i n t e r p r e t t h e c o d e 1 0 0 0 t i m e s Listing 4.1: A n example of JIT compiler inside a JavaScript engine. As a multi-paradigm language, JavaScript supports several programming styles, and even combines them; be it event-driven, functional, or an imperative paradigm. Although initially only aimed to be a client-side language for web browsers, JavaScript is currently also being used on web servers[23], databases[24], and in some non-web programs like PDF software [25]. 4.1.2 A short look on history Back in 1993, the University of Illinois released the first popular graphical web browser named NCSA Mosaic. Following this, a company called Mosaic Communications was founded in California's Mountain View by several authors of the original NCSA Mosaic browser. The company's main aim was to create a browser similar to NCSA Mosaic, but better in every way. The browser was internally called "Mozilla"1 , and was released under the name Mosaic Netscape in the third quarter of 1994. After gaining popularity on the Internet2 , the company decided to re-name the browser to Netscape Navigator, and itself to Netscape Communications, as to avoid a potential lawsuit by the original creators. In the following years, Netscape started believing that the web needs to become more dynamic, by using a simple language which would be easy to use by web designers to assemble and communicate with components conveniently. Specifically, Netscape provided the following explanation: 1. A combination of "Mosaic" and "Godzilla". 2. Being the most used browser in the 1990s. 26 4. REACT NATIVE - A N IN-DEPTH LOOK We aimed to provide a "glue language" for the Web designers and part time programmers who were building Web contentfrom components such as images, plugins, and Java applets. We saw Java as the "component language" used by higher-priced programmers, where the glue programmers—the Web page designers—would assemble components and automate their interactions using [a scripting language]. Figure 4.1: Netscape's advocacy on JavaScript's genesis[26]. Afterwards, the company decided to hire Brendan Eich, whose goal was to embed the Scheme language into Netscape Navigator[27]. Shortly following his acquisition, Netscape started negotiating with Sun Microsystems about utilizing the Java language in Netscape Navigator. Consequently, the company decided to develop their own language which had to "look like Java ", ruling out languages as Python, Perl, or Scheme. The company required a working prototype rapidly, to defend the genesis of this new language. Eich was able to write a prototype in May of 1995, in just 10 days. Figure 4.2: A schema depicting the sources behind JavaScript's fea- tures^]. 27 4. REACT NATIVE - A N IN-DEPTH LOOK Although originally developed under the name Mocha, the language was renamed to LiveScript when shipped with the beta release of Netscape Navigator 2.0 in September 1995, followed by another renaming to JavaScript a few months later. Almost a year later, Netscape started cooperating with Ecma International to create a standardized specification based on Netscape's work[29]. This cooperation led to the release of ECMAScript language specification - published in the first version of ECMA-262 standard in June 1997[30] - of which JavaScript is the most popular implementation. 4.1.3 Advantages & shortcomings of JavaScript JavaScript, as any other programming language, comes with its own set of conveniences & flaws that are important to mention. Advantages » Dynamically typed - JavaScript uses at-runtime type checking and implicit type conversion where necessary. This could be taken as both an advantage and a shortcoming; the former in smaller & medium-sized projects, the latter in larger & enterprise-sized projects. » Simple debugging - Debugging a JavaScript application is usually made simple using a well-equipped browser like Google Chrome, which already implements tools for debugging - the Chrome DevTools - out of the box. » Platform agnostic - Given that JavaScript is generally processed (using a JavaScript engine like V8 or SpiderMonkey), the resulting interpreted (or compiled) code is typically platform agnos- tic. » Multi-paradigm approach -JavaScript supports the utilization of multiple paradigms, which liberates the developer to utilize whatever approach he deems the most appropriate to his specific situation. Examples of various paradigms' approaches are portrayed in listings 4.2, 4.3 and 4.4. 28 4. REACT NATIVE - A N IN-DEPTH LOOK 1 l e t t e x t = " H e l l o W o r l d ! " ; 2 c o n s o l e . l o g ( t e x t ) ; 3 // > H e l l o W o r l d ! Listing 4.2: A n example of imperative approach in JavaScript. 1 l e t t e x t = () => c o n s o l e . l o g ( " H e l l o W o r l d ! " ) ; 2 t e x t () ; 3 // > H e l l o W o r l d ! Listing 4.3: A n example of functional approach in JavaScript. 1 f u n c t i o n T e x t ( ) { 2 c o n s o l e . l o g ( t h i s . t e x t ) ; 3 } 4 5 T e x t . p r o t o t y p e = { 6 t e x t : " H e l l o W o r l d ! " 7 } 8 l e t f o o = n e w T e x t () ; 9 // > H e l l o W o r l d ! Listing 4.4: A n example of object-oriented approach in JavaScript. Shortcomings » Dynamically typed - As mentioned above, runtime type checking and implicit type conversion could be considered advantages in a smaller project, but become a hindrance in biggersized projects. » Potential security issues - JavaScript scripts execute immediately after the user enters a web page, which opens the door for potential malicious exploits of the user. Although modern web browsers are usually restrictive concerning these exploits, there are still ways to abuse the user. » Compatibility - Different browser engines could execute identical JavaScript snippets differently, causing hard-to-track inconsistencies. Fortunately, this issue is typically solved by utilizing a transpiler. 29 4. REACT NATIVE - A N IN-DEPTH LOOK 4.1.4 Transpilers Transpilers - or transcompilers - are an important part of the JavaScript ecosystem. A transpiler, is a tool that reads source code written in one programming language, and outputs the equivalent code in another language[31]. The usage of transpilers is twofold. On one hand, they are used with languages like TypeScript or CoffeeScript, to help implement different specific concepts that are absent in JavaScript. The other usage is to utilize features from newer editions of the ECMAScript specifications, which are not necessarily supported by all browsers. Every browser uses a different JavaScript engine, with different performance characteristics, and varying implementations of the ECMAScript standard, which forces the developer to either write old JavaScript code, or to utilize a transpiler to take care of the issue. Transpilers also play an important role in the decision-making of the TC39 committee[32] - the committee responsible for designing the ECMAScript standard. The committee considers a feature's implementation in a transpiler to determine whether the feature should be implemented in the ECMAScript standard. Arguably the most popular JavaScript transpiler is Babel, which supports transpilingboth standardized ECMAScript language, and several cutting-edge ECMAScript versions - labeled as stages. 4.2 React React is an open-source JavaScript library used for building user interfaces. It was created and is maintained by Facebook, Inc. The main motivation of React was to create a library with great speed and fluidity of user interfaces, which reacts immediately to any changes, but only updates the components that require an update. React utilizes so-called components as a main building block. Each component is either a JavaScript function (a stateless component), or a JavaScript class (a stateful component). A stateless component returns the HTML markup directly, whereas a stateful component extends the React.Component class and implements the renderO method, which returns either another component, or HTML markup. 30 4. REACT NATIVE - A N IN-DEPTH LOOK React typically requires a root component that is rendered to a
tag with its ID set to "root". React makes use of the uni-directional dataflow. In other words, in vanilla3 React, the data received by a component cannot be directly changed, but can be used as variables in its methods or attributes. On the other hand, a component can receive a callback function to change this data in the component above, and receive the updated data afterwards. This ensures that data only moves downwards, whereas function callbacks only move upward the component tree. Virtual DOM State Change • Compute Diff • Re-render Browser DOM Figure 4.3: A simplified schema of Read's DOM Reconciliation[33] Another important feature React provides is the Virtual DOM. D O M 4 is a programming interface representing the HTML document as a tree-like structure of nodes and objects. This allows the developer to edit the nodes programatically[34]. A major flaw of traditional DOM manipulation is its resource-heavy computation. React instead 3. Without any other Node modules 4. Document Object Model 31 4. REACT NATIVE - A N IN-DEPTH LOOK utilizes the Virtual DOM; an abstract representation of the DOM that is synchronized with the real DOM whenever necessary. This synchronization process is called Reconciliation, and is explained in depth in Read's documentation[35]. Whenever the developer modifies the DOM with React, instead the Virtual DOM is modified. Then, React starts the Reconciliation, which compares the DOMs, locates the differences and transforms the original DOM in the most effective way. This results in both faster reactivity and a better developer experience5 . It is important to specify that React is not a framework, but a Javascript library instead. The distinction comes with respect to the MVC software architecture model, where the "Model" maintains data and the application logic, "View" represents the visualized data & user interface, and "Controller" accepts input and passes it to the other two parts. In this architecture, React takes the role of the "View" component of the MVC model, since e.g. working with a database requires use of a different technology altogether. 4.2.1 JSX JSX is a Read-specific syntax extension that enhances the ECMAScript standard with the usage of XML-like tags. JSX is built out of elements and components. Elements are the JSX equivalents of ordinary HTML tags (e.g.