In the process of Android version development, we need to use some of the NDK's native libraries developed with JNI. Here to talk about stepping on the pit, for everyone's reference.
Although Java program I am familiar with, but did not understand the development of JNI Native, is generally directly developed. NET call. So library. There is a library from Java code porting, and there is no source code, and then encountered some problems. First, the name of the function of the JNI is required,. NET can directly invoke native-looking libraries such as the monodroid example[DllImport ("Sanangeles", EntryPoint = "Java_com_example_sanangeles_demorenderer_nativeinit")] private static extern void Nativeinit (IntPtr jnienv); Java requires naming according to Java_pakcage_class_method. Look at the Java declarationclass Demorenderer implements Glsurfaceview.renderer {Public void onsurfacecreated (GL10 gl, EGLConfig config) {nativeinit (); }private static native void Nativeinit ();If you do not have the JNI source code, do not know the function declaration, you write this[DllImport ("Sanangeles",entrypoint = "Nativeinit")] private static extern void Nativeinit (IntPtr jnienv); Then wait to play, if you improve the Java source code, be sure to pay attention to the EntryPoint statement. Second, can not find the library, instruction set (eabi,eabi-v7a) of the problem by default debugging, Xamarin debugging state, according to the Android environment, the execution of the selected instruction set. The problem comes, it can't make you choose. Then your libs only Eabi. So, there will be problems that the DLL cannot find. Debug State, add the target library of eabi-v7a. Or when using a virtual machine, select the Armeabi environment. You can see whether the library is copied to the/data/app/package/lib directory through the ADB shell and into the system. Third, JNI returns the processing of a string. Many of the methods introduced by Monodroid are called by JNIEnv class reflection, and it is not necessary to use the Reflection method var ver =XXX(JNIEnv. Handle);var s = new Java.Lang.Object (ver, jnihandleownership.transferglobalref). Javacast<java.lang.string> (). ToString (); Ver is IntPtr, so avoid using reflection and make a few more conversions. Recommendation: If a library of functions written using the NDK is only invoked with monodroid, there is no need to use the JNI specification, which is more efficient and does not require an incoming JNIEnv object.
Some problems with Monodroid calling JNI Native