TIQETS.ENGINEERING
Sharing code between iOS and Android
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.
It's 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.
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 be used in your iOS app in both Swift and Objective-C.
Update 2021: KMM Kotlin Multiplatform Mobile
Kotlin Native has matured and for mobile development, they released Kotlin Multiplatform Mobile: https://kotlinlang.org/lp/mobile/. While currently in Alpha, it could be the solution for your project! You can keep using pure native UI’s while sharing business logic between your apps, and potentially even your backend/web app. (Since its Kotlin).
Transcompilation
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, it's 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 currently in a number of their apps.
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.
You could for example run JavaScript code in your app if you had a JavaScript interpreter in your app.
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, it's 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.
Web Apps
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
Hybrid apps are apps that run both native code and have a runtime bundled to run code in a different interpreted language. This language is often JavaScript.
The idea here is to basically load a website or a bundled website in an embedded web component in your native “wrapper” app. This wrapper app might also expose native APIs to your JavaScript app that are not found in the web APIs standard.
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 that 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.
React Native
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.
https://facebook.github.io/react-native/
Xamarin
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
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 is 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!
Conclusions
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 with their first Android app. And while doing other apps for them, I’ve learned a lot about the different options for cross-platform development.