Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
I need to write an app for both iOS and Android (and possibly Windows and Mac OS X in the future) and was wondering how you guys go about code sharing between platforms?

I was thinking about writing the core of the app in standard C++ (probably will make use of all the new features in C++11 and C++14) and then just adding a small amount of platform specific code for the user interface and go from there. Does that sound reasonable?

Alternatively I was thinking of using Xamarin for the app and developing everything in C#. I've never used Xamarin before though so I'm not sure how good it is. Does anyone have any experience with it at all?

Any hints at all from people who have done this cross platform coding before? I have an awful lot to learn in a small amount of time. The last time I did any iOS programming was when iOS was at version 3 or something so I need to get up to speed with the new features and the new Xcode and I've never done any Android programming before either so I need to refresh my Java skills and get to grips with the Android development platform.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
This isn't specific to your case, it's about porting in general.

When it comes to picking the language and its capabilities, pick the smallest subset that you know works across all intended platforms. Do not pick a large, expansive, or evolving set. Do not be vague about what the subset is.

Unless you know with certainty that C++14 works on all intended platforms, you'll be safer picking an older version, and maybe even a subset of an older version. "With certainty" means that you've actually tested and confirmed that a feature works. You'd be surprised how much stuff has at least one failure when trying to port to multiple platforms. For a single platform the failures may be edge cases, but after each platform contributes its own edge failures, that's a lot of sharp edges that have to be avoided.


Create portable implementations that hide differences. In pattern terms, create a single Facade that's identical across platforms, even though its internals may be platform-specific. Do this for each modular subsystem. If this seems obvious, all I can say is it's not. I can't tell you how much crap I've seen with #if and #elif around platform-peculiar stuff that was easily wrapped into a portable and simplified Facade.


Another thing that may be obvious is picking a language that directly addresses the portability problem. Maybe you can pick an existing cross-platform language, and let it deal with all (or most of) the differences. These are often marketed and discussed as "game languages" because games are frequent targets. But that doesn't mean they can't be used for other apps. It depends on what the app is intended to do, what it looks and acts like, etc.

Even if the language is sub-optimal compared to C++, you won't need to write Obj-C, Swift, C#, Android, etc. for each of the target platforms. That's a lot of separately maintained UI code that doesn't have to be written. User-interface code is almost always more complex than one first imagines. And if you end up doing a UI redesign, you'd have to rewrite it in N different languages. That could easily become a barrier that inhibits necessary improvements.

Maybe C# meets the criteria for a cross-platform language, and maybe it doesn't. You'll have to test it to find out. Maybe there are other languages, more "game oriented", whose cross-platform support has fewer errors.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
On the several projects I've worked on where we delivered iOS and Android versions there was never any code sharing. Objective-C and Swift don't run on Android and Java doesn't run on iOS. Other languages were never in the running. I did observe one project that used PhoneGap. That project is now defunct.

IMO, the best you can do is have a shared design of the UI and the data model layer. You are also likely to end up with some features that make sense on one platform and not the other.
 

1458279

Suspended
May 1, 2010
1,601
1,521
California
Many of us are in the same boat, including me.

What you are asking about is the time you would spend learning to make a program work on different platforms. One solution is to use one code base, another solution is to rewrite from scratch for each platform, another is a blend where you write routines as generic as you can, maybe C/C++ so that you can reuse them.

Part of the problem is making use of the power each platform offers.

ObjC runtime is VERY powerful, but NOT directly transferable to other platforms.

Point: if your app wants/needs to take advantage of the ObjC runtime, you'll be forced to rewrite for each platform.

IMO, you need to look at the needs requirements of your specific app. If it were a game, I'd use Unity. If you need to take advantage of ObjC runtime and if other products (Xamarin) don't offer that functionality, then you need to use ObjC/Swift.

You should base your answer on the needs of your app.

The time you spend converting from one language to another should be compared to the cost of the products (Xamarin) and the advantages your app will gain from being in the native language.

There is also an advantage of knowing more than one language, although is sucks that we have all these different OSs/APIs/device sizes/etc... It's just the way things are right now.
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
Thanks for the responses all :). I'm in the lucky position of already knowing C, some C++, Java and Objective-C so at least I won't need to learn a new language for a little bit.

I'll have a think about this. The problem is that this app is likely to have quite complex internals that rewriting for multiple platforms just isn't an option. This I think would fit perfectly in C or C++ since that works on all platforms I am currently interested in. On the other hand I do accept the advantages of using each platforms native development tools and their APIs in order to keep the "look and feel" right for each platform.

I also like the fact that if you develop in each platforms "native" language you get the advantage of excellent documentation resources. I think what I might do is develop a testing app for one platform (one with cut down features compared to our idea) and see how that goes and then see how much code I can extract for sharing between platforms. Often it takes a bit of playing around with an idea to fully understand the problems you are likely to come across.
 

Boris-VTR

macrumors regular
Apr 18, 2013
247
17
Think about hybrid apps using for example http://ionicframework.com
Write once and use it on ios, android and windows. If your app is basic and you have time limit and tight budget that would be logical way. I must say, on new device they run very very fast.

If you want to go native, there is no code sharing. And their style is different so event code architecture cant be related all that much. But having said that...when you build on app and solve logistic problem.....when you write other app, you will do it much faster, because you know exactly what to do. I am currently at that stage, when i am writing same app nativly for both platforms.

I have writen and published couple of html/ccs/js and couple of native apps. So from my experience in business when you have to consider time nd budget i go with hybid app when they are basic. It there is some super difficult feature i have to opt for native.

If i do personal hobby project I always go ntive.....and ios :)
 

1458279

Suspended
May 1, 2010
1,601
1,521
California
I imagine that these platform sharing tools have the ability to run 3rd party libraries. If that's the case, you could write your own personal library in C/C++, then use the platform tools to simply take care of the rest.

I haven't tried any of those products, but my guess is that they would have to support 3rd party libraries.
 

Boris-VTR

macrumors regular
Apr 18, 2013
247
17
Not entirely true as I already stated. You can write the core of the app in C or C++ and use it on both iOS and Android.

I meant that if you write in so called "native" language Java or Swift(or Objective-c) than there is no sharing.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.