开发者

Are there any huge differences between objective-c and Java, or iPhone and Android?

Edit: My bad, I meant objective-c, not c#. Some reason I got it into my head it was c# the iphone used. So the answers for c# were great, thanks, but theyre a bit irrelevant, sorry about that.

I've had a look but can't find anything that answers this, though a few have shortened the question by answering parts of it. Between a small group, we were planning on doing some work on iPhone and Android, the 2 seperate for the most part but helping each other out, and with some guys doing graphics work split between them.

But we were thinking about the possibilities of moving things between the two, not necessarily apps, maybe jus开发者_JAVA百科t useful classes or something. Looking at objective-c and Java, they seem to have about the same features that the biggest obstacle would be system interface stuff, so we were wondering whether, if we created an abstraction over these on each system so they could be given the same input (which unless I'm wrong wouldn't put too much strain on the system?), would there be any problems in writing something to convert between objective-c and Java, worse than the locations of methods in the sdks? Or are there key features or something in one language that the other doesn't have which we've missed that would mean the only way to do it would be rewriting from scratch.


I wouldn't waste time trying to find commonality between iOS and Android.

Cross-platform is almost always a waste of time and resources unless the cross-platform capability is central to the apps functioning. That is especially true for platforms such as iPhone and Android which have custom OS and work very tightly with the hardware.

Cross-platform development environments add rather than reduce complexity long term. Yeah, it sounds neat but usually you get 90% what you want easily and then you hit a roadblock that destroys all the savings you made and then starts putting you in the hole. There are simply to many compromises and square pegs jammed into round holes.

Unless your app could in theory work from a generic web page, cross-platform is not for you.

In the specific case of Objective-C and Java, although Java is descended from Objective-C they have no modern interoperability. You can't use code from one on the other.

You should spend the time to learn each platform's specific API. There are no shortcuts.


Tools like XMLVM, Codename One and iSpectrum tell us that you can actually cross-compile Java code (from an Android app or creating one from scratch) to Objective-C code that is running on an iPhone without having to install any virtual machine on the Apple side of things.

I suggest watching Developing iPhone Applications using Java

Unfortunately Apple's license agreement for the iPhone SDK prohibits the porting of the Java virtual machine to the iPhone. In this presentation we introduce an Open Source Java-to-Objective-C cross-compiler as well as a Java-based implementation of the Cocoa library. With the help of these tools, iPhone applications can be written in pure Java. Using the Java version of Cocoa, it is possible to run a Java-based iPhone application as a Java desktop/applet application that can be cross-compiled to run natively on the iPhone. The talk will discuss the challenges of the Java-to-Objective-C cross-compiler as well as the Java-based version of Cocoa. Details are available at http://www.xmlvm.org/

and for more insight a more recent talk about Cross-Compiling Android applications to the iPhone from Frebruary 2010 is available here, which is documented at http://www.xmlvm.org/android/ :

Android is an Open Source platform for mobile devices. Initiated by Google, Android has received much attention. Android applications are developed using Java, although a special compiler converts class files to a proprietary, register-based virtual machine that is used on Android devices to execute applications. Android defines its own API for writing mobile applications. With the help of XMLVM it is possible to cross-compile Java-based Android applications to native iPhone applications.

The Android application is written in Java and makes use of an Android specific API. XMLVM offers a compatibility library, written in Java, that offers the same API as Android, but only makes use of the Java-based API for Cocoa Touch. During the cross-compilation process, both the application and the Android compatibility library are cross-compiled from Java to Objective-C and linked with the Cocoa Touch compatibility library to yield a native iPhone application.

From the 2008 talk about building Java applications for the iPhone http://www.xmlvm.org/iphone/ :

Apple's iPhone has generated huge interest amongst users and developers alike. Like MacOS X, the iPhone development environment is based on Objective-C as the development language and Cocoa for the GUI library. The iPhone SDK license agreement does not permit the development of a virtual machine. Using XMLVM, we circumvent this problem by cross-compiling Java to the iPhone. Just like a Java application can be cross-compiled to AJAX, XMLVM can be used to cross-compile a Java application to Objective-C. The cross-compilation is also accomplished by mimicking a stack-based machine in Objective-C.


If you focus on abstracting away things like system interactions, and stick to the common subset of the languages, you can probably build classes that would require little or no modification to move from one language to the other.

C# does have a better implementation of generics than Java, in that C#'s generics retain strong typing at run-time.

C# also has LINQ, which as far as I know has no equivalent (yet) in Java. LINQ provides a SQL-like query capability, built into the language and fully supported by the .NET frameowrk, that lets you query object collections, and does so in a very functional-language style. LINQ can be extended by query providers to work with other data formats, such as XML and SQL, once the query provider provides an object model that translates to the other format. LINQ also makes parallelization very easy for multi-core work. It's a huge extension to language capability.


This topic is often filled with the dogma of technology jihadis, so I shall try and steer clear of that in my answer.

In my experience, I would largely agree with people saying that trying to share code between the two platforms would be difficult. However, there are some important exceptions:

I would consider cross-platform development where your business logic:

  • is non-trivial;
  • should be standardized across platforms; and
  • has well defined interactions with the outside world (e.g. network stack or UI).
  • (bonus) is written already.

Apple is the rate determining step here, given the now infamous 3.3.1 restrictions on source code languages. You can write code in Javascript according to the bundled Webkit, C, C++ and Objective C.

If you don't want to install extra languages on a 'droid, you can use the Java that compiles down to dalvik code, the Javascript as per the slightly different build of webkit or something from the NDK. Then you'll be looking at C/C++. You could cross-compile Objective-C, but I haven't had any experience of that.

The clean separation of business logic from UI and networking is important, as you'll be looking to write adapters for the networking layer, something else for the UI.

I would not attempt to write cross platform UI code in C/C++, and would either write something that used HTML/CSS/JS, or more likely write something completely custom to take advantage of the different UI metaphors on each platforms - e.g. there is no analogue to the notification bar on the iPhone. Animation on the iPhone is orders of magnitude simpler to implement than on Android.

If you don't need the UI to be massively integrated with the OS, then a webview and some HTML5 may be sufficient. Titanium is a good option here, and my colleagues in the know tell me it is better (i.e. compiles, not interpreted) than PhoneGap. Again I don't know.

Going the other way in complexity, Open GLES is available on both platforms.

It should also be noted that SVG is not available on the current crop of Android OSes.


If you actually want to do cross phone development, as long as it is not something really computationally expensive, I would probably go with PhoneGap.

PhoneGap is an open source development framework for building cross-platform mobile apps. Build apps in HTML and JavaScript and still take advantage of core features in iPhone/iTouch, iPad, Google Android, Palm, Symbian and Blackberry SDKs.

As far as I can tell, it's pretty much the only cross platform toolkit that is allowed in the iPhone app store.

If you're dead set on native development, I think you'll find that unless you're making a game and targeting c, it will be a lot of work to port. The frameworks are very different and the way of building UI is very different. That said, to answer you're direct question of objective-c vs. java, it's not that bad. It is different enough, though, that an abstraction tool would not be that easy - not like Java vs C#. Objective-c does not have garbage collection (on the iPhone anyway), for example, and the syntax is very different. Objective-c is more loosely typed in the way that it does method calls (messages in objective-c). That said, they aren't wildly different in terms of programming paradigms. They are both object oriented imperative languages. They both have classes and methods, public and private. If you wanted to port the code by hand, it wouldn't be the end of the world, I just think you might wind up spending a lot more time trying to build an abstraction layer.


If you're thinking about doing Android development my best advice is to go for java. There is no benefit at all of writing a complete abstraction (assuming you can) layer to convert from C# to Java. As for the IPhone development dont doubt about using Objective-C for the same purpose.


You can't develop for the iPhone in the same language as for Android. For the iPhone, you can only program in Objective C, it's developed by Apple. You can't use it for android, and the only way to do iPhone development is with that language (it's in the user agreement).

As for C# vs. Java, the whole principle is different. Java is one language for every platform, .Net is one platform for every language. They are not compatible. You can use Java on Android, but I'm not sure if you can use C# (.Net) as well.

Anyway, you'll have to build two different apps for these two mobile operating systems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜