JNI_OnLoad not found

2019-05-06 14:08发布

问题:

I started android application development and followed this tutorial:

http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/ but the application didn't work. I debug it and the log cat show this message: JNI_Onload not found..how can I solve this problem?

Thanks

回答1:

The main purpose of the JNI_OnLoad function is to register all of your native methods.

This is the recommended, but not the only, approach. Thus providing a JNI_OnLoad function is optional. Because it is used to register all native methods, it can discover a signature mismatch between a java native method declaration and its C/C++ counterpart before the method is actually used.

You could instead just load a native library from a static class initializer like that:

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

That way, you won't have to provide a JNI_OnLoad function and all native methods in 'mylib' would be discovered automatically. The only downside is that you won't know if some of your native method signatures is wrong, until you actually call it. In that case you would get an 'unsatisfiedlinkerror' telling you that no implementation was found for the native method you were trying to call.

If you go for this option (Option 2 - automatic discovery), the debug-level message will be just a warning telling you that you have 'forgotten' to provide a JNI_OnLoad function, so you can just ignore it.

For more information just look at the JNI Tips:
http://developer.android.com/guide/practices/jni.html



回答2:

JNI_OnLoad is a debug-level message that just lets you know that the function wasn't found.

It's there because people occasionally write JNI_OnLoad without the 'extern "C"' declaration and then aren't sure why things don't work.

The problem you're facing is probably unrelated to the message. Look at the nearby logcat output for clues. dlopen() failures and Java exceptions are likely relevant.

You should paste some of that into a question, and perhaps describe the failure in more detail ("didn't work" is a little vague).