Home » Java » Android JNI

Android JNI

JNI call simple comb, keep slowly perfect.

java access to C logic is relatively simple,

Javah -jni com.android.client.Foo generates header file name + class name Class file

In the native engine, the AndroidRuntime class provides a registerNativeMethods function that defines the name mapping relationship between the Native function and the C function,

/work/project/kraft_m/libnativehelper/include/nativehelper/jni.h each platforms has a different definition,

Have a dynamic library java to call the Native function, need to use the System.loadLibrary ("libname") function to load the library

When the program is running, the programmer must load the C's dynamic link library into the
before invoking the native method
Java cannot access variables in C; the variables in C are private to Java,

C access Java

C needs to pass. You need the class name, function name, parameter

1. get the object of the Java class

Clazz = env->getObjectClass (jobejct); C calls Java
through the Java call C interface
2. get the Id value of the function in Java

Jmethodid mid = env->getMethodId (clazz, method_name, parameter and return value type);

The parameter is in parentheses, and the return value is outside the bracket

3. after you find the function, you can call the function

Env->CallXXXMethod (jobject, mid, RET);

Accessing variable Java in C

Clazz = env->getObjectClass (jobejct);

Find the ID value of the variable

JfieldID, FID = env->GetFiledId (clazz, filed_name, I);

Value = env->GetXXXFiled (Env, jobejct, FID); the first second parameter is the original Java, accessing the two parameter C of the
Gets the variable value by the return value