views:

1184

answers:

6

Simply put: What is the most effective way to share / reuse code between iPhone and Android builds?

The two most common scenarios I think would be:

  1. Blank slate new project, knowing ahead of time there is a large chunk of reusable logic that needs to run on each device.
  2. Existing iPhone code base, porting of C, C++ and Objective-C to the Android NDK or otherwise.

Yes of course in a perfect world all apps would just plug into the magical cloud and all the reusable logic would be up in Google App Engine or some web services, but that is not the spirit of this question. After experiencing a port of iPhone to Android with no code reuse at all second-hand and seeing the pain that person had to endure, I'd like to know how other people are avoiding it.

+1  A: 

I have been working on creating applications and games for iPhone using Lua with my own framework. This way I could eventually implement the same framework for Android using Android NDK, but the actual application code would hopefully be exactly the same for both platforms.

I do not think there is an easy way to do this, because the APIs are obviously different and native programming languages are different, but building your own framework in any language that is supported by both platforms, would be my suggestion. Maybe there already is a framework that would do the hard stuff for you? If there isn't any good frameworks for doing that, then this is clearly an opportunity to implement one yourself.

Tuomas Pelkonen
+1 for raising the topic of LUA - excellent point I think people are forgetting
slf
+4  A: 

Like I told someone who asked a similar question a while ago, use MVC and implement the MC in C++ and the V in obj-c or Java.

Hassan Syed
This is not a very good idea since without Objective-C and Cocoa Bindings for the M+C you end up with something very inelegant and primitive.
Tommy
If you are thinking of framework provided MVC bindings, or autogenerated code. These don't need to be used, one can simply write the interface layer oneself. Also, what criteria for inelegant and primitive are you thinking of ?
Hassan Syed
+4  A: 

In my experience, you can use Android NDK to compile C and C++ , so if you use iPhone Obj-C++ (.mm) bindings for a C++/C engine in the iPhone, and in Android you use Java bindings to the same engine, It should be totally possible.

So C++/C engine ( almost same codebase for Android and iPhone ) + Thin bindings layer = Portable code.

Mr.Gando
A: 

You could also have a look at Titanium. The say with their tool you can write your app in abstract web code and they will help you web app interface with all those gps, accelerometer sensors etc.

Janusz
A: 

Write as much as possible in plain old C (or C++ if needed) and just include the same files in Android and iPhone. Works on Windows/Mac too. "cross platform" libraries tend to consume you.

Tom Andersen
You really can't go wrong with putting as much as possible of your logic in plain old C.
zumalifeguard
+1  A: 

You could have a go with Airplay SDK... Compile once to native ARM and deploy to both Android and iPhone with the same binary, standard C/C++, develop on windows or mac, lots of (optional) middleware tech included.

maurice
this looks incredibly cool from a game dev perspective
slf