Android JNI calls the C + + interface
Android uses the NDK native support to invoke code for the C + + interface, which can be used directly in the program as programmed in the Android JNI specification.
C language version
JNI calls the C language relatively simple, naming a JNI function, the system will be automatically registered to the Java Virtual machine, and then the Java code can be directly called:
Native Code:
#include <jni.h>int Add (int x, int y) {return x + y;} Jintjava_com_example_android_simplejni_simplejni_add (jnienv* env, jobject this , jint x, jint y) { return Add (x, y);
It is important to note that the function name must correspond to the Java class, such as the package Com.example.android.simplejni in the example: Class name is: public class Simplejni
Method name is: add
Then the Java class code can be loaded directly into the library, declared as its own method:
Package Com.example.android.simplejni;import Android.app.activity;import Android.os.bundle;import Android.widget.textview;public class Simplejni extends activity { /** called when the activity is first created. */
@Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); TextView TV = new TextView (this); System.loadlibrary ("Simplejni"); int sum = Add (2, 3); Tv.settext ("2 + 3 =" + integer.tostring (sum)); Setcontentview (TV); } public native int Add (int x, int y);}
C + + version
The C + + language, which is an object-oriented design language like Java, has more features, but the transfer function does not require C + +. It would be a bit more cumbersome to register with C + + code with a Java virtual machine.
Manual registration Method required: Directly to the Code bar: development/samples/simplejni/jni/native.cpp
#define LOG_TAG "Simplejni native.cpp" #include <utils/Log.h> #include <stdio.h> #include "jni.h" static Jintadd (jnienv *env, Jobject thiz, jint A, jint b) {int result = a + B; Alogi ("%d +%d =%d", a, b, result); return result;} static const char *classpathname = "com/example/android/simplejni/native"; static Jninativemethod methods[] = {{"Add", "( II) I ", (void*) Add},};/* * Register Several native methods for one class. */static int Registernativemethods (jnienv* env, const char* className, jninativemethod* gmethods, int nummethods) {j Class Clazz; Clazz = Env->findclass (className); if (clazz = = NULL) {aloge ("Native Registration unable to find class '%s '", className); return jni_false; } if (Env->registernatives (Clazz, Gmethods, Nummethods) < 0) {Aloge ("Registernatives failed for '%s '", cl Assname); return jni_false; } return jni_true;} /* * Register native methods for all classes we know about. * * Returns jni_tRUE on success. */static int registernatives (jnienv* env) {if (!registernativemethods (env, Classpathname, methods, sizeof (methods)/sizeof (Methods[0])) {return jni_false; } return jni_true;} ----------------------------------------------------------------------------/* * Called by the VM when the SHA Red Library is first loaded. */typedef Union {jnienv* env; void* venv;} Unionjnienvtovoid;jint jni_onload (javavm* vm, void* reserved) {unionjnienvtovoid uenv; Uenv.venv = NULL; Jint result =-1; jnienv* env = NULL; Alogi ("Jni_onload"); if (Vm->getenv (&uenv.venv, jni_version_1_4)! = JNI_OK) {aloge ("error:getenv failed"); Goto bail; } env = uenv.env; if (registernatives (env) = jni_true) {aloge ("Error:registernatives failed"); Goto bail; } result = Jni_version_1_4; Bail:return result;}
Java:simplejni/src/com/example/android/simplejni/simplejni.java
Package Com.example.android.simplejni;import Android.app.activity;import Android.os.bundle;import Android.widget.textview;public class Simplejni extends activity { /** called when the activity is first created. */
@Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); TextView TV = new TextView (this); int sum = Native.add (2, 3); Tv.settext ("2 + 3 =" + integer.tostring (sum)); Setcontentview (TV); }} Class Native { static { //The runtime would add "Lib" on the front and ". O" on the end of //the name Suppli Ed to loadLibrary. System.loadlibrary ("Simplejni"); } static native int add (int a, int b);}
Android JNI calls C/C + +