Search code examples
androidc++cjava-native-interface

Calling function from .so file created in android in linux C++ code


I have .so file which I take from apk. With command nm -D libnative-lib.so I took list of functions. I need to call function named Java_com_example_nativelib_utils_Constants_getStringFromJNI . I wrote the code below:

#include <stddef.h>
#include <dlfcn.h>
#include <stdio.h>

int init_library() {
    void* hdl = dlopen("./libnative-lib.so", RTLD_LAZY);
    if(hdl == 0)
        return 0;
    else return 1;
}

int main() {
    printf("%s\n", (init_library() ? "OK": "ERROR"));
}

and make file:

all: exe

exe:    main.c
        gcc main.c -fPIC -ldl -o main

but result of program is always "ERROR". I tried libnative-lib.so from x86 and armeabi-v7a architectures (others not present). How to load library and call function from it? C++ or C do not care.


Solution

  • It is not possible to use android .so files in non-android project. similar question is here