My problem is as follows.
I need to create a library in Java. So far so good. Like any decent Java Libraries, it must be usable on any JVM.
But the difficulty here is that this library will contain a lot of native code.
I have some knowledge in JNI
.
I've tried creating a few native
methods in Java in an Android application with Android Studio
and with the Android NDK
.
That's great I can do a lot of HelloWorld examples. But how can I export this as a library ?
My project needs external C++ Libraries. I would like to wrap these libraries and use them in a JNI wrapper wrapped inside a Java Library.
To make things even simpler, take a simple HelloWorld JNI API for example.
Create a Java Desktop App (or anything else). Import the Helloworld JNI API (as jar), call String HelloWorldJNI_API.sayHello(String yourName)
and print the result.
What is required here :
- The JNI API will obviously declare the sayHello() method as a native method ;
- The argument (
String yourName
) is sent to JNI ; - The JNI code calls an internal awesome C++ so library that crunches the data and returns a "hello" + "yourName" (awesome, right ?)
- The JNI code returns the result as a
jstring
- Finally, the Java API returns the result as a
String
and voila !
This simple example should show you what I am trying to do.