Write iOS apps in Java along with Android

Worldwide, Android is installed on 66% of mobile devices, while iOS is used by 24% of the global users. But in countries, where users generates more revenue (like USA or UK) more people uses an iOS (58% iOS and 40% Android in USA and 53% iOS and 40% Android in UK).

Mobile OS market share worldwide and in USA for Feb 16 — Feb 17. Source: StatCounter.com

That is why most companies focus their app development on both platforms. Having an Android app results in popularity (but not in all countries) and iOS basically means bigger earnings.

Mobile OS market domination in countries for Q1 2017. Source: StatCounter.com

Native vs cross-platform apps

To handle both platforms we need to create two native apps or use one of the cross-platform mobile framework. Some frameworks enables developers to write only one project and launch it on both platforms, losing some native profits.

Most popular cross-platform frameworks

Web-based frameworks like PhoneGap or Ionic allows developers to quickly create an Android and iOS app using HTML, CSS and JavaScript. However those apps are basically a webview with embedded web app. This idea may look promising but in the end apps have terrible performance, UI is looking not so well, access to the platform API is very limited, using any native library is impossible and hard-to-trace issues.

While moving toward native apps we gain better performance (and usually happier developers) with native UI views and better platform API support.

In Xamarin (not Xamarin.forms) views has to be designed natively but it provides vast platform API support (C#). However using native libraries is still impossible and you have to rely on “Xamarin Components” — some libraries rewritten in C#.

And there is one more special framework, which requires some special attention - Multi-OS Engine - the closest option to native development.

Multi-OS Engine

An open-source framework created by Migeran and developed by Intel. It enables developers to create Android and iOS app in Java (or Kotlin).

  • Fully integrated with Android Studio and Xcode,
  • ART on iOS (with AOT compilation) - provides optimal performance,
  • Requires native views for Android and Xcode,
  • Fully supports external native libraries
  • Gradle plugin support,
  • Great, helpful and growing community

But the most important and innovative thing here is that:

Multi-OS Engine is completely not influencing your Android application, developing it is 100% native.

Magic of this framework lies in the Nat/J - the bridge between native iOS code and Java. Using plugins it can be used to generate Java bindings for iOS native views or even CocoaPods!

Talk is cheap. Show me the code.

You will need OS X (or Windows - it is a little bit harder there) and completely working Android Studio and Xcode. Full specification is available here.

Install Multi-OS Engine plugin for Android Studio:

  • Go to Settings > Plugins > Browse repositories > Manage repositories > Add:
  • Refresh plugins and install Multi-OS Engine Plugin (v. 1.3.0-beta-2 or newer)
Multi-OS Engine MVP project structure

Project structure

Enabling iOS support is basically adding a new java (or kotlin) module to the project.

If you keep your Android project in a clean Model-View-Presenter architecture adding iOS view layer by Multi-OS Engine is quite easy.

That’s next steps of enabling iOS support can be done at any point of developing an Android app. You can start with iOS in the beginning or add it later.

Add Java (or Kotlin) based iOS module to Android project

Basic structure visible in Android Studio
  • In project view click New > Module > Java Module and name it “commons”,
  • Right click on your Android module > Open module settings > Dependencies > Add module > commons,
  • In project view click New > Multi-OS Engine Module > Single View Application. Good practice here is to name the module “ios” in the last step,
  • Right click on your iOS module > Open module settings > Dependencies > Add module > commons,
  • Now move your model and presentation layer to the commons module - now you can access it both in Android and iOS modules.

Take a look at your run configurations. Now you are able to run and debug your iOS app from Android studio while working on a iOS Simulator or connected iPhone/iPad device.

Now, let’s switch to Xcode and add some native views. Right click on iOS project and Multi-OS Engine Actions > Open project in Xcode.

Create iOS view and generate Java bindings

In Xcode open your storyboard file and modify the views as you wish. I highly recommend here to spend some time and watch some iOS UI tutorials. To continue you should have your views created and linked to view controllers classes headers.

Then proceed back to Android Studio, select your iOS project with right click and select Multi-OS Engine > Create new binding. Name it and fill it as below:

Nat/J binding generator.

It will create an Java class named MainViewController in selected package.

Here you have remember to:

  • Change the class annotations to these visible above,
  • Override an viewDidLoad() method,
  • Access desired method by removing the @Generated annotation, native keyword and adding an body.

Now you are able to use your Java classes from commons (maybe subscribing to some RxJava subjects?) and fully access native iOS SDK.

The community

In case of any problems, issues questions I highly recommend to check official Multi-OS Engine forum: http://discuss.multi-os-engine.org

Usually every issue is solved there, the community is extremly helpful, nice and it’s growing intensively. Also there is a very high probability that your issue will be solved by the creators of this framework, who spend a lot of their time helping developers.

I have spent last few months exploring and working with Multi-OS Engine to develop the SlideCase Android and iOS apps. It truly was a great adventure and I cannot wait to write next story about using native CocoaPods.

If you have any questions, please don’t hesitate to reach me using: