Java Objective-C 和 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2394236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C and Android
提问by Tom R
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).
我刚刚完成了一个相对较大的 Android 项目,它在我的嘴里留下了苦涩的味道,因为它永远不会在太阳系这一侧最普遍的手机之一上运行(那个果味小俱乐部)。
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.
因此,对于我的下一个项目,我希望以一种使大多数组件可以在 iPhone 和 Android 平台之间轻松传输的方式编写它。我想这样做的方法是用 Objective-C 编写大部分代码,然后分别在更多的 Objective-C 和 Java 中添加特定于平台的部分。在 Android 方面,这将需要使用 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?
我对C的了解不错,但是我对Objective-C的了解接近于零,我没有学习C++的欲望。上面的方法有多理智,有没有更好的方法?有什么方法可以让我用 Java 编写代码并仍然进入未破解的 iPhone 市场?我认识的人(iPhone 用户)在明年拥有一部 Android 手机的可能性有多大?
回答by asveikau
My guess, which has no experience to back it up, is that you probably couldwrite 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.
我的猜测,没有任何经验来支持它,你可能可以以某种方式用谷歌的 NDK 编写 Obj-C,因为 GCC 存在于 ARM,是开源的,有一个 Obj-C 编译器和一个基本的 Obj-C 运行时(如果它还没有可能被黑客入侵以在新架构上工作)等。
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 reallymean 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.
当然,“Obj-C”(没有NS
类)的意思与“Cocoa”非常不同,这是大多数人在说“Obj-C”时的真正含义。您也许可以重新使用一些 GNUstep 来解决某些问题,但是...老实说,我对此表示怀疑。听起来又像是很多工作。
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.
鉴于您所说的,如果我尝试这样做,我会尽可能多地用 C 编写您的核心逻辑,然后为每个平台用两个单独的 GUI 包装它。
回答by Chuck
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).
没有 Cocoa 的 Objective-C 不是那么有用,也不会让你更接近拥有一个有效的 iPhone 代码库。您可能最好使用 Core Foundation 用 C 编写核心,并使用 Java 或 Objective-C 作为平台特定部分。Apple 已将 Core Foundation 的一大块作为 CF-Lite 开源,并且它与 OS X 上的 Cocoa 免费桥接(即,您可以与 Cocoa 对应物互换使用许多 CF 类)。
回答by James Raybould
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.
我不确定 Android,但对于 iPhone,你基本上可以直接编写 C,只要你把它包装在 Objective-C 类中。
回答by Kendall Helmstetter Gelner
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.
UI 模型相当不同,组件也不同。最后,您可能能够共享的是数据对象类,可能是一些算法。甚至不像过去那样您实际上最终可以共享网络代码,因为您不是直接使用套接字,而是使用 HTTP 库。
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?
你为什么要写应用程序?让您或您的用户的生活更轻松?
回答by andyvn22
Others have said basically this, but I'd like to make it more explicit. Your best bet is probably to write:
其他人基本上已经说过了,但我想让它更明确。你最好的选择可能是写:
- Cross-platform data models & core logic, using:
- iPhone-only interface code, using Cocoa Touch (Obj-C)
- Android-only interface code, however they do it for the Android.
- 跨平台数据模型和核心逻辑,使用:
- 仅限 iPhone 的界面代码,使用 Cocoa Touch (Obj-C)
- 仅适用于 Android 的界面代码,但他们为 Android 执行此操作。
That's as close as you can get; any attempt to write cross-platform interfacecode will undoubtedly result in a mediocre app on both platforms. But making all the restof your code portable and just wrapping a device-specific interface around it is done all the timeand has been worked great for some iPhone developers.
这是你能得到的最接近的;任何编写跨平台界面代码的尝试无疑都会导致两个平台上的应用程序平庸。但是,让您的所有其余代码都具有可移植性,并且只围绕它包装一个特定于设备的接口一直都是这样做的,并且对一些 iPhone 开发人员来说效果很好。
回答by Ryan Ferretti
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.
从不同的角度来讨论这个问题...我知道你说你想尝试并坚持使用 Java,但如果你知道 C#,那么你可以使用 iPhone 的 MonoTouch 框架。Mono 本质上是 .Net 堆栈的开源实现。Mono 团队正致力于将 Mono 引入 Android,因此您基本上可以为您的业务逻辑编写一个共享的 C# 库,并且每个平台都有不同的视图/控制器。这当然都是用 C# 编写的,它有点贵,但它确实解决了用不同语言编写所有内容的问题。
I believe it is called MonoTouchon the iPhone and MonoDroid on Android.
我相信它在 iPhone 上被称为MonoTouch,在 Android 上被称为MonoDroid。
回答by wvdschel
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.
Objective-C 运行时尚未移植到 Android。这不应该是太多的工作,但是,如果没有该语言的工作知识,我怀疑你会很容易移植它。
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.
您正在尝试做的对于通用应用程序来说将是困难的,但如果您选择使用纯 C 语言开发游戏(Android NDK 和 iPhone 都支持),那么对于游戏来说应该是可能的。您必须编写一些胶水代码才能将来自 Obj-C 和 Java 环境的输入事件传递到您的 C 代码中,但这应该不是什么大问题——Objective-C 允许您直接调用您的 C 代码并且有很多示例项目可以为 Android 做到这一点。
回答by Yanik Magnan
I haven't tried this myself or finished watching the talk yet, but there is a Google Tech Talk on Developing iPhone Applications using Javaup on YouTube that looks pretty promising.
我自己还没有尝试过,也没有看完演讲,但是YouTube上有一个关于使用 Java 开发 iPhone 应用程序的谷歌技术演讲,看起来很有前景。
回答by Todd Moses
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.
如果您能等到今年晚些时候(确切时间未知),Adobe 将拥有适用于 Android 的 AIR 和适用于 iPhone 的编译器。因此,您可以在 AIR 中为 Android 编写应用程序,并使用大部分相同的代码编译到 iPhone。
http://labs.adobe.com/technologies/flashcs5/appsfor_iphone/
http://labs.adobe.com/technologies/flashcs5/appsfor_iphone/
Even if you can't wait see: http://www.insideria.com/2008/12/actionscript-to-cocoa---protot.htmlwhere it explains the similarities between ActionScript and Cocoa.
即使您迫不及待,请参阅:http: //www.insideria.com/2008/12/actionscript-to-cocoa---protot.html,其中解释了 ActionScript 和 Cocoa 之间的相似之处。
Also check out: http://labs.adobe.com/technologies/air2/for the AIR version capable of using the touch screen.
另请查看:http: //labs.adobe.com/technologies/air2/可以使用触摸屏的 AIR 版本。
So you can soon write once and deploy to Android and iPhone using ActionScript 3.
因此,您很快就可以编写一次并使用 ActionScript 3 部署到 Android 和 iPhone。
回答by wvdschel
XMLVM is a project which is capable of translating (some) Android applications to the iPhone. For more infromation, visit http://xmlvm.org/android/
XMLVM 是一个能够将(一些)Android 应用程序翻译成 iPhone 的项目。有关更多信息,请访问http://xmlvm.org/android/