Crossplatform iPhone / Android code sharing

前端 未结 11 1457
北海茫月
北海茫月 2020-12-01 01:51

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

The two most common scenarios I think would be:

  1. Bla
相关标签:
11条回答
  • 2020-12-01 02:25

    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.

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

    0 讨论(0)
  • 2020-12-01 02:27

    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.

    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.

    0 讨论(0)
  • 2020-12-01 02:32

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

    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-win32 - Just for building out to Windows, uses code from gamename project

    On my Mac:

    gamename - contains just the common code

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

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

    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.

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

    0 讨论(0)
  • 2020-12-01 02:33

    You can use Scapix Language Bridge to 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:

    Define your class in 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:

    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:

    class View {
        private contact = new Contact;
    
        public void send(Contact friend) {
            contact.sendMessage("Hello", friend);
            contact.addTags({"a","b","c"});
            contact.addFriends({friend});
        }
    }
    
    0 讨论(0)
  • 2020-12-01 02:37

    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.

    0 讨论(0)
  • 2020-12-01 02:38

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

    0 讨论(0)
提交回复
热议问题