TIQETS.ENGINEERING

Sharing code between iOS and Android

Tjerk Wolterink
Nov 26, 2019 · 5 min read

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..

Image for post
Image for post
Photo by Elaine Casap on Unsplash

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.

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 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.

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.

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

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 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.

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.

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.

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 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!

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 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.

Tiqets Engineering

Technology and more ways to culture

Tjerk Wolterink

Written by

Engineering Lead with a focus on Scaling Teams, Backend (Python, Golang, Kotlin, Kubernetes, Microservices) and Mobile (Android, Kotlin, Flutter, iOS) at Tiqets

Tiqets Engineering

Tiqets connects travelers with instant, last-minute, mobile tickets to top museums and attractions all over the world. Read about all the Tech we do at Tiqets and more!

Tjerk Wolterink

Written by

Engineering Lead with a focus on Scaling Teams, Backend (Python, Golang, Kotlin, Kubernetes, Microservices) and Mobile (Android, Kotlin, Flutter, iOS) at Tiqets

Tiqets Engineering

Tiqets connects travelers with instant, last-minute, mobile tickets to top museums and attractions all over the world. Read about all the Tech we do at Tiqets and more!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store