A major slowdown in app development is the fact that you have to write code for different platforms. App development would be a lot cheaper if we could share some, or even all of the code between the platforms.
Its not easy to achieve code sharing currently, and still keep your app of high quality. (OK, it’s not easy to build an app of high quality… period 😛). However there are possibilities.
In this article I’ll discuss the main solutions in this space, without pointing you to the right solution. That all depends on your requirements. Like with everything in Software Engineering. It depends..
Cross Platform Logic
Let’s first see how we can share code between platforms without involving the UI. Mainly the business logic of your app. Which is often similar between platforms and therefore a good candidate for code re-use.
Using Compiled code
One straightforward way to share code, is using a language that can compile to machine code for both platforms.
Historically C++ is used for this mostly, but languages like Golang and Rust also lend themselves well for this technique. This is often how Games are targeting multiple platforms.
The Go mobile subrepository adds support for mobile platforms (Android and iOS) and provides tools to build mobile…
A new kid on the block here is Kotlin. Kotlin/Native project uses LLVM to compile Kotlin (or a subset of) to native code. This could could be used in your iOS app in both Swift and ObjectiveC.
A transcompiler is a source-to-source compiler. It rewrites code from one language to another language. Using this technique we could simply share our code between platforms by transpiling it from language platform A (Java, Kotlin) to language on platform B (Objective C, Swift).
However its quite difficult to write a transpiler.
Google achieved this with the J2Objc project. This tool allows you to transpile Java code to Objective C. Google is using this actively in a number of their apps.
Project site: https://j2objc.org J2ObjC blog: https://j2objc.blogspot.com Questions and discussion…
Experience Note: I’ve tried cross compilation on a production app, however it was hard to actually get the cross-compiled code compile succesfully for the iOS platform.
Bundled Language Runtimes
Another way to share code is to bundle a language runtime with your app. This is mostly needed for interpreted / VM based languages.
Other approaches that fall in this category:
- Dart runtime engine
- A LISP -or put your language here- runtime
Moving code Server-side
This is not really a solution in the sense that it offloads the code from your app to the server. However its a valid technique that often gets forgotten: Put your business logic in the backend. This allows you to keep the apps lean. This is often only possible for simple apps.
The Backends-for-Frontend architectural pattern fits in this category
Cross Platform UI
In this section we’ll discuss frameworks that allow you to write your app completely in one language and run it on both iOS and Android.
You could argue that you don’t need an app to deliver rich experiences to users. A website using advanced W3C/WHATWG web APIs should be sufficient.
And indeed this might become a replacement for the apps in the future. However currently these “apps” have shortcomings. Which is out of scope for this article.
Hybrid (Web) Apps
Experience Note: I’ve worked on a hybrid app in 2010 replacing a native Blackberry, Windows phone, iOS and Android app. While the project was successful, the final hybrid app performance was not on-par with the native apps.
Native UI Abstraction Frameworks
There are frameworks which provide an abstraction layer on top of the native APIs and UI Components. In this way you can use native components in an abstract way, and let the framework render them for both platforms.
With reactive native you define your UI Components using the React Native Components. These React Native Components are abstractions on of native platform components.
The nice thing about react-native is that is a complete reactive framework where your UI is declarative.
Xamarin is a .Net C# based framework that provides an abstraction layer of the native platform.
Custom UI Frameworks
Another approach is to include a complete custom UI rendering pipeline in the cross platform framework. This does mean that you cannot use the native UI components directly anymore. However flutter has a solution to this problem.
Flutter is an UI Framework build using the Dart Language. It mimics every native UI component on iOS and Android exactly. Rendering of these UI Components is completly done in Dart.
This allows you to write an app that looks completely native, but under the hood all your code is written in the Dart Language using the Flutter UI components.
Flutter is a project created by Google and is in use currently on Google Home devices and in other Google Apps.
The nice thing about flutter it that a lot of thought and effort has been put into it by Google to make it one of the best contenders for solving the cross platform app development issue. Developing apps in flutter is fast, the apps are performant, the platform is reactive by design, etc etc. Yes I’m a fan!
Flutter - Beautiful native apps in record time
Flutter is Google's UI toolkit for crafting beautiful, natively compiled applications for mobile, web, and desktop from…
There are a lot of ways to enable code sharing between your iOS and Android app. However that makes it more difficult to choose the right solution.
This article gives you the insights you need to make a wise decision.
Note: Not sharing code between your iOS and Android apps is also an option 😉
Note: I’ve helped tiqets.com on their first Android app. And while doing other apps for them, i’ve learned a lot about the different options for cross platform development.