0

I am trying to use the .so external library in my Android project. It's not clear to me how can I access the classes in the library once it's loaded via System.load("path to lib")?

The library is already there, and it's already compiled for use on mobile devices. The loading process does not cause errors. Due to the specifics of the project, I cannot use .so files from the unpacked apk along the standard path - I need to connect it in the code via System.load("path to lib").

Please, tell me, what is the syntax for referring to classes in a loaded library?

2 Answers 2

0

Create method in one of the your class like below

public static native String methodName();

Now you can access classes in cpp file using below syntax

Java_your_package_name_classname_methodName(JNIEnv *env, jclass clazz) {
  return something
}

Get your something by calling methodName. Thats it.

for more information refer https://developer.android.com/studio/projects/add-native-code

0

Android can load arbitrary .so libraries via System.load() that is correct (if the architecture is correct). But that a library can be loaded does not mean you can use it from within your app.

Android bases on Java and Java uses a special system for calling methods in native libraries/.so files: Java Native Interface (JNI).

Based on the Java method name and it's parameters JNI defines the function name and it's parameters in the .so file and exactly this way you have to implement this function. JNI also provides helper functions for converting typical Java types into C types and the other way around.

What does that mean for you:

Assuming the library is not prepared for JNI and you have the source code of the used library you can add the necessary JNI functions yourself.

Otherwise you don't have the source code of the used .so library and the library does not provide the required JNI functions you have to develop your own .so library that acts as a bridge from Java-JNI to the exported functions of the .so file you want to call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.