{"id":193,"date":"2020-06-15T10:51:58","date_gmt":"2020-06-15T02:51:58","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=193"},"modified":"2023-08-15T17:07:32","modified_gmt":"2023-08-15T09:07:32","slug":"jni02-jni%e8%af%ad%e6%b3%95%e4%b9%8b%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=193","title":{"rendered":"JNI02-JNI\u8bed\u6cd5\u4e4b\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#1cpp%E6%96%87%E4%BB%B6\" title=\"1.cpp\u6587\u4ef6\">1.cpp\u6587\u4ef6<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#2cpp%E4%B8%8Ec%E6%96%87%E4%BB%B6%E5%9C%A8%E5%86%99JNI%E6%97%B6%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B7%AE%E5%BC%82\" title=\"2.cpp\u4e0ec\u6587\u4ef6\u5728\u5199JNI\u65f6\u7684\u4e00\u4e9b\u5dee\u5f02\">2.cpp\u4e0ec\u6587\u4ef6\u5728\u5199JNI\u65f6\u7684\u4e00\u4e9b\u5dee\u5f02<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#3JNI%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\" title=\"3.JNI\u6570\u636e\u7c7b\u578b\">3.JNI\u6570\u636e\u7c7b\u578b<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#1JNI_%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\" title=\"1)JNI \u57fa\u7840\u6570\u636e\u7c7b\u578b\">1)JNI \u57fa\u7840\u6570\u636e\u7c7b\u578b<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#2%EF%BC%89JNI_%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B\" title=\"2\uff09JNI \u5f15\u7528\u7c7b\u578b\">2\uff09JNI \u5f15\u7528\u7c7b\u578b<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#jstring%E8%BD%ACchar\" title=\"jstring\u8f6cchar*\">jstring\u8f6cchar*<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#%E5%AD%97%E7%AC%A6%E4%B8%B2\" title=\"\u5b57\u7b26\u4e32\">\u5b57\u7b26\u4e32<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\/#%E6%95%B0%E7%BB%84\" title=\"\u6570\u7ec4\">\u6570\u7ec4<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1cpp%E6%96%87%E4%BB%B6\"><\/span>1.cpp\u6587\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7531\u4e8e.cpp\u662fjni\u65b9\u6cd5,\u6240\u4ee5\u5728\u6700\u524d\u9762\u8981\u52a0\u4e0ainclude <jni.h>,<jni.h>\u5176\u5b9e\u5c31\u662f\u7528\u539f\u751f\u7684C\u5b9e\u73b0\u4e86java\u4e0eC\u4ea4\u4e92\u4e00\u5957\u539f\u5219\u5373JNI\u89c4\u8303\u3002<\/p>\n<p>.cpp\u672c\u8d28\u5c31\u662fc++\u6587\u4ef6\uff0c\u53ea\u662f\u5b9e\u73b0\u4e86java native\u65b9\u6cd5\uff0c\u6309\u7167\u4e00\u5b9a\u7684\u547d\u540d\u89c4\u5219\uff0c\u4f7fnative\u65b9\u6cd5\u53ef\u4ee5\u5173\u8054\u5230.cpp\u91cc\u7684\u65b9\u6cd5\u3002<\/p>\n<p>.cpp\u6587\u4ef6\u7684\u9664\u4e86include\u4ee3\u7801,\u5176\u5b83\u4ee3\u7801\u90fd\u8981\u5199\u5728<br \/>\nextern &quot;C&quot; {<br \/>\n}<br \/>\n\u6a21\u5757\u91cc.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"2cpp%E4%B8%8Ec%E6%96%87%E4%BB%B6%E5%9C%A8%E5%86%99JNI%E6%97%B6%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B7%AE%E5%BC%82\"><\/span>2.cpp\u4e0ec\u6587\u4ef6\u5728\u5199JNI\u65f6\u7684\u4e00\u4e9b\u5dee\u5f02<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>1\uff09\u8c03\u7528\u540c\u4e00\u4e2a\u65b9\u6cd5MethodXX<\/p>\n<pre><code>\u5728.C\u91cc\u5e94\u5199\n(*env)-&gt;MethodXX(env,jRootKey, 0);\n\u5728.cpp\u91cc\uff0c\u5c31\u8981\u5199\u6210\nenv-&gt;MethodXX(jRootKey, 0);<\/code><\/pre>\n<p>\u5982\u679c\u5728cpp\u91cc\u8c03\u7528<code>(*env)-&gt;<\/code>\u5219\u4f1a\u62a5\u9519\uff1a<code>error: base operand of &#039;-&gt;&#039; has non-pointer type &#039;JNIEnv {aka _JNIEnv}&#039;<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"3JNI%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\"><\/span>3.JNI\u6570\u636e\u7c7b\u578b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<a href=\"https:\/\/www.cnblogs.com\/guanmanman\/archive\/2017\/05\/05\/6811264.html\">https:\/\/www.cnblogs.com\/guanmanman\/archive\/2017\/05\/05\/6811264.html<\/a><\/p>\n<p>\u539f\u751f\u6570\u636e\u7c7b\u578b\u540cJava\u4e00\u6837\uff0c\u90fd\u5305\u542b\u4e24\u7c7b\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u5f15\u7528\u7c7b\u578b\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1JNI_%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\"><\/span>1)JNI \u57fa\u7840\u6570\u636e\u7c7b\u578b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5728JNI\u5f53\u4e2d\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u76f4\u63a5\u4e0eC\/C++\u7684\u76f8\u5bf9\u5e94\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6620\u5c04\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u62ff\u6765\u4f7f\u7528\u5e76\u4e0d\u9700\u8981\u8f6c\u6362\u3002<\/p>\n<p>\u6211\u4eec\u5148\u6765\u770b\u770bjava\u3001JNI\u3001C\u4e4b\u95f4\u7684\u57fa\u672c\u7c7b\u578b\u7684\u6620\u5c04\u5173\u7cfb\uff1a<\/p>\n<p><img src=\"http:\/\/122.152.205.50\/wordpress_it\/wp-content\/uploads\/2020\/06\/image.png\" alt=\"img\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"2%EF%BC%89JNI_%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B\"><\/span>2\uff09JNI \u5f15\u7528\u7c7b\u578b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5f15\u7528\u7c7b\u578b\u6211\u4eec\u5e76\u4e0d\u80fd\u76f4\u63a5\u7684\u4f7f\u7528\uff0c\u5b83\u4e0d\u662f\u4ee5\u539f\u751f\u6570\u636e\u7c7b\u578b\u7684\u5f62\u5f0f\u5c55\u73b0\uff0c\u800c\u662f\u9700\u8981\u901a\u8fc7JNI\u63d0\u4f9b\u7684\u4e00\u7ec4\u76f8\u5173\u7684API\u628a\u5f15\u7528\u7c7b\u578b\u63d0\u4f9b\u7ed9\u539f\u751f\u4ee3\u7801\u4f7f\u7528\u3002<\/p>\n<p><img src=\"http:\/\/122.152.205.50\/wordpress_it\/wp-content\/uploads\/2020\/06\/image-1.png\" alt=\"img\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"jstring%E8%BD%ACchar\"><\/span>jstring\u8f6cchar*<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code>const char* cStr;\ncStr = (*env)-&gt;GetStringUTFChars(env, jUserName,0);  \/\/\u901a\u8fc7jni\u7684API\u5c06jni\u5f15\u7528\u7c7b\u578b\u8f6c\u6362\u6210\u539f\u751f\u6570\u636e\u7c7b\u578b(C\u6570\u636e\u7c7b\u578b)\n...\nif (cStr) {\n        env-&gt;ReleaseStringUTFChars(jUserName, cStr);\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cGetStringUTFChars\u5c31\u662fJNI\u6240\u63d0\u4f9b\u7684API\uff0c\u901a\u8fc7\u8be5\u65b9\u6cd5\u53ef\u4ee5\u628ajava\u4e2d\u6240\u4f20\u9012\u7684Stirng\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u539f\u751f\u4ee3\u7801\u6240\u80fd\u8bc6\u522b\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5728\u8fdb\u884c\u4e0b\u9762\u7684\u6bd4\u8f83\u64cd\u4f5c\u3002strcmp\u65b9\u6cd5\u662fC\u8bed\u8a00\u4e2d\u6240\u63d0\u4f9b\u7684\u7684\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E5%AD%97%E7%AC%A6%E4%B8%B2\"><\/span>\u5b57\u7b26\u4e32<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u2460\uff1a\u521b\u5efa\u5b57\u7b26\u4e32<\/p>\n<p>\u53ef\u4ee5\u5728\u539f\u751f\u4ee3\u7801\u4e2d\u4f7f\u7528NewString\u51fd\u6570\u6784\u5efaUnicode\u7f16\u7801\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u5b9e\u4f8b\uff0c\u5047\u5982\u4f60\u4f7f\u7528\u7684\u662futf-8\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528NewStringUTF\u51fd\u6570\u6765\u6784\u5efa\uff0c\u5982\uff1a<\/p>\n<pre><code>jstring jString = (*env)-&gt;NewString(env,&quot;I am from C&quot;);\njstring jString = (*env)-&gt;NewStringUTF(env,&quot;I am from C&quot;);<\/code><\/pre>\n<p>\u2461\uff1a\u628aJava\u5b57\u7b26\u4e32\u8f6c\u6362\u6210C\u5b57\u7b26\u4e32<\/p>\n<p>Java\u5b57\u7b26\u4e32String\u662f\u5c5e\u4e8e\u5f15\u7528\u7c7b\u578b\u7684\uff0c\u5b83\u4e0d\u80fd\u591f\u76f4\u63a5\u7684\u88ab\u539f\u751f\u4ee3\u7801\u4f7f\u7528\uff0c\u4e3a\u4e86\u5728\u539f\u751f\u4ee3\u7801\u4e2d\u4f7f\u7528Java\u4f20\u9012\u8fc7\u6765\u7684String\u4e32\uff0c\u6211\u4eec\u9700\u8981\u501f\u7528JNI API\uff0c\u6839\u636e\u7f16\u7801\u7684\u4e0d\u540c\uff0c\u5206\u522b\u53ef\u4ee5\u662f\u7528GetStringChars\u548cGetStringUTFChars\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff0c\u5982\u6211\u4eec\u4e0a\u9762\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre><code>const char* cStr = (*env)-&gt;GetStringUTFChars(env, jUserName,&amp;isCopy);<\/code><\/pre>\n<p>isCopy\u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u5b83\u662fjboolean\u7c7b\u578b\u7684\uff0c\u5b83\u7684\u8c03\u7528\u8005\u660e\u786e\u8fd4\u56de\u7684C\u5b57\u7b26\u4e32\u5730\u5740\u662f\u6307\u5411\u526f\u672c\u9ed1\u5e02\u6307\u5411\u5806\u4e2d\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u5b57\u7b26\u4e32\u7528\u5b8c\u8bb0\u5f97\u91ca\u653e\u4e00\u4e0b<\/p>\n<pre><code>(*env)-&gt;ReleaseStringUTFChars(env, javaString, str);<\/code><\/pre>\n<p>\u2462\uff1a\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/p>\n<p>int dataLen = env-&gt;GetStringUTFLength(originalStr);<\/p>\n<p>\u8fd9\u91cc\u7684originalStr\u662fjstring\u7c7b\u578b<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%95%B0%E7%BB%84\"><\/span>\u6570\u7ec4<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u4e00\u822c\u6570\u7ec4\u76f8\u5173\u7684\u64cd\u4f5c\u65b9\u6cd5\u90fd\u8981\u6210\u5bf9\u4f7f\u7528\uff0c\u7533\u8bf7\u548c\u91ca\u653e\u3002<\/p>\n<p>\u2460\uff1a\u521b\u5efa\u6570\u7ec4<\/p>\n<p>JNI\u901a\u8fc7New&lt;\uff1f&gt;Array\u51fd\u6570\u5728\u539f\u751f\u4ee3\u7801\u4e2d\u521b\u5efa\u6570\u7ec4\uff0c&lt;\uff1f&gt;\u7c7b\u578b\u53ef\u4ee5\u662fbyte\uff0cshort\uff0cint\u7b49\u7c7b\u578b\uff0c\u5982\uff1a<\/p>\n<pre><code>jintArray intArray = (*env)-&gt;NewIntArray(env,5);<\/code><\/pre>\n<p>NewIntArray\u6570\u7ec4\u4e2d\u5e94\u8be5\u7ed9\u51fa\u660e\u786e\u7684\u6570\u7ec4\u957f\u5ea6\uff0c\u4f8b\u5982\uff0c5\u5c31\u662fintArray\u7684\u6570\u7ec4\u957f\u5ea6\u3002<\/p>\n<p>\u2461\uff1a\u5bf9\u6570\u7ec4\u7684\u64cd\u4f5c<br \/>\n\u5982\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6570\u7ec4\u5c5e\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u901a\u8fc7Java\u4f20\u9012\u8fc7\u6765\u7684\u6570\u7ec4\u6211\u4eec\u662f\u65e0\u6cd5\u76f4\u63a5\u8fdb\u884c\u64cd\u4f5c\u7684\uff0c\u4f46\u662f\u6839\u636eJNI\u63d0\u4f9b\u7684API\u6211\u4eec\u53ef\u4ee5\u5f88\u987a\u5229\u7684\u8fdb\u884c\u6570\u7ec4\u7684\u64cd\u4f5c.<\/p>\n<p><strong>1)\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6<\/strong><\/p>\n<pre><code> jsize size = env-&gt;GetArrayLength(arr);<\/code><\/pre>\n<p><strong>2)\u83b7\u53d6\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u5c06jbyteArray\u8f6c\u6362\u6210jint*\uff0cjint\u662fjni\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0cC\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u3002<\/p>\n<pre><code>jboolean copy = true;\njint* p = env-&gt;GetIntArrayElements( arr, &amp;copy);<\/code><\/pre>\n<p>\u7136\u800ccopy\u8fd9\u4e2a\u53c2\u6570\u4e0d\u7ba1\u662f\u4e0d\u662ffalse\uff0c\u5728JNI\u5c42\u5bf9java\u4f20\u9012\u8fc7\u6765\u7684\u6570\u7ec4\u8fdb\u884c\u4fee\u6539\uff0c\u5728java\u91cc\u6253\u5370\u4fee\u6539\u540e\u7684\u7ed3\u679c\u5e76\u6ca1\u6709\u53d8\u3002<a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1338252\">https:\/\/cloud.tencent.com\/developer\/article\/1338252<\/a><br \/>\n\u4e8b\u5b9e\u4e0a\uff0c\u4e0a\u9762\u535a\u5ba2\u4f5c\u8005\u548c\u6211\u90fd\u6ca1\u6709\u5bf9\u8fd9\u4e2a\u65b9\u6cd5\u4f5c\u51fa\u6df1\u523b\u7684\u7814\u7a76\u3002\u6211\u8bef\u6253\u8bef\u649e\u5730\u8c03\u7528ReleaseIntArrayElements\u65b9\u6cd5\uff0c\u53d1\u73b0java\u91cc\u6253\u5370\u7684\u6570\u7ec4\u7adf\u7136\u6539\u53d8\u4e86\u3002<a href=\"https:\/\/blog.csdn.net\/xlh1191860939\/article\/details\/77200559\u8fd9\u7bc7\u6587\u7ae0\u4f5c\u8005\u5bf9\u6570\u7ec4\u64cd\u4f5c\u7684\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4f5c\u4e86\u6df1\u523b\u7684\u8bb2\u89e3\">https:\/\/blog.csdn.net\/xlh1191860939\/article\/details\/77200559\u8fd9\u7bc7\u6587\u7ae0\u4f5c\u8005\u5bf9\u6570\u7ec4\u64cd\u4f5c\u7684\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4f5c\u4e86\u6df1\u523b\u7684\u8bb2\u89e3<\/a>\u3002<\/p>\n<p>\u7136\u800c\uff0c\u6211\u901a\u8fc7ReleaseIntArrayElements\u91ca\u653e\u4e86\u7f13\u51b2\u533a\uff0c\u4e3a\u5565\u6307\u9488\u8fd8\u80fd\u8bbf\u95ee\u5230\u6570\u7ec4\uff1f<\/p>\n<pre><code> env-&gt;ReleaseIntArrayElements(arr,p,0);\n  LOGD(&quot;arr[0] xxx = %d&quot;,*p);<\/code><\/pre>\n<p><strong>3)java\u6570\u7ec4\u590d\u5236\u6210C\u6570\u7ec4<\/strong><\/p>\n<p>\u901a\u8fc7Get&lt;\uff1f&gt;ArrayRegion\u51fd\u6570\u5c06\u7ed9\u5b9a\u7684java\u6570\u7ec4\u590d\u5236\u6210C\u6570\u7ec4<\/p>\n<p>\u793a\u4f8b1\uff1a\u4ecejava\u4f20\u9012\u4e00\u4e2a\u6570\u7ec4\u5230C\uff0c\u5728C\u5c42\u5bf9\u6570\u7ec4\u5143\u7d20\u52a01\uff0c\u518d\u8fd4\u7ed9java\u3002<\/p>\n<p>java\u4ee3\u7801\uff1a<\/p>\n<pre><code> int[] arr = new int[]{1,2,3};\n              sendArray2JNI(arr);\n\n              for (int i = 0; i &lt; arr.length; i++) {\n                  Log.d(&quot;CZLog&quot;, &quot;newArr[&quot; + i + &quot;]: &quot; + arr[i]);\n              }<\/code><\/pre>\n<p>C\u4ee3\u7801\uff1a<\/p>\n<pre><code>extern &quot;C&quot; JNIEXPORT void JNICALL Java_com_example_jni01_MainActivity_sendArray2JNI\n        (JNIEnv *env, jobject obj, jintArray arr){\n    jint p2[3] ;\n    env-&gt;GetIntArrayRegion(arr,0,3,p2);\n    p2[0]++;\n    p2[1]++;\n    p2[2]++;\n    env-&gt;SetIntArrayRegion(arr,0,3,p2);\n}<\/code><\/pre>\n<p>\u7591\u95ee1:GetIntArrayRegion\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662fjint<em>,\u4e3a\u5565\u6211\u58f0\u660ejint<\/em>\u4f20\u5165GetIntArrayRegion\u65b9\u6cd5\u62a5\u9519\uff1a<\/p>\n<p>A\/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x24 in tid 24200 (m.example.jni01), pid 24200 (m.example.jni01)<\/p>\n<p><strong>4\uff09JNI \u7684 jbyteArray \u8f6c\u6362\u4e3a c\u7684 unsigned char *<\/strong><\/p>\n<pre><code>jint Java_cn_hongdameng_aacencoder_AacEncoder_AacWrite(JNIEnv* env,jobject this,jbyteArray jBuffer, jint jBufferSize) \n\n{    \n\n    jbyte* bBuffer = (*env)-&gt;GetByteArrayElements(env,jBuffer,0);    \n\n    unsigned char* buf=(unsigned char*)bBuffer; \n\n }<\/code><\/pre>\n<p><strong>5)C\u7684char*\u8f6cjni\u7684jbyteArray<\/strong><\/p>\n<p>jbyteArray byteArray = env-&gt;NewByteArray(len_enc);<br \/>\nenv-&gt;SetByteArrayRegion(byteArray, 0, len_enc, (jbyte *) buff_enc);<br \/>\nreturn byteArray;<\/p>\n<button class=\"simplefavorite-button\" data-postid=\"193\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 1.cpp\u6587\u4ef62.cpp\u4e0ec\u6587\u4ef6\u5728\u5199JNI\u65f6\u7684\u4e00\u4e9b\u5dee\u5f023.JNI\u6570\u636e\u7c7b\u578b1)JNI \u57fa\u7840\u6570\u636e\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/193"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=193"}],"version-history":[{"count":26,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":10241,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions\/10241"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}