March 12, 2014

Porting Cocos2d Game To Android With Apportable

Apportable allows developer to port their iOS application to Android easily with a very little alterations to the source code. Basically it cross-compiles Objective C or C++ code to Android NDK library. Also it includes libraries for emulating significant amount of iOS SDK APIs. Although Apportable supports UIKit to certain extent, it is intended for OpenGL applications only.

Recently, we have ported our old game title Dragons Rage to Android by means of Apportable platform. This took around 4 days total, which is pretty cool. Apportable is free if you are fine with supporting Android 4.0+ only, don't need support for expansion files and don't need or fine with implementing some useful modules like Facebook SDK, Flurry or Google Analytics yourself.

If your application uses third party library distibuted as a source code, you can try your luck and just compile it as your own code. However, if is a static library, you don't have other choice except for using its native Android version (if it exists of course). Although I would recommend this approach even if you managed to compile iOS version just fine. Since Apportable includes BridgeKit framework for interoperating with Java code, it is not as hard as it sounds.

For Dragons Rage we needed support for Chartboost SDK, which unfortunately is distributed as a static library. So we have to write some Chartboost support code for Android. It is available on github, feel free to use it for your projects.

If you want to port your own application, the first place to start is Apportable quick start guide. It is well-written, although fails to mention a few important things:

  1. If you plan to test your application in the emulator, it is crucial that the emulator device has "Use Host GPU" option turned on:

    Use Host GPU

    You will just get the black screen if you don't.

  2. Our application was built on cocos2d 1.0.1. I am not sure, if this is specific to this exact version. Loading texture in background with [[CCTextureCache sharedTextureCache] addImageAsync:] didn't work, causing application to crash, because of thread-related issues. As a workaround we have simply rewritten our code to load UIImage in background and the create texture in the foreground thread.

  3. Threading setup of the Apportable activity responsible for presenting is "a bit non-standard". Watch out, when trying to do something related to Android native views. In our Chartboost code we had to show interstitials on the separate activity, because Chartboost SDK didn't want to play nice with this kind of threading setup.

  4. Unlike iPhones and iPads, Android devices come in variety of screen resolutions. With Apportable you can have some control over the way your game adapts to them by setting [UIScreen mainScreen].currentMode. UIScreenMode is poorly documented, but a bit of experimenting will get you the desired results. It is set to UIScreenBestEmulatedMode by default, which worked just fine for Dragons Rage, although we had to drop all our iPad-related layouts code.

So to summarize, Apportable is a nice project, which can be used to port your iOS games or other kind of OpenGL applications quickly. This comes at a cost of additional 10 Mb of libraries added to resulting APK and quite a sum of money if you want advanced features, e.g. Android 2.x support.

We would love to hear from you.

This form sends a email right into our inbox. One of us will get back to you as soon as possible.

Thanks for contacting us!

We will respond as soon as possible - normally, within a day.

Want to get in touch even faster? Then, please, try:

Calling us at +1-424-777-2422.

Skyping factorial.complexity.

Something went wrong :(

We are very sorry. Here are some alternative ways to contact us:

Want to get in touch even faster? Then, please, try:

Phone +1-424-777-2422

Skype factorial.complexity

Email info@f17y.com