Tutorial :Objective-C and Android [closed]


I've just finished a relatively large project for the Android, and it's left a bitter taste in my mouth with the knowledge that it will never run on one of the most ubiquitous handsets this side of the solar system (the one by that fruity little club).

So, for my next project, I want to write it in a way that makes most of the components easily transportable between the iPhone and Android platforms. The way I'm thinking of doing this is by coding most of it in Objective-C, and then adding the platform-specific parts in more Objective-C and Java respectively. On the Android side, this will require using the the NDK.

My knowledge of C is good, but my knowledge of Objective-C is close to zero, and I have no desire to learn C++. How sane is the approach above, and is there a better one? Is there any way I can code in Java and still reach the un-hacked iPhone market? And how likely is it that the people I know (iPhone users) will have an Android phone by next year?


Step back and think about what in the end you will logically be able to share.

The UI models are fairly different, the components are different. In the end what you might be able to share is data object classes, possibly some algorithms. It's not even like you could realistically end up sharing network code as in the old days because you aren't directly using sockets, you are using HTTP libraries.

So will all of the effort you are putting into this really find a payoff in the end? It seems to me the end result will be a brittle mess that is hard to update, and is mediocre on both platforms instead of being great on either.

Why are you writing applications? To make life easier for you, or your users?


Others have said basically this, but I'd like to make it more explicit. Your best bet is probably to write:

  1. Cross-platform data models & core logic, using:
    • bits of GNUstep (Obj-C), or
    • CF-Lite (C), or
    • Whatever you'd like, as long as it's cross-platform :P
  2. iPhone-only interface code, using Cocoa Touch (Obj-C)
  3. Android-only interface code, however they do it for the Android.

That's as close as you can get; any attempt to write cross-platform interface code will undoubtedly result in a mediocre app on both platforms. But making all the rest of your code portable and just wrapping a device-specific interface around it is done all the time and has been worked great for some iPhone developers.


Objective-C without Cocoa is not so useful and won't bring you much closer to haveing a working iPhone codebase. You'd probably be better off writing your core in C with Core Foundation and using either Java or Objective-C for the platform specific parts. Apple has open sourced a large chunk of Core Foundation as CF-Lite, and it's toll-free bridged with Cocoa on OS X (i.e. you can use many CF classes interchangeably with their Cocoa counterparts).


My guess, which has no experience to back it up, is that you probably could write Obj-C with Google's NDK somehow, given that GCC exists for ARM, is open source, has an Obj-C compiler and a basic Obj-C runtime (which if it doesn't already probably could be hacked up to work on a new architecture), etc.

That might also be a lot of work for questionable benefit.

And of course "Obj-C" (without the NS classes) means something very different than "Cocoa", which is what most people really mean when they say "Obj-C". You might be able to re-use some of GNUstep for some that, but... Honestly, I doubt it. Sounds again like a lot of work.

So, yes, I think it is possible. It's also a lot of work and I don't think it's worth it.

Given what you've said, if I were attempting this, I would be tempted to write as much of your core logic as possible in C, then wrap it with two separate GUIs for each platform.


Coming at this from a different angle... I know that you said you wanted to try and stick with Java, but if you know C# then you could go with the MonoTouch framework for the iPhone. Mono is essentially and open source implementation of the .Net stack. The Mono team is working on bringing Mono to the Android so you could basically write a shared C# library for your business logic and have different Views/Controllers per platform. This would all be in C# of course and it is a bit more expensive, but it does solve the problem of writing everything in different languages.

I believe it is called MonoTouch on the iPhone and MonoDroid on Android.


The Apportable SDK is an Objective-C approach to write once and deploy to both IOS and Android. It will cross-compile a running IOS Xcode project to an Android SDK.

See here for sample apps that run on both platforms in minutes after download.


I'm not sure about Android but with the iPhone you can essentially write straight C as long as you wrap it up in Objective-C classes.


The Objective-C runtime has not been ported to Android yet. It shouldn't be too much work, but still, without a working knowledge of the language I doubt you'll have an easy time porting it.

What you are trying to do is going to be hard for a generic application, but should be possible for games, if you choose to develop the game in plain C (which is supported by both the Android NDK and the iPhone). You'd have to write up some glue code to pass input events from the Obj-C and Java environments into your C code, but that shouldn't be much of a problem - Objective-C allows you to directly call into your C code and there are plenty of example projects which do exactly this for Android.


I haven't tried this myself or finished watching the talk yet, but there is a Google Tech Talk on Developing iPhone Applications using Java up on YouTube that looks pretty promising.


XMLVM is a project which is capable of translating (some) Android applications to the iPhone. For more infromation, visit http://xmlvm.org/android/


I realize this may be a tad late, but it seems the industry is going in the direction of web apps these days to achieve app portability. That is, embedding a web-browser in your "skeleton native app", and writing javascript, css and html for Android, iOS and the other major smartphone platforms.

There are tools that help you with this. You might want to check out PhoneGap and Sencha Touch, but there are many more. Note that this approach may not be ideal for real-time/animation-intensive apps.


Here is a talk from facebook's mobile @ scale conference where two teams (dropbox and orchestra) used similar approaches. Dropbox used C++ to create libdropbox and Orchestra (mailbox) used Objective-c to create libmailbox.

Again, they wrote their front ends in the platform native language and used their cross platform libs for core logic and data.

Key benefits I took away: Mailbox went from ios to android in 5 weeks because it was just building UI code. Dropbox can beta test changes to core functionality that are in the shared library with Android beta deployments were it's easier to do massive deploys at scale for beta builds.


If you can wait until later this year (exact amount of time unknown), Adobe will have AIR for Android and a compiler to iPhone. Thus you can write an app in AIR for the Android and use most of the same code to compile to the iPhone.


Even if you can't wait see: http://www.insideria.com/2008/12/actionscript-to-cocoa---protot.html where it explains the similarities between ActionScript and Cocoa.

Also check out: http://labs.adobe.com/technologies/air2/ for the AIR version capable of using the touch screen.

So you can soon write once and deploy to Android and iPhone using ActionScript 3.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »