Mirko
Mirko

Reputation: 85

Cannot resolve corresponding JNI function with .so file

I'm completely new to JNI and I have to use an existing compiled .so file. The problem is that Android Studio isn't able to find JNI fuctions. I load my library with a code similiar to this:

public class MyService extends Service{

    static {
        System.loadLibrary("mylib");
    }

    private native void example();

}

and it gives me an error "Cannot resolve corresponding JNI function Java_com_company_app_MyService_example".

If I run this command (in a terminal)

arm-linux-androideabi-nm -DC mylib.so

I get, between others (I need only few functions of the library),

register_com_company_app_service(_JNIEnv*)

(instead of MyService there is service)

What can I do? Are there problems loading the library or what else?

I tried also to refractor class name "MyService" in "service" but "Java_com_company_app_service_example" wasn't resolved either!

Thanks in advance!

Upvotes: 2

Views: 8802

Answers (1)

Yochai Timmer
Yochai Timmer

Reputation: 49261

You need to keep in mind that the function signature must match its name in Java, and that it must be exported.
Also, member functions receive the object as a first parameter, and static functions receive the class.

#ifdef __cplusplus
extern "C" {
#endif
 JNIEXPORT void JNICALL Java_Namespace_Class_MemberFunction
        (JNIEnv *, jobject);

 JNIEXPORT void JNICALL Java_Namespace_Class_StaticFunction
        (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif

So in your case it would be:

 JNIEXPORT void JNICALL Java_com_company_app_MyService_example
        (JNIEnv *, jobject);

Upvotes: 3

Related Questions