December 8, 2016

Android Studio setup for Cocos2d-X 3.x

Keepers Game

I’ve been working on Cocos2d-X 3.x all of 2016. And boy, it’s a doozy! It’s great when it works and other times I am slapping Google like a piece of analog hardware - tap, tap, tap - are you working?!

It all started with the easiest of setups.

cocos new -p com.shipatwarp.game AwesomeInfinite

That set me up nicely with project.ios_mac and project.android for iOS and Android respectively.

Xcode compiled the game and brought it up on the simulator super smooth. For Android, project.android is actually an Eclipse based layout with ant build system. It then requires setting up the Google Play Services Library as a library project which is essentially a relative instead of absolute path. Which on a good day is maybe and on on a bad day - never. Anyhow, I carried on since it gave an APK that I could adb install. Days were happy in the Shire.

Then came the big day! Beta testing wrapped up. No crashes! Built an IAP and pushed to iTunes Connect. Wait for Apple approval. Built an APK and pushed to Android Console. And wait-a-minute! Did I read that right?! Google rejected the APK because of OpenSSL 1.01l compiled in somewhere. Anything less than 1.02f/1.01r carries a Logjam man-in-the-middle attack vulnerability. I didn’t make that up. Read it here.

What’s wrong with the Eclipse based android.project build you ask? Everything. Atleast everything now. Google moved the Android toolchain to Android Studio and Gradle. So if you’re needing latest support libs even for Google Analytics, then you have to use Android Studio. Cocos2d-x 3.9 didn’t support Android Studio very well, certainly not the latest android-ndk-r13b. I found out that aws-cpp-sdk was using the older version, so had to upgrade and compile against r13b. That meant that upgrading cocos version to 3.13.1, upgrading the NDK, upgrading all libs. Just upgrading. Ladies and Gentlemen, welcome to hell.

Much reading revealed following:

  1. The cocos command has an project.android-studio.
  2. The cocos compile uses ndk-build system which means the jni/Android.mk and jni/Application.mk are back in action.
  3. Setting up NDK build from within Android Studio to generate libcocos2dcpp.so is not easy. It wants to use CMake and which means porting Android.mk setup to the gradle task. I’m not sure what it means for Application.mk. For JNI/NDK builds, ndk-build option is also available but I didn’t go that route as what I did next worked.

This is the strategy I came up with:

  1. Download latest of everything
  2. Use Android Studio to setup the SDK packages, tools, NDK etc. Follow the steps here until NDK setup.
  3. Setup vars required by Cocos2d-X ie run setup.py.
  4. Now comes the hacker behavior ie break out the duct tape roll:
    • Create a new cocos project with same package name. We will be working with the project.android-studio directory.
    • Run Android Studio build from command line to make sure the hello world sources compile. The APK would work too. Here’s the command: cocos compile -p android --android-studio -m debug
    • Copy the project.android-studio to the original project’s root dir, replacing the one there.
    • Copy project.android/jni/Android.mk into project.android-studio/app/jni/Android.mk. Edit the paths in the latter to update to correct relative path eg ../../../Classes.
    • Copy project.android/jni/Application.mk into project.android-studio/app/jni/Application.mk. I didn’t have to mess with this one. YMMV.
    • Since we don’t have the Eclipse based library reference project anymore, what do we do? What do we do in gradle, kids? Bingo! add a line to dependencies in app/build.gradle. Much cleaner than relative paths. Here’s mine: compile 'com.google.android.gms:play-services-analytics:10.0.1'.

That’s it!

The nice thing is that I future proofed my Android builds since Google Play Services, AppCompat, Support Library are pretty much supported on the Gradle build system. Please feel free to drop me a line. And check out my game!

Keepers HD for iOS

Note to self: build a shortcode to display app store badges.

© Sumeet Parmar 2018

Powered by Hugo & Kiss.