Android Game Development : from Java to C++ (with cocos2d-x)


In a previous post (here), I was talking about my progress with libgdx, a Java library for Android (and other platforms). Shortly after writing this article, I realized that there is a big disadvantage that I forgot to talk about. It’s so big that it made me stop using libgdx : It is not portable on iOS. I knew this from the beginning but didn’t really think about using anything else than Java. I finally realized that while porting from one language to another is easy, it can take a lot of time.


I searched for multiplatform libraries (Android and iOS). As the game I’m porting (Don’t Feed the Trolls) was developed with XNA, I looked at C# solutions. There is at least one but it’s too expensive (I previously linked to it but I was asked to remove the link by the company, which I did). I’ve been doing C++ indie/casual games in the past and I develop in C++ in my day job, so this is the obvious choice. I like the efficiency of languages such as C# or Java (easy to write and debug), but if I want to avoid porting the game another time for the iPhone, I should to use C++. There are quite some libraries in C++ (such as Marmalade). I finally decided to use cocos2d-x. This is a C++ port of the famous cocos2d library for iPhone which is in Objective C. Cocos2d-x has an active community, some multiplatform games have been released already, it’s free and open source. All good stuff!

C++ on Android

I am mostly working using the Windows build of the library but I also did some work on Android. Mixing C++ and Java (using JNI : Java Native Interface) is doable, but really not user friendly. Debugging is terrible, building is slow, you have to install tools such as cygwin, use makefiles (how old school!), and so on. It really does not seem the right solution at first. However, once everything is set up correctly, it’s working fine. Cocos2d-x has good tutorials about this. I develop with Visual C++ 2010 Express on the Windows build, I have all the great tools to debug generic problems. The only real troubles are when you have android-specific bugs in the C++ code but it’s really not frequent. And there are a few ways to debug it with gdb and gdb-server (I never had to use it yet). When you have a crash, there is a very useful tool in the NDK called addr2line (here’s how to use it) that allows you to get the callstack. It’s usually enough to understand and fix the bug. You also can still use old-school debugging techniques such as logging to the output 😉 Hopefully, most of my bugs are multiplatform, or in Java code, so I can fix them easily.

Java is still needed

To develop Android-specific code, you still need Java. For example, I use Java for AdMob integration, In-App Purchases or the Facebook SDK for instance. Setting up Java functions called from C++ is not very intuitive, so I opted for a very simple and centralized way: I have one Java function that can be called from C++ with two String parameters. In the Java code, I read these parameters that contains all I need to know to call the specific Java code.


Even though libgdx is really a great lib, after a few weeks working on C++, I’m confident that I made the right choice with cocos2d-x. I did not start developing on iPhone already but it should be quite fast compared to porting Java to C++ (or worse, Objective-C!). I have a working Android version of my game and the performances seems good.

Which language do you use to build your multiplatform mobile games? Do you know other good C++ solutions? Feel free to comment!

Follow me on twitter, facebook or google+ for more game development information.


  1. I am also using Cocos2d-x and I think that it is a really good game framework for mobile devices. The only caveat is the fact that it is primarily focused on mobile platforms: iOS and Android (and just recently Blackberry?)

    If it officially supported desktop: Mac, Windows, Linux it would be even better.

    Just curious…. any reason you decided to go with cocos2d-x and C++ instead of libgdx and Java?

  2. About the “C++ on Android” part.

    You should switch to Linux. I did it. Took me a month to set up, because I’m all new to pretty much everything and I didn’t really understand the tutorials. Though that was partly their fault, they just weren’t very descriptive and were written in you know, “bad English”.

    So and now that I’ve finally got everything going, while learning C++, it’s like slicing through liquid butter. Compiling is MUCH faster, debugging is, bit better, but still not very good. But it’s due to c2d-x’s nature, but it does pseudo-stack-trace if you’re debugging on device, which is neat!

    It’s kind of officially supported on linux, because the libraries it needs are mostly there, and if they’re needed it’s less then few MB and available for almost every distro.

    And you can develop on iOS at the same time. The best thing is testing. You don’t have to open up emulator/send to device every time you need to test something, just ctrl+F11 and you’re right in just like it would be on a real device. Compatibility with android is 99% you just sometimes need to build manually to ensure all resources get packed up and sometimes you need to add cpp files manually. Compatibility with iOS is 98%, you just need to switch2mac to compile and edit a few settings manually, nothing longer the 10 seconds and 5 minutes of research.

    And what’s even better is the speed. At least in comparison with Java… In short, 20% more particles for free. Like?

    By the way your game is stupid like hell =) Death to trolls!

  3. I ran into somewhat of the same issue. I was attracted to GDX because of the promise of developing on the desktop and then with a snap and a click, having the same thing compile seamlessly to the phone.

    I started out using AndEngine and started shopping around after I learned of a HTC related bug (which AndEngine was affected by) that made VBO calls screw with the GC through java. I assumed GDX wouldn’t have this issue but it turned out it suffered from the same problem, and thus my game (even very early on in porting) ran really slow. So, I thought okay, if I go C++ this MUST solve the problem. Why would I think that? Because I’m bypassing the entire OS essentially and going directly to the hardware.

    I started writing my own cross platform engine in C++ and sure enough when I had completed enough code to run some tests everything worked out great. I was able to, on my HTC desire Z, get a test running of 170 characters/players, all equally using 4 different textures (so rebinding 4 times per render), and animating running at 60 fps. The textures are 512×512 and there’s 15 frames per texture which are precalculated and stored in a VBO as texture coordinates. Even AndEngine using direct buffers would have choked on that a little and GDX was dying with just 20 of these all of the same texture (due to the bug).

    My point to this whole story: it’s been a very painful experience, and I’ve had to learn A LOT. My development time has pretty much quadrupled BUT… it’s all worth it because by going lower-level with C++ I am now confident that when I deploy my game(s), I’ll get maximum performance and compatibility (by not actually caring about what OS I’m on). Also for those who are interested, GDX is nearly done being ported to C++ with back end support for android and linux, support for windows and iOS in the works:

    I’ll also be releasing my engine when it’s polished and my game is released under the Mozilla Public License, or possibly under a Tri-License MPL/GPL/LGPL but not sure on the latter.

  4. Thanks Jesse for giving your experience. Do not forget to tell me when your engine is released. I also thought about using no engine and do everything by myself. But I finally like using engine such as cocos2d-x because there are many useful feature that save a lot of time and can greatly improve a game. I really like the actions of cocos2d, for instance, even though it’s really different from my graphics coding habits.

Comments are closed.