{"id":164,"date":"2020-11-16T15:41:55","date_gmt":"2020-11-16T07:41:55","guid":{"rendered":"http:\/\/pareto.fun\/?p=164"},"modified":"2020-11-16T15:41:56","modified_gmt":"2020-11-16T07:41:56","slug":"jni%e4%b8%a4%e7%a7%8d%e6%b3%a8%e5%86%8c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/pareto.fun\/?p=164","title":{"rendered":"JNI\u4e24\u79cd\u6ce8\u518c\u65b9\u6cd5"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u9759\u6001\u6ce8\u518c<\/h1>\n\n\n\n<p>\u901a\u8fc7\u51fd\u6570\u540d\u5efa\u7acbJNI\u548cjava\u5c42\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u73b0\u8fc7\u7a0b<\/h2>\n\n\n\n<ol><li>\u5199java\u4ee3\u7801<\/li><li>\u7528javah\u6307\u4ee4\u4ea7\u751f\u5bf9\u5e94\u7684 *.h \u6587\u4ef6<\/li><li>\u5728 *.h \u4e2d\u5b9e\u73b0\u5b9a\u4e49<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f3a\u70b9<\/h2>\n\n\n\n<ol><li>\u4e0d\u65b9\u4fbf\u7f16\u5199\uff0c JNI\u65b9\u6cd5\u540d\u9700\u8981\u9075\u5b88\u6ce8\u518c\u89c4\u5219\uff0c\u4e14\u540d\u79f0\u975e\u5e38\u7684\u957f\u3002<\/li><li>\u7f16\u5199\u8fc7\u7a0b\u4e2d\u592a\u591a\u6b65\u9aa4 \uff0c\u5f88\u4e0d\u65b9\u4fbf\u3002<\/li><li>\u7a0b\u5e8f\u8fd0\u884c\u4e0d\u591f\u9ad8\u6548\uff0c\u56e0\u4e3a\u521d\u59cb\u8c03\u7528native \u51fd\u6570\u8981\u53bb\u641c\u7d22JNI \u5c42\u5bf9\u5e94\u51fd\u6570\u540d\u7684\u672c\u5730\u51fd\u6570\uff0c\u7136\u540e\u5efa\u7acb\u5bf9\u5e94\u5173\u7cfb\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u6d88\u8017\u4e00\u5b9a\u7684\u65f6\u95f4\u3002<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u547d\u540d\u89c4\u5219<\/h3>\n\n\n\n<p>Java\u5f00\u5934\uff0c\u7136\u540e\u662f\u4e0b\u5212\u7ebf\uff0c\u7d27\u63a5\u7740\u662f\u4e0b\u5212\u7ebf\u5206\u5272\u7684package name\uff0c\u7136\u540e\u8ddf\u7740\u7684\u662f\u4e0b\u5212\u7ebf\u548cclass name\uff0c\u6700\u540e\u662f\u4e0b\u5212\u7ebf\u548c\u65b9\u6cd5\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">JNIEXPORT jstring JNICALL<br>Java_com_example_efan_jni_1learn2_MainActivity_stringFromJNI(<br> &nbsp; &nbsp; &nbsp;  JNIEnv *env,<br> &nbsp; &nbsp; &nbsp;  jobject \/* this *\/) {<br> &nbsp;  std::string hello = \"Hello from C++\";<br> &nbsp;  return env-&gt;NewStringUTF(hello.c_str());<br>}<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u52a8\u6001\u6ce8\u518c<\/h1>\n\n\n\n<p>\u4f7f\u7528RegisterNatives \u65b9\u6cd5\u6765\u6ce8\u518cjni\u51fd\u6570\u4e0ejava\u65b9\u6cd5\u4e3a\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u73b0\u8fc7\u7a0b<\/h2>\n\n\n\n<ol><li>\u4f7f\u7528JNINativeMethod \u6570\u7ec4\u6765\u8bb0\u5f55\u5bf9\u5e94\u7684java\u548cjni\u65b9\u6cd5\uff1b<\/li><li>\u5b9e\u73b0JNI_OnLoad \u65b9\u6cd5\uff0c\u5728\u8f7d\u5165\u52a8\u6001\u5e93\u65f6\u8fdb\u884c\u52a8\u6001\u6ce8\u518c\uff1b<\/li><li>\u4f7f\u7528FindClass\u65b9\u6cd5\u6765\u83b7\u53d6java\u5bf9\u8c61\uff1b<\/li><li>\u8c03\u7528RegisterNatives\u65b9\u6cd5\uff0c\u4f20\u5165java\u5bf9\u8c61\u548cJNINativeMethod \u6570\u7ec4\uff0c\u548c\u6ce8\u518c\u53bb\u5b8c\u6210\u7684\u6570\u76ee\uff1b<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9<\/h3>\n\n\n\n<ol><li>\u8fc7\u7a0b\u66f4\u52a0\u6e05\u6670\u548c\u53ef\u63a7\uff1b<\/li><li>\u9ad8\u6548<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">jstring stringFromJNI(JNIEnv *env, jobject thiz){<br> &nbsp; &nbsp;std::string hello = \"Hello from C++\";<br> &nbsp; &nbsp;return env-&gt;NewStringUTF(hello.c_str());<br>}<br> <br>static const JNINativeMethod gMethods[] = {<br> &nbsp; &nbsp; &nbsp;  {\"stringFromJNI\", \"()Ljava\/lang\/String;\", (jstring*)stringFromJNI}<br>};<br> <br>JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved){<br> <br> &nbsp; &nbsp;__android_log_print(ANDROID_LOG_INFO, \"native\", \"Jni_OnLoad\");<br> &nbsp; &nbsp;JNIEnv* env = NULL;<br> &nbsp; &nbsp;If(vm-&gt;GetEnv((void**)&amp;env, JNI_VERSION_1_4) != JNI_OK) \/\/Get JNIEnv from JavaVM, generally use version 1.4<br> &nbsp; &nbsp; &nbsp; &nbsp;return -1;<br> &nbsp; &nbsp;jclass clazz = env-&gt;FindClass(\"com\/example\/efan\/jni_learn2\/MainActivity\");<br> &nbsp; &nbsp;if (!clazz){<br> &nbsp; &nbsp; &nbsp; &nbsp;__android_log_print(ANDROID_LOG_INFO, \"native\", \"cannot get class: com\/example\/efan\/jni_learn2\/MainActivity\");<br> &nbsp; &nbsp; &nbsp; &nbsp;return -1;<br> &nbsp;  }<br> &nbsp; &nbsp;if(env-&gt;RegisterNatives(clazz, gMethods, sizeof(gMethods)\/sizeof(gMethods[0])))<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;__android_log_print(ANDROID_LOG_INFO, \"native\", \"register native method failed!\\n\");<br> &nbsp; &nbsp; &nbsp; &nbsp;return -1;<br> &nbsp;  }<br> &nbsp; &nbsp;return JNI_VERSION_1_4;<br>}<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">JNINativeMethod<\/h2>\n\n\n\n<p>\u8be5\u7ed3\u6784\u4f53\u7528\u4e8e\u52a8\u6001\u6ce8\u518c\u8fc7\u7a0b \uff0c JNINativeMethod \u88ab\u7528\u6765\u8bb0\u5f55\u5bf9\u5e94\u7684jni\u65b9\u6cd5\u548cjava\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">typedef struct {<br> &nbsp;  const char* name;<br> &nbsp;  const char* signature;<br> &nbsp;  void* &nbsp; &nbsp; &nbsp; fnPtr;<br>} JNINativeMethod;<\/pre>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0cname \uff0cjava\u65b9\u6cd5\u7684\u540d\u79f0\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0csignature\uff0c\u7528\u6765\u63cf\u8ff0\u65b9\u6cd5\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0cfnPtr , \u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6307\u5411jni\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\uff0c\u7b2c\u4e8c\u53c2\u6570signature\u7528\u5b57\u7b26\u4e32\u7c7b\u578b\u4e3a\u53c2\u6570\u8bb0\u5f55\u65b9\u6cd5\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u5177\u4f53\u7684\u683c\u5f0f\u50cf&#8221;()V&#8221; , &#8220;(II)V&#8221; ,\u683c\u5f0f\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff0c\u62ec\u53f7\u5185\u90e8\u548c\u62ec\u53f7\u53f3\u8fb9\u7684\u53c2\u6570\uff0c\u53f3\u8fb9\u7684\u53c2\u6570\u8868\u793a\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u7c7b\u578b\u6620\u5c04<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u7840\u6570\u636e\u7c7b\u578b<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Java type<\/th><th>Native type<\/th><th>Domain descriptor<\/th><th>supplement<\/th><\/tr><\/thead><tbody><tr><td>boolean<\/td><td>jboolean<\/td><td>Z<\/td><td><\/td><\/tr><tr><td>byte<\/td><td>jbyte<\/td><td>B<\/td><td><\/td><\/tr><tr><td>char<\/td><td>jchar<\/td><td>C<\/td><td><\/td><\/tr><tr><td>short<\/td><td>jshort<\/td><td>S<\/td><td><\/td><\/tr><tr><td>int<\/td><td>jint<\/td><td>I<\/td><td><\/td><\/tr><tr><td>long<\/td><td>jlong<\/td><td>J<\/td><td><\/td><\/tr><tr><td>float<\/td><td>jfloat<\/td><td>F<\/td><td><\/td><\/tr><tr><td>double<\/td><td>jdouble<\/td><td>D<\/td><td><\/td><\/tr><tr><td>void<\/td><td>void<\/td><td>V<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u5bf9\u8c61\u5f15\u7528\u7c7b\u578b<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u5176\u4ed6\u5f15\u7528\u7c7b\u578b\uff0c\u4f8b\u5982java\u5bf9\u8c61\uff0c\u6620\u5c04\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><\/th><th><\/th><th><\/th><th><\/th><\/tr><\/thead><tbody><tr><td>Java type<\/td><td>Native type<\/td><td>Domain descriptor<\/td><td>supplement<\/td><\/tr><tr><td>Class name (such as Surface)<\/td><td>Usually jobject, with one exception, if the java type is a String, the corresponding native type is jstring<\/td><td>Start with &#8220;L&#8221; and end with &#8220;;&#8221; in the middle of the package and class name separated by &#8220;\/&#8221; (such as Landroid\/view\/Surface;) if the inner class uses $ to connect the inner class;<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u5bf9\u8c61\u6570\u7ec4\u5f15\u7528\u89c4\u5219<\/h3>\n\n\n\n<p>\u5982\u679c\u4ed6\u662f\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5982\u4e0b\u8868\u3002\u5982\u679c\u662f\u4e8c\u7ef4\u6216\u8005\u66f4\u9ad8\u7ef4\u6570\u7ec4\uff0c\u5bf9\u5e94\u7684native\u7c7b\u578b\u662fjobjectArray \uff0c \u5e76\u4e14'[&#8216; \u7684\u6570\u76ee\u8868\u793a\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Java type<\/th><th>Native type<\/th><th>Domain descriptor<\/th><th>supplement<\/th><\/tr><\/thead><tbody><tr><td>Class name (such as Surface)<\/td><td>Usually jobject, with one exception, if the java type is a String, the corresponding native type is jstring<\/td><td>Add &#8216;[&#8216; character to the left based on the domain descriptor of the object reference type<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">jni \u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570<\/h2>\n\n\n\n<p>\u6709\u4e24\u4e2a\u9ed8\u8ba4\u53c2\u6570\u5728jni\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">JNIEnv *env, jobject thiz<\/pre>\n\n\n\n<p>JNIEnv \u6307\u5411\u5f53\u524djava\u73af\u5883\uff0c\u53ef\u4ee5\u4f7f\u7528JNIEnv\u6765\u64cd\u7eb5java\u5c42\u4ee3\u7801\uff1bjobject \u6307\u5411\u5bf9\u5e94\u7684java native\u65b9\u6cd5\u5bf9\u5e94\u7684jni\u51fd\u6570\u7684\u7c7b\u5b9e\u4f8b\uff0c\u5982\u679cjava\u65b9\u6cd5\u662f\u9759\u6001\u7684\uff0c\u5b83\u5c31\u4ee3\u8868\u7c7b\u5bf9\u8c61\u7684\u5f15\u7528\uff1b<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><a href=\"https:\/\/github.com\/doom-man\/jnidemo\">https:\/\/github.com\/doom-man\/jnidemo<\/a><\/p><p><a href=\"http:\/\/zhixinliu.com\/2015\/07\/01\/2015-07-01-jni-register\/\">http:\/\/zhixinliu.com\/2015\/07\/01\/2015-07-01-jni-register\/<\/a><\/p><p><a href=\"https:\/\/www.programmersought.com\/article\/70121569265\/\">https:\/\/www.programmersought.com\/article\/70121569265\/<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u9759\u6001\u6ce8\u518c \u901a\u8fc7\u51fd\u6570\u540d\u5efa\u7acbJNI\u548cjava\u5c42\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002 \u5b9e\u73b0\u8fc7\u7a0b \u5199java\u4ee3\u7801 \u7528javah\u6307 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/164"}],"collection":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=164"}],"version-history":[{"count":1,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":165,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/164\/revisions\/165"}],"wp:attachment":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}