10

I am using LibGDX to write apps for both Android and iOS and I want to be able to add C++ code to my apps to optimize certain parts and to port some functions etc.

I have been searching the internet and tried to follow some tutorials, but did not find what I need.

How can I write a very basic C++ library which I can load in LibGDX? Which tools do I need to use? Visual Studio? I develop in Android Studio.

I think that I need an .so file for Android and an .a file for iOS, is that correct?

Z0q
  • 1,689
  • 3
  • 28
  • 57

1 Answers1

6

On both platforms, it's possible to include a precompiled library as well as C++ source code directly.

On Android, you'll want to look into using the Android NDK. This allows you to include native C/C++ code that can bridge over to Java. The connection between Java and C/C++ is managed with the JNI. It's a fairly tedious, awkward system for communicating between C++ and Java. You'll want to look into setting up an Android.mk makefile that specifies how to include your library (or source code) into your build.

On iOS, it's a little more tightly linked. You can have Objective-C++ files that can run both C++ and Objective-C code. If you're using Swift, it's a little different (bridging between Objective-C++ and Swift).

In some cases, when the platform (Android/iOS) provides functionality that is superior to what is possible or realistic with C++, you might find yourself architecting the code such that your C++ can reach out to the platform as needed. This means that you might have headers with separate implementation files per platform.

  • thing.h
  • thing_android.cpp
  • thing_ios.mm

The android app's Android.mk file will include thing_android.cpp (but not thing_ios.mm). This file could cross the JNI bridge to talk to Java as needed, whenever you need something from Android SDK.

The iOS app will include thing_ios.mm (but not thing_android.cpp). The .mm extension signifies Objective-C++, so that file could directly call powerful Cocoa libraries as needed.

Finally, on all platforms, you'll want to be sure to either scale back your usage of C++ to the lowest common denominator platform. In other words, if iOS supports a particular feature of C++, and Android doesn't, then you cannot use that particular feature.

drhr
  • 2,261
  • 2
  • 17
  • 35
  • Thank you for the extensive answer. I read about the NDK toolchain. Is it possible to use Visual Studio 2015 to write a cross-platform library for Android / iOS (which compiles to `.so`, and `.a`? Or what would you recommend? – Z0q Mar 11 '16 at 11:33
  • 1
    I don't see why you couldn't do that - for some companies that want to keep their library source closed, that is a standard practice. I happened to use Xcode, for the convenience of testing the iOS app directly and making rapid changes to both the Objective-C and C++ source code, but in that same vein I also compiled the C++ code directly into the Android build, which would report compilation issues up front. – drhr Mar 11 '16 at 23:32
  • 1
    Thank you. How do I compile to `.so` and `.a`? – Z0q Mar 15 '16 at 15:57
  • 1
    Depends on where you write the code and what your preferences are. If you're on OSX, I'd recommend using Xcode. You can set up a project that is intended to output a `.a` static library. (iOS can't do shared objects). You could have a build target specifically for unit tests, if you wanted. And then finally, in the iOS and Android app projects, you'd want to include the static library (with Xcode via the project explorer gui on the left side, and with Android via the Android.mk makefile). – drhr Mar 15 '16 at 20:57
  • 1
    Also, with Android Studio, you might want to look into how to configure the Android.mk makefile contents via Gradle instead. That way, you can keep your android app configuration centralized with the powerful gradle config file. – drhr Mar 15 '16 at 21:02
  • 1
    And another improvement would be having the iOS and Android apps' build processes include compilation of the static lib, treating it as a direct dependency, and making the library update process more automatic during development. I'm not exactly sure to what extent gradle will monitor that dependency and whether it would get built every single time you compile the Android project... but I'll stop here and leave you with those thoughts to begin searching or asking new SO questions. – drhr Mar 15 '16 at 21:06