10

I develop an app for both iOS and Android, and I'm loon'ing for the best way to share code between this two platforms.

What I would like to do is creating all the View (UI part) in native but share the code for the logic (controller + model).

With all I found, 3 things seems to be quite good :

1) C++ --> Build library file Using c++ For the logic so I'll be able To use the .dll files in the 2 platforms

2) Azure mobile apps services. Is it possible to habe all the logic in a webservice? The issue is that if I dont have acces to internet, my app will be unaivalable, right?

3) I hear a lot about React native used by Facebook, but it seems to be used to create the UI, but I prever create it in native. Can I use react only for logic?

CinCout
  • 9,486
  • 12
  • 49
  • 67
Adz
  • 281
  • 3
  • 16
  • Have a look at xamarin, allows you to write in C# and generate applications for iOS and android. – Jonathan May 05 '16 at 09:59
  • Reactive programming to me sounds like the latest fad in software engineering. In my own experience, reactive programming applied to even half-way bigger projects results in a hopeless, unmaintainable, undebuggable mess. – Christian Hackl May 05 '16 at 10:00
  • *"Is it possible to habe all the logic in a webservice? The issue is that if I dont have acces to internet, my app will be unaivalable, right?"* - How do you expect the answer to this question to be anything but "no, you don't get into the web without internet"? – Christian Hackl May 05 '16 at 10:01
  • And finally, if you aim for good user experience, then you will have to create different UIs for every platform anyway. Depending on the type of application you want to develop, chances are that sharing any logic is not worth the trouble at all. – Christian Hackl May 05 '16 at 10:03
  • it's hard to reason about 2) without knowing what does an app do. Apart from that you would have another piece in an equation and from my experience supporting even a small web server is not trivial. – szymek May 05 '16 at 11:53
  • You may want to look at j2objc.org. It's a translator of Java code to Objective-c. You can have javascript too if you use Gwt compiler. – Francesco Donzello Sep 29 '17 at 08:21

2 Answers2

15

It seems like you have three options:

1. C++


You can't just have a compiled .dll and expect it to work for iOS and Android. They both have to be compiled in different architectures and it has to be a static library on iOS.

Dropbox's done it this way, and they've put up a lot of notes and example code, and code you can use so you can take a look.

Pros

• Pretty straightforward after you manage to set it up
• No additional layer of dependencies, bugs, etc (like in case of Xamarin/React Native)

Cons

• Setting it up and using it needs a lot of extra work: you need to setup additional compile steps and write wrappers for both platforms.
• Some other challenges you're surely going to meet when trying to compile the same code for two different architectures

Here's a SO post on how to do it in detail...

2. Xamarin


This option seems to extreme to use in this case. You're forced to use C# and introduce another layer of dependencies and bugs. You said you don't want to use another language for UI so I wouldn't recommend it.

3. React Native


Now this is a viable option. You can write scripts in JS and use them in native code in both Android and iOS.

Here's an article on how to share code with code examples...

Unfortunately it uses React Native for UI, but you can easily call React Native functions from native code.

There are a lot of downfalls to using this, including the fact that the calls are asynchronous as they're executed on another thread, so you would have to implement some kind of callback system for functions that return something.

Pros

• Seems to be easy to set up and write

Cons

• You'd have to implement a native callback for every function that returns something
• Using it has a lot of downfalls that the document describes:

• As events can be sent from anywhere, they can introduce spaghetti-style dependencies into your project.

• Events share namespace, which means that you may encounter some name collisions. Collisions will not be detected statically, what makes them hard to debug.

• If you use several instances of the same React Native component and you want to distinguish them from the perspective of your event, you'll likely need to introduce some kind of identifiers and pass them along with events (you can use the native view's reactTag as an identifier).

Conclusion


I think I'd go with C++, mainly because a big company (Dropbox) tried it and succeeded and actually uses it in production. You could try React Native as an experiment, it would make a great study case!

Community
  • 1
  • 1
michal.ciurus
  • 3,616
  • 17
  • 32
3

I'd say that putting the "core" logic into a separate library is a sensible approach.

You are not the first who wants to do this, and I highly recommend looking at Djinni. It's a tool to accomplish just that. You can define common interfaces and datatypes and fill in the native parts. Communication is possible in both ways.

It's not as easy as writing the whole thing natively at once, but it supports clean design which you might benefit from anyway.

Eiko
  • 25,601
  • 15
  • 56
  • 71