JNI (Java Native Interface) is the mechanism that enables Java code to call native functions, and native code to call Java functions.
<jni.h>, which basically mirror Java's reflection APIs.
nativekeyword, and then calling them as normal Java functions.
jni_generator generates boiler-plate code with the goal of making our code:
jni_generator uses regular expressions to parse .Java files, so don't do anything too fancy. E.g.:
java.langclasses, add an explicit import.
void call(Outer.Inner inner)
The presense of any JNI within a class will result in ProGuard obfuscation for the class to be disabled.
Without Crazy Linker:
With Crazy Linker:
dlsym(), but JNI is hardcoded to use the system's
.javafiles of an APK. Inefficient, but very convenient.
dlsym()is not used in this case, we use a linker script to avoid the cost of exporting symbols from the shared library (refer to
jni_registration_generator.pyexposes two registrations methods:
RegisterNonMainDexNatives- Registers native functions needed by multiple process types (e.g. Rendereres, GPU process).
RegisterMainDexNatives- Registers native functions needed only by the browser process.
Java methods just need to be annotated with
@CalledByNative. The generated functions can be put into a namespace using
Because the generator does not generate any source files, generated headers must not be
#included by multiple sources. If there are Java functions that need to be called by multiple sources, one source should be chosen to expose the functions to the others via additional wrapper functions.
nativewill have stubs generated for them that forward calls to C++ function (that you must write).
long mNativePointer), then the bindings will automatically generate the appropriate cast and call into C++ code (JNI itself is only C).
@CalledByNativewill have stubs generated for them.
All pointers to Java objects must be registered with JNI in order to prevent garbage collection from invalidating them.
For Strings & Arrays - it's common practice to use the
//base/android/jni_* helpers to convert them to
std::strings as soon as possible.
For other objects - use smart pointers to store them:
ScopedJavaLocalRef<>- When lifetime is the current function's scope.
ScopedJavaGlobalRef<>- When lifetime is longer than the current function's scope.
JavaObjectWeakGlobalRef<>- Weak reference (do not prevent garbage collection).
JavaParamRef<>- Use to accept any of the above as a parameter to a function without creating a redundant registration.
Minimize the surface API between the two sides. Rather than calling multiple functions across boundaries, call only one (and then on the other side, call as many little functions as required).
If a Java object “owns” a native one, store the pointer via
"long mNativeClassName". Ensure to eventually call a native method to delete the object. For example, have a
close() that deletes the native object.
The best way to pass “compound” types across in either direction is to create an inner class with PODs and a factory function. If possible, make mark all the fields as “final”.
generate_jni- Generates a header file with stubs for given
generate_jar_jni- Generates a header file with stubs for a given
generate_jni_registration- Generates a header file with functions to register native-side JNI methods (required only when using crazy linker).
Refer to //build/config/android/rules.gni for more about the GN templates.