java 跨平台 iPhone/Android 代码共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2380258/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:52:40  来源:igfitidea点击:

Crossplatform iPhone / Android code sharing

javac++iphoneobjective-candroid

提问by slf

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

简单地说:在 iPhone 和 Android 版本之间共享/重用代码的最有效方法是什么?

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 NDKor otherwise.
  1. 空白的新项目,提前知道有大量可重用逻辑需要在每个设备上运行。
  2. 现有的 iPhone 代码库,将 C、C++ 和 Objective-C 移植到Android NDK或其他方式。

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.

是的,当然,在完美的世界中,所有应用程序都将插入神奇的云中,所有可重用逻辑都将在 Google App Engine 或某些 Web 服务中运行,但这不是这个问题的精神。在体验了从 iPhone 到 Android 的移植而完全没有二手代码重用并看到这个人不得不忍受的痛苦之后,我想知道其他人是如何避免它的。

采纳答案by Goles

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.

根据我的经验,你可以使用 Android NDK 来编译 C 和 C++ ,所以如果你在 iPhone 中使用 iPhone Obj-C++ (.mm) 绑定作为 C++/C 引擎,而在 Android 中你使用 Java 绑定到同一个引擎,应该是完全可能的。

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

所以 C++/C 引擎(Android 和 iPhone 的代码库几乎相同)+ 薄绑定层 = 可移植代码。

回答by Hassan Syed

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

就像我之前告诉过一个类似问题的人一样,使用 MVC 并在 C++ 中实现 MC 并在 obj-c 或 Java 中实现 V。

回答by Tom Andersen

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.

尽可能用普通的旧 C(或 C++,如果需要)编写,并且只在 Android 和 iPhone 中包含相同的文件。也适用于 Windows/Mac。“跨平台”库往往会消耗你。

回答by maurice

You could have a go with Marmalade 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.

您可以尝试使用Marmalade SDK...一次编译到本机 ARM 并使用相同的二进制文件、标准 C/C++ 部署到 Android 和 iPhone,在 Windows 或 Mac 上开发,包括许多(可选)中间件技术。

回答by Tuomas Pelkonen

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.

我一直致力于使用 Lua 和我自己的框架为 iPhone 创建应用程序和游戏。通过这种方式,我最终可以使用 Android NDK 为 Android 实现相同的框架,但希望两个平台的实际应用程序代码完全相同。

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.

我不认为有一种简单的方法可以做到这一点,因为 API 显然不同,本地编程语言也不同,但我的建议是用两种平台都支持的任何语言构建自己的框架。也许已经有一个框架可以为您完成困难的工作?如果没有任何好的框架可以做到这一点,那么这显然是一个自己实施的机会。

回答by Victor Sergienko

XMLVMlooks worth a try.

XMLVM看起来值得一试。

回答by Jonas Bystr?m

Moaiis free for smaller projects, and used by big studios as well. I run homebrew myself, but if I didn't I would probably use Moai myself as it looks very promising. They claim it ports to Mac, PC, iOS, Android and I think even Kindle Fire.

Moai对于较小的项目是免费的,并且也被大型工作室使用。我自己运行自制软件,但如果我不这样做,我可能会自己使用 Moai,因为它看起来很有前途。他们声称它可以移植到 Mac、PC、iOS、Android,我认为甚至是 Kindle Fire。

You'd want to write the bulk of the code in Lua I guess, but you'd have access to all the code, so you can use C++ when you need it.

我猜您想用 Lua 编写大部分代码,但您可以访问所有代码,因此您可以在需要时使用 C++。

回答by Boris Rasin

You can use Scapix Language Bridgeto automatically bridge C++ to Java and ObjC/Swift (among other languages). Bridge code automatically generated on the fly directly from C++ header files. Here is an example:

您可以使用Scapix Language Bridge自动将 C++ 桥接到 Java 和 ObjC/Swift(以及其他语言)。桥接代码直接从 C++ 头文件动态生成。这是一个例子

Define your class in C++:

用 C++ 定义你的类:

#include <scapix/bridge/object.h>

class contact : public scapix::bridge::object<contact>
{
public:
    std::string name();
    void send_message(const std::string& msg, std::shared_ptr<contact> from);
    void add_tags(const std::vector<std::string>& tags);
    void add_friends(std::vector<std::shared_ptr<contact>> friends);
};

And call it from Swift:

并从 Swift 调用它:

class ViewController: UIViewController {
    func send(friend: Contact) {
        let c = Contact()

        contact.sendMessage("Hello", friend)
        contact.addTags(["a","b","c"])
        contact.addFriends([friend])
    }
}

And from Java:

来自Java:

class View {
    private contact = new Contact;

    public void send(Contact friend) {
        contact.sendMessage("Hello", friend);
        contact.addTags({"a","b","c"});
        contact.addFriends({friend});
    }
}

回答by rbgrn

I use BatteryTechfor my platform-abstraction stuff and my project structure looks like this:

我将BatteryTech用于我的平台抽象内容,我的项目结构如下所示:

On my PC:

在我的电脑上:

gamename - contains just the common code

游戏名称 - 只包含通用代码

gamename-android - holds mostly BatteryTech's android-specific code and Android config, builders point to gamename project for common code

gamename-android - 主要包含 BatteryTech 的 android 特定代码和 Android 配置,构建器指向 gamename 项目以获得通用代码

gamename-win32 - Just for building out to Windows, uses code from gamename project

gamename-win32 - 仅用于构建到 Windows,使用来自 gamename 项目的代码

On my Mac:

在我的 Mac 上:

gamename - contains just the common code

游戏名称 - 只包含通用代码

gamename-ios - The iPhone/iPad build, imports common code

gamename-ios - iPhone/iPad 构建,导入通用代码

gamename-osx - The OSX native build. imports common code.

gamename-osx - OSX 原生构建。导入通用代码。

And I use SVN to share between my PC and Mac. My only real problems are when I add classes to the common codebase in Windows and then update on the mac to pull them down from SVN. XCode doesn't have a way to automatically add them to the project without scripts, so I have to pull them in manually each time, which is a pain but isn't the end of the world.

我使用 SVN 在我的 PC 和 Mac 之间共享。我唯一真正的问题是当我将类添加到 Windows 中的公共代码库,然后在 mac 上更新以将它们从 SVN 中拉下来时。XCode 没有办法在没有脚本的情况下自动将它们添加到项目中,所以我每次都必须手动拉入它们,这很痛苦,但并不是世界末日。

All of this stuff comes with BatteryTech so it's easy to figure out once you get it.

所有这些东西都随 BatteryTech 一起提供,所以一旦你得到它就很容易弄清楚。

回答by Frederik

If you have an existing iOS code base in Objective C, you can use the GNUstep Android toolchainto use your existing model code based on Foundation and CoreFoundation in an Android app, and then write a new UI layer that interacts with the Objective C model via native NDK calls on top of that (e.g. in Android Studio).

如果你有 Objective C 中的现有 iOS 代码库,你可以使用GNUstep Android 工具链在 Android 应用程序中使用你现有的基于 Foundation 和 CoreFoundation 的模型代码,然后编写一个新的 UI 层,通过最重要的是本机 NDK 调用(例如在 Android Studio 中)。