I searched but did not find an answer to this "problem". Basically, one usually needs to include the jni.h header to access jni functions, but the header contains mostly function prototypes and struct declarations, so we either include the sources to resolve the functions or include a static or dynamic library(none found so far) so the linker can resolve the function prototype declarations.

I have so far used the Android NDK to build a native library and all I did was include the jni.h header to use jni functions, but even on Oracle's documentation and examples all they do is include the jni.h header and specify include directories to the compiler, so how does the compiler find the implementations?

1 Answer
1

The compiler doesn't have to find the implementations, the JVM tells you where they are dynamically:

JNIEnv is a typedef for the struct JNIEnv_. That struct contains a const struct JNINativeInterface_* called functions and many methods. If you take a look at the JNI documentation you will notice that most methods have a JNIEnv* as their first argument, however you call them with env->method(...), without the JNIEnv* argument. The methods from the documentation are the actual methods and pointers to them are in functions in JNIEnv_. The methods in JNIEnv_ are wrappers that simply call the method-pointers in functions. Whenever the JVM creates a JNIEnv_ and the corresponding JNINativeInterface_, it dynamically writes all the method addresses.