2

I have an archive file which contains a number of functions which are built in.

Having read the android ndk documentation I assumed that upon System.loadLibrary() in the Java class I would have access to the functions from the archive file.

I am not having access to the .cpp files so I am using only the .a + .h(header file) .

I am still new to this so some help would be appreciated.

Thank you

Mr. Supasheva
  • 311
  • 1
  • 4
  • 14
  • You can't. You can load a .so file that's designed for JNI. – user253751 Oct 11 '16 at 09:59
  • Interesting. In that case, can i convert the archive to a .so file? – Mr. Supasheva Oct 11 '16 at 10:07
  • The easiest thing to do would be to recompile and re-link it as a shared object (.so). – user253751 Oct 11 '16 at 10:08
  • Thats the issue. I don't have access to the original .cpp files. I've been told to make use of the archive file alone in addition to the header file – Mr. Supasheva Oct 11 '16 at 10:10
  • Then there's probably a way to convert it but I don't know what that is. Plus it needs to be specifically written as JNI, or you need to write JNI wrapper functions, because Java can only call JNI functions. – user253751 Oct 11 '16 at 10:11
  • Just a follow up... Is there a way I should be loading the objs folder? I read somewhere that when the nkd-build command is called it compiles the static library into that folder. – Mr. Supasheva Oct 11 '16 at 10:48

2 Answers2

1

You have Java and you have Native (binary + .h file)

Now you need JNI (https://en.wikipedia.org/wiki/Java_Native_Interface)

JNI will helps you making call to native function (in .a file) from Java

Akivamu
  • 550
  • 5
  • 17
  • Thats great. Unfortunately in my research most of the examples seem to lean towards .so files though. If you have any you can share I would be most grateful – Mr. Supasheva Oct 11 '16 at 10:09
  • You mean load static lib (.a file) right? It's been a while since I worked with JNI, I'm not sure by now. But here some links I just found: http://stackoverflow.com/questions/24493337/linking-static-library-with-jni http://stackoverflow.com/questions/2943828/how-to-compile-a-static-library-using-the-android-ndk – Akivamu Oct 11 '16 at 10:27
0

This sample uses 2 libs: one static, the other one shared lib. It is just a framework for how to use external ( 3rd party ) libs in android platform, which might be close to what you are at. System.LoadLibrary() will load shared lib, which means you will need create your own wrapper for your static lib:

  • Create your java apps
  • Add java wrapper function (s) for your native function (s)
  • Generate your native function(s) prototype with javah or let android studio auto generate them for you ( right click on the java function, select generate prototype ); those functions have some special signature so they could be called by your java side code (you will see it when you get there ). Remember to add JNIEXPORT JNICALL to them (manually add them into your function if Android Studio forgets about it)
  • Call your lib functions inside your archive ( *.a, static lib) from your generated native function(s)

some samples could be found in the sample repo, but you will need to decide whether to use ndk-build or cmake to build your native C/C++ code. It is better to use CMake for new projects.

Gerry
  • 1,223
  • 9
  • 17