Objective-C and Android

29,724

Solution 1

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?

Solution 2

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.

Solution 3

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).

Solution 4

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.

Solution 5

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.

Share:
29,724
Tom R
Author by

Tom R

Good times with C, Java, Python on web and embedded devices.

Updated on November 14, 2020

Comments

  • Tom R
    Tom R over 3 years

    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?

  • Tom R
    Tom R over 14 years
    Thanks for your answer. The reason I specifically mention Obj-C as the language I'd like to code the core in (as opposed to C/C++) is that it has been recommended to me as being a very good C-based OOP language. How unusable is Obj-C without Cocoa?
  • David Thornley
    David Thornley over 14 years
    Objective-C looks to me to be a perfectly reasonable language without Cocoa. Hardly anybody ever uses it without Cocoa, because there are more popular languages on all the non-Mac platforms, but I see no reason not to use it. It is part of gcc, so it should be available on pretty much any Unix or Linux platform.
  • Tom R
    Tom R over 14 years
    Is is too much to ask for both? :)
  • rcw3
    rcw3 over 14 years
    This is becoming my new mantra... model (and any services/libraries/shared code) in C, view/controller in the most appropriate platform language / library.
  • Tom R
    Tom R over 14 years
    Best answer so far. This is very helpful for things like simple games, where a large amount of the code is game logic, and a far smaller amount accounts for rendering and UI.
  • Quinn Taylor
    Quinn Taylor over 14 years
    He didn't say Objective-C was unreasonable without Cocoa, just not nearly as useful. Imagine Java without any java.* and javax.* class, C without the standard library, C# without .NET, etc. A language may be great, but the corresponding code library is what really adds value for most people. Cocoa dramatically cuts the work that the developer must do to the point that without it, Objective-C is reduced to just a really interesting language. By itself, "interesting" doesn't pay the bills. More specifically, an iPhone app without Cocoa will be rudimentary and difficult to write and maintain.
  • SinisterMJ
    SinisterMJ over 14 years
    It is not too much to ask... but the universe is not prone to answering such requests very often!
  • asveikau
    asveikau over 14 years
    I would disagree that CF-Lite is a good cross platform solution. Basically it's convenient for Mac programmers and no one else. I'd sooner suggest something like GLIB -- although that is a bit biased towards gtk+ programmers.
  • asveikau
    asveikau over 14 years
    It might surprise you, but even today, despite some people's best efforts, not all the world fits neatly into an HTTP request.
  • asveikau
    asveikau over 14 years
    Obj-C is a good core language. If I were using it without Cocoa/GNUstep I think the things I would miss the most are retain and release from NSObject, as well as NS{Mutable,}{Array,Dictionary} and all the supporting classes. But if you wanted to rewrite those and whatever else yourself I guess you'd have a good foundation to work with.
  • andyvn22
    andyvn22 over 14 years
    You're unlikely to find something that's convenient to use with both the iPhone code and the Android code; I figure why not choose something like CF-Lite that has toll-free bridging with NS classes? That way, at least ONE of your interfaces will be easy to write. :)
  • Quinn Taylor
    Quinn Taylor over 14 years
    Yeah, that's kinda the key. If you didn't have all those things, you'd have to write them yourself from scratch. I agree that the core language is good. However, without the parts I use every day, it would be just another programming language to me. There are many solid languages that nobody uses. Like Smalltalk, on which Objective-C was based. Plenty of others come to mind, but perhaps that's because I spent a fair chunk of time in academia studying CS. :-)
  • Giao
    Giao over 14 years
    @asveikau That's a statement to print out and put up on a wall!
  • Cliff
    Cliff over 14 years
    I totally disagree with this comment. There is a large amount of behavior code that you can share across platform. Using an approach such as TDD would force you to separate the platform code from the business logic. Also there is a wide open market for consistent apps that work equally across handsets.
  • SinisterMJ
    SinisterMJ over 14 years
    In my experience the behavior code is so marginal that it's not worth hamstringing the whole project to just share what amounts to a few pages of psudeocode. You can keep apps consistent without sharing code, even moreso if you are willing to front a lot of automated tests to ensure consistency. The reason to keep the code separate is one question: Do you want your application to be consistent, or great on whatever platform it runs on? You cannot have both. Apps that run the same across all platforms always have areas they could be improved.
  • Warren  P
    Warren P about 14 years
    Go with this, and get your store blocked from the AppStore. Apple doesn't want you going this way. The original question asker apparently already knew that too, and that's why he thought of the "okay, so we'll use Objective C everywhere" idea, which is also unworkable. Apple's restriction requires that your app be written "originally" in some combination of C, C++, and Objective C. Since C libraries are allowed, I don't see why the original asker would care at all about using ObjectiveC anywhere other than iPhone. Thus the question is a bit of an unusual one.
  • Ivan Vučica
    Ivan Vučica about 13 years
    @Warren P: I know this is an old comment, but for future readers, I'd like to note that this is no longer so. Neither Unity nor Mono have had problems with Apple. Apple's restriction was targeted at Adobe Flash, and that was about it. Rule was also lifted.
  • Warren  P
    Warren P about 13 years
    That's correct. Apple has relaxed a LOT since then. They relaxed not only mono (runtime) and language (other than Objective-C) questions, they also allowed emulators (like C=64) back in, and lots more. My comment was correct when it was written, and could be correct again, in the future, if the winds at Apple shift again.
  • Prof. Falken
    Prof. Falken over 12 years
    Smalltalk and Objective C are both great and should be used, Apple libs or no.
  • andyvn22
    andyvn22 over 12 years
    Some types of apps, especially games, are almost all behavior that could be cross-platform and needs to be consistent. The UI should then be grafted atop that in a platform-specific implementation.
  • SinisterMJ
    SinisterMJ over 12 years
    But you can define behavior at a meta level (usually in some kind of domain specific language) while having engines to process the behavior that vary by platform.
  • Frederik
    Frederik almost 5 years
    You can now use the GNUstep Android toolchain to set up a project as described above and use Objective C model code based on Foundation and CoreFoundation in an Android app.
  • Frederik
    Frederik over 4 years
    While Apportable no longer exists, the GNUstep Android Toolchain provides a similar solution to enable using Objective-C on Android using Foundation, CoreFoundation, and libdispatch.
  • Frederik
    Frederik over 4 years
    You’re right that gcc and llvm support Objective-C on Android as well. In addition to the compiler you’ll also need the Objective-C runtime, which exists open-source as libobjc2 and works on Android. With this, and using GNUstep as an implementation of the Objective-C frameworks (i.e. Foundation) you can absolutely use Objective-C code on Android. The GNUstep Android Toolchain provides a full setup for this.
  • Paul Beusterien
    Paul Beusterien over 4 years
    Still some artifacts from Apportable available on GitHub - github.com/apportable/Foundation and github.com/apportable
  • Frederik
    Frederik over 4 years
    True, but afaik not the toolchain to enable compilation of Objective-C code on Android.
  • Paul Beusterien
    Paul Beusterien over 4 years
    Likely has rotted - but github.com/phausler/clang