1>>JNI structure (Java code >jni Code >c/c++ code)
2>> a Mediascanner invocation example
1.java layer: (Load function library name is configured by. mk file)
public class mediascanner{ static { system.loadlibrary ("Media_jni");//Load Class Library native_init ();//Call JNI layer Code } private static native final void Native_init ();}
2. JNI Layer: Static registration (function name to package name _ Class Name _ Method name)
Static Voidandroid_media_mediascanner_native_init (jnienv *env)//jni code Implementation { Jclass clazz; Clazz = Env->findclass ("Android/media/mediascanner"); if (clazz = = NULL) { jnithrowexception (env, "java/lang/runtimeexception", "Can ' t find Android/media/mediascanner") ; return; } Fields.context = Env->getfieldid (Clazz, "Mnativecontext", "I"); if (Fields.context = = NULL) { jnithrowexception (env, "java/lang/runtimeexception", "Can" t find Mediascanner.mnativecontext "); return;} }
Static registration ***********************************
Java files:
/** * @author Lean @date: 2014-11-29 */public class Getstr {static{system.loadlibrary ("Jnisample");} public static native String testget ();}
>> get jni. h Code files
Generate the. h file, call Javah-jni Packagename.classname
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class com_example_jnisample_getst R */#ifndef _included_com_example_jnisample_getstr#define _included_com_example_jnisample_getstr#ifdef __ Cplusplusextern "C" {#endif */* Class: com_example_jnisample_getstr * Method: testget * Signature: () Ljava/lang /string; */jniexport jstring jnicall java_com_example_jnisample_getstr_testget (jnienv *, jclass); #ifdef __cplusplus}# Endif#endif
>> Generate. So correspondence
To write the Jni file, copy the Com_example_jnisample_getstr.h file function name to JNISample.cpp as follows:
#include <jni.h> #include "com_example_jnisample_getstr.h" extern "C" {#endif */* Class: com_example_ JNISAMPLE_GETSTR * Method: testget * Signature: () ljava/lang/string; */jniexport jstring Jnicall Java_com_example_ Jnisample_getstr_testget (jnienv *env, Jclass) {return Env->newstringutf ("Hello");} #ifdef __cplusplus}
At this point, you can call into the JNI layer by calling the Java file and invoke the specific C + + code at the JNI layer.
*************************************** Dynamic Registration ***********************************
1. Disclaimer Gmethods (Jninativemethod is a struct that corresponds to a Java function and a JNI function one by one)
Static Jninativemethod gmethods[] = {{"Processdirectory", "(ljava/lang/string; ljava/lang/string; landroid/media/mediascannerclient;) V ", (void *) Android_media_me Diascanner_processdirectory}, {"Processfile", "(ljava/lang/string; ljava/lang/string; landroid/media/mediascannerclient;) V ", (void *) Android_media_me Diascanner_processfile}, {"SetLocale", "(ljava/lang/string;) V", (void *) Android_media_mediascanner_setloca Le}, {"Extractalbumart", "(ljava/io/filedescriptor;) [B", (void *) Android_media_mediascanner_extractalbumart}, {"Native_init", "() V", (void *) Android_media_mediascanner_native_init}, {"Native_setup", "() v", (void *) Android_media_mediascanner_native_setup}, {"Native_finalize", "() v", (void *) android_media_mediascanner_native_finalize},};
1-1.2nd parameter Method simplification of type signature
2. Actual invocation of the registration code:
This function was only registers the native methods, and was called from//Jni_onload in Android_media_mediaplayer.cppint re Gister_android_media_mediascanner (jnienv *env) { return androidruntime::registernativemethods (env, " Android/media/mediascanner ", Gmethods, Nelem (gmethods));
3. Declare the method in Android_media_mediaplayer.cpp and bind in Jni_onload, call Jni_onload () automatically after calling in System.loadlibrary ()
>>android_media_mediaplayer.cpp Statement
extern int Register_android_media_mediascanner (jnienv *env);
>>jni_onload () call
Jint jni_onload (javavm* vm, void* reserved) {jnienv* env = NULL; Jint result =-1; if (vm->getenv (void**) &env, jni_version_1_4)! = JNI_OK) { LOGE ("error:getenv failed\n"); Goto bail; } ASSERT (env! = NULL); if (Register_android_media_mediascanner (env) < 0) { LOGE ("Error:mediascanner native registration failed\n"); Goto bail; } /* Success--Return valid version number */ result = Jni_version_1_4;bail: return result;
3>> corresponding type conversions
4>>jnienv (a thread-related JNI environment struct)
1) jnienv operation Jobject equivalent to Operation Jfieldid and Jmethodid
Get Methodid
Call menv operation (Call<type>method) methodid
Get FieldID
5>> garbage collection mechanism Variable life cycle (global reference Local reference weak global Reference)
Global Reference:newglobalref declaration mclient (env->newglobalref (client))
deleteglobalref Destruction Menv->deleteglobalref (mclient);
Weak Global References: Special global references call Jnienv->issameoject before using to determine whether they are recycled
6>>jstring Mapping of String objects in Jni in Java
string of local strings to Java
Unicode:jnienv->newstring ();
Utf-8:jnienv->newstringutf ();
String -to-local strings for JAVA
Unicode:jnienv->getstringcharas ();
Utf-8:jnienv->Getstringutfcharas();
Release resources:
Unicode:jnienv->releasestringcharas ();
utf-8:jnienv->ReleaseStringutfcharas ();
Android-jni Summary (1)