android jni的JNINativeMethod

<p>Andoird 中使用了一種不一樣傳統Java JNI的方式來定義其native的函數。其中很重要的區別是Andorid使用了一種Java 和 C 函數的映射表數組,並在其中描述了函數的參數和返回值。這個數組的類型是JNINativeMethod,定義以下:</p> <p><wbr></p> <p>typedef struct {<br> <wbr><wbr><wbr>const char* name;<br> <wbr><wbr><wbr>const char* signature;<br> <wbr><wbr><wbr>void* <wbr><wbr><wbr><wbr><wbr><wbr>fnPtr;<br> } JNINativeMethod;</p> <p>第一個變量name是Java中函數的名字。</p> <p>第二個變量signature,用字符串是描述了函數的參數和返回值</p> <p>第三個變量fnPtr是函數指針,指向C函數。</p> <p><wbr></p> <p>其中比較難以理解的是第二個參數,例如</p> <p>"()V"</p> <p>"(II)V"</p> <p>"(Ljava/lang/String;Ljava/lang/String;)V"</p> <p><wbr></p> <p>實際上這些字符是與函數的參數類型一一對應的。</p> <p>"()" 中的字符表示參數,後面的則表明返回值。例如"()V" 就表示void Func();</p> <p>"(II)V" 表示 void Func(int, int);</p> <p><wbr></p> <p>具體的每個字符的對應關係以下</p> <p><wbr></p> <p><strong>字符 <wbr><wbr><wbr><wbr>Java類型 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>C類型</strong></p> <p>V&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>void<br> Z&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jboolean&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>boolean<br> I&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int<br> J&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr><wbr><wbr><wbr><wbr><wbr><wbr>jlong&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<br> D&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jdouble&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>double<br> F&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jfloat&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>float<br> B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jbyte&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>byte<br> C&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jchar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>char<br> S&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jshort&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>short</p> <p><wbr></p> <p>數組則以"["開始,用兩個字符表示</p> <p><wbr></p> <p>[I&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jintArray&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int[]<br> [F&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jfloatArray&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr>float[]<br> [B&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jbyteArray&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>byte[]<br> [C&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jcharArray&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>char[]<br> [S&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jshortArray&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr>short[]<br> [D&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jdoubleArray <wbr><wbr><wbr><wbr>double[]<br> [J&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jlongArray&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>long[]<br> [Z&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jbooleanArray <wbr><wbr><wbr>boolean[]</p> <p><wbr></p> <p>上面的都是基本類型。若是Java函數的參數是class,則以"L"開頭,以";"結尾中間是用"/" 隔開的包及類名。而其對應的C函數名的參數則爲jobject. 一個例外是String類,其對應的類爲jstring</p> <p><wbr></p> <p>Ljava/lang/String; <wbr><wbr><wbr><wbr><wbr>String <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jstring<br> Ljava/net/Socket; <wbr><wbr><wbr><wbr><wbr>Socket <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>jobject</p> <p><wbr></p> <p>若是JAVA函數位於一個嵌入類,則用$做爲類名間的分隔符。</p> <p>例如 "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z"</p> <p><br> </p>java

相關文章
相關標籤/搜索