Search code examples
javaandroidc++java-native-interface

How to include unmodifiable native C++ in Android with JNI?


We're exploring including some existing C++ library in an Android project and was recommended to use JNI. Most references I've seen suggest something like this: https://medium.com/@ssaurel/create-your-first-jni-application-on-android-with-the-ndk-5f149508fb12 where you generate the native headers and code to interface with the Java.

Assuming you can't modify the native libraries, is this still the best way? In that case, would you create an additional native shim layer that calls out to the existing native libs and just defines the interfaces?


Solution

  • Yes. If you can't modify the library, you're going to have to create a wrapper layer. Especially if its C++, since you'd need the JNI layer to be extern "C" to avoid name mangling.