This article is part of a talk done at the App Builders conference in Lugano that took place on the 30th of April 2019. Another part talking about code sharing between iOS and Android using C++ is available here.

Why sharing code?

At Ubique, we like to write native apps, in our opinion the best UX is achieved with fluid and native UI. If the presentation layer needs to be native, on the contrary the business logic can be shared. When business logic is shared then it is written once, therefore reducing maintenance, and allowing us to enforce a similar behaviour between iOS and on Android.

What is J2ObjC?

As the official docs says, J2ObjC is a transpiler or in other words source-to-source compiler. It will convert your Java code into Objective-C code. So at the end of the day, your code will run natively in Java on Android and in Objective-C on iOS. A more in depth explanation can be found at https://developers.google.com/j2objc/ alongside a detailed installation and different setup.

What’s this article about?

This article will illustrate how the engineering department at Ubique used the J2ObjC setup to achieve code sharing between our Android projects and iOS. It will show how we installed it and some sample code.

Bear in mind there might be different techniques to achieve the same result (some are better of course) but that’s the way that worked for us and we would be happy to see in the comments the approach you took.

How does it work?

The guys behind J2ObjC took most of the Java Runtime Environment and translated it into Objective-C. So when you return an ArrayList from your Java code, what J2ObjC will do is to translate that Java code to return a subclass of IOSArray . So under the hood there is no Java involved, only Objective-C code that is written to offer the same functionalities as the JRE library.

Requirement

You will need a Mac running OS X 10.11 or higher with Xcode 7 or up installed. The documentation asks for a JDK of 1.8 or higher, but digging through their code I found that only the JDK 1.8 is supported.

Installation

Head to the app store and download Xcode.

Check your Java version by entering java -version in a terminal. It should be 1.8. If non is present or you have a different version you can always install the JDK 1.8 from this link https://www.oracle.com/technetwork/java/javaee/downloads/jdk8-downloads-2133151.html

in a terminal. It should be 1.8. If non is present or you have a different version you can always install the JDK 1.8 from this link https://www.oracle.com/technetwork/java/javaee/downloads/jdk8-downloads-2133151.html We need to get a compiled version of the JRE translated into Objective-C, we can do that by downloading any release ZIP file from https://github.com/google/j2objc/releases. There is also the possibility to build from source but we will not cover it here. At the time of writing, we used release 2.3.1

Optional: Rename the folder to j2objc and move it to a location where all your projects can access it.

Setting up Xcode

Create or open an Xcode Project. In our case we created a single view iOS application project called J2ObjCTest.

Linking the JRE: