1

I am compiling a c++ library to be used on my Android device.

In compiling the library I did not take into account the architecture I was building the library for.

As a result I have a 64 bit dynamically linked shared library x86_64 which only works on 64 bit systems.

I intend to link this library to my android device using the JNA tool.

What is the appropriate way to compile my c++ library for android architecture and JNA.

Mr. Supasheva
  • 311
  • 1
  • 4
  • 14

1 Answers1

1

You must use the Android NDK.

Depending on the target architecture, you must select the appropriate toolchain/cross-compiler, e.g. ARM, MIPS or x86.

See also Getting Started with the NDK


JNI or JNA have nothing to do with how the library is built. You must build the library for your target architecture, so it can be used on your phone or tablet.

To access this library from Java, you may use either JNI or JNA.

With JNI, you must implement glue code in C/C++. With JNA on the other side, you do more or less the same, but you use an existing library (libffi) and implement the glue code in Java. This is done dynamically at runtime and may be thought of something like reflection for a library.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • I see. That would entail wrapping my cpp functions with JNI yes? – Mr. Supasheva Oct 19 '16 at 09:44
  • I was hoping to use JNA instead – Mr. Supasheva Oct 19 '16 at 09:44
  • JNA will work if your library exposes a standard C-compatible API. If you are using C++, you need to wrap exposed functions with `extern "C"` and avoid using any C++-specific classes or data types. – technomage Oct 19 '16 at 19:50
  • Thank you @technomage That was a big help. Only issue is that the app works fine on the simulator but not a physical android device. I keep getting this error java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-arm/libjnidispatch.so) not found in resource path (.) – Mr. Supasheva Oct 20 '16 at 12:41
  • Maybe these help https://github.com/java-native-access/jna/issues/268, http://stackoverflow.com/q/36305914/1741542 – Olaf Dietsche Oct 20 '16 at 13:39
  • As noted by @OlafDietsche, you need to add JNA's native library (libjnidispatch.so) to your Android project just like your other native libraries. – technomage Oct 21 '16 at 12:56