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:
- Blank slate new project, knowing ahead of time there is a large chunk of reusable logic that needs to run on each device.
- Existing iPhone code base, porting of C, C++ and Objective-C to the Android NDK or otherwise.
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. 开发者_StackOverflow社区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.
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.
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.
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.
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.
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.
XMLVM looks worth a try.
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.
Moai is 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.
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.
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});
}
}
You could also have a look at Titanium. The say with their tool you can write your app in abstract web code and they will help you web app interface with all those gps, accelerometer sensors etc.
If you have an existing iOS code base in Objective C, you can use the GNUstep Android toolchain to 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).
精彩评论