{"id":412,"date":"2023-02-25T10:59:52","date_gmt":"2023-02-25T02:59:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=412"},"modified":"2023-04-29T21:27:43","modified_gmt":"2023-04-29T13:27:43","slug":"android-ndk-basic-jni-array-processing-jni-reference-variable","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-jni-array-processing-jni-reference-variable\/","title":{"rendered":"Android NDK\u57fa\u784012\uff1aJNI\u6570\u7ec4\u5904\u7406_JNI\u5f15\u7528\u53d8\u91cf"},"content":{"rendered":"<h2>JNI\u6570\u7ec4\u5904\u7406<\/h2>\n<h3>\u4f20\u5165\u6570\u7ec4<\/h3>\n<pre><code class=\"language-c\">public class JniTest {\n\n    public native void giveArray(int[] arr);\n\n    public static void main(String[] args) {\n        JniTest t = new JniTest();\n        int arr = {9, 100, 10, 37, 5, 10};\n        \/\/\u6392\u5e8f\n        t.giveArray(arr);\n        for (int i : arr) {\n            System.out.println(i);\n        }\n    }\n\n    \/\/\u52a0\u8f7d\u52a8\u6001\u5e93\n    static {    \n        System.loadLibrary(&quot;JniTest&quot;);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint compare(int *a, int *b) {\n    return (*a) - (*b);\n}\n\n\/\/\u4f20\u5165\u6570\u7ec4\nJNIEXPORT void JNICALL Java_cn_appblog_jni_JniTest_giveArray\n  (JNIEnv *env, jobject jobj, jintArray arr) {\n    \/\/jintArray -&gt; jint\u6307\u9488 -&gt; c int \u6570\u7ec4\n    jint *elems = (*env)-&gt;GetIntArrayElements(env, arr, NULL); \/\/\u5b9e\u9645\u6d4b\u8bd5\u8868\u660e\u662f\u590d\u5236\u6570\u7ec4\n    \/\/printf(&quot;%#x,%#x\\n&quot;, &amp;elems, &amp;arr);\n\n    \/\/\u6570\u7ec4\u7684\u957f\u5ea6\n    int len = (*env)-&gt;GetArrayLength(env, arr);\n    \/\/\u6392\u5e8f\n    qsort(elems, len, sizeof(jint), compare);    \n\n    \/\/\u540c\u6b65\n    \/\/\u7b2c\u4e09\u4e2a\u53c2\u6570mode\n    \/\/0\uff1aJava\u6570\u7ec4\u8fdb\u884c\u66f4\u65b0\uff0c\u5e76\u4e14\u91ca\u653eC\/C++\u6570\u7ec4\n    \/\/JNI_ABORT\uff1aJava\u6570\u7ec4\u4e0d\u8fdb\u884c\u66f4\u65b0\uff0c\u4f46\u662f\u91ca\u653eC\/C++\u6570\u7ec4\n    \/\/JNI_COMMIT\uff1aJava\u6570\u7ec4\u8fdb\u884c\u66f4\u65b0\uff0c\u4e0d\u91ca\u653eC\/C++\u6570\u7ec4\uff08\u51fd\u6570\u6267\u884c\u5b8c\uff0c\u6570\u7ec4\u8fd8\u662f\u4f1a\u91ca\u653e\uff09\n    (*env)-&gt;ReleaseIntArrayElements(env, arr, elems, JNI_COMMIT);\n}<\/code><\/pre>\n<h3>\u8fd4\u56de\u6570\u7ec4<\/h3>\n<pre><code class=\"language-c\">public class JniTest {\n\n    public native void getArray(int len);\n\n    public static void main(String[] args) {\n        JniTest t = new JniTest();\n        int[] arr = t.getArray(10);\n        for (int i : arr) {\n            System.out.println(i);\n        }\n    }\n\n    \/\/\u52a0\u8f7d\u52a8\u6001\u5e93\n    static {    \n        System.loadLibrary(&quot;JniTest&quot;);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u8fd4\u56de\u6570\u7ec4\nJNIEXPORT jintArray JNICALL Java_cn_appblog_jni_JniTest_getArray(JNIEnv *env, jobject jobj, jint len) {\n    \/\/\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5927\u5c0f\u7684\u6570\u7ec4\n    jintArray jint_arr = (*env)-&gt;NewIntArray(env, len);\n    jint *elems = (*env)-&gt;GetIntArrayElements(env, jint_arr, NULL);    \n    int i = 0;\n    for (; i &lt; len; i++) {\n        elems[i] = i;\n    }\n\n    \/\/\u540c\u6b65\n    (*env)-&gt;ReleaseIntArrayElements(env, jint_arr, elems, 0);    \n\n    return jint_arr;\n}<\/code><\/pre>\n<h2>JNI\u5f15\u7528\u53d8\u91cf<\/h2>\n<p>\u5f15\u7528\u7c7b\u578b\uff1a\u5c40\u90e8\u5f15\u7528\u548c\u5168\u5c40\u5f15\u7528<\/p>\n<p>\u4f5c\u7528\uff1a\u5728JNI\u4e2d\u544a\u77e5\u865a\u62df\u673a\u4f55\u65f6\u56de\u6536\u4e00\u4e2aJNI\u53d8\u91cf<\/p>\n<h3>\u5c40\u90e8\u5f15\u7528<\/h3>\n<pre><code class=\"language-c\">\/\/\u5c40\u90e8\u5f15\u7528\uff0c\u901a\u8fc7DeleteLocalRef\u624b\u52a8\u91ca\u653e\u5bf9\u8c61\n\/\/1.\u8bbf\u95ee\u4e00\u4e2a\u5f88\u5927\u7684java\u5bf9\u8c61\uff0c\u4f7f\u7528\u5b8c\u4e4b\u540e\uff0c\u8fd8\u8981\u8fdb\u884c\u590d\u6742\u7684\u8017\u65f6\u64cd\u4f5c\n\/\/2.\u521b\u5efa\u4e86\u5927\u91cf\u7684\u5c40\u90e8\u5f15\u7528\uff0c\u5360\u7528\u4e86\u592a\u591a\u7684\u5185\u5b58\uff0c\u800c\u4e14\u8fd9\u4e9b\u5c40\u90e8\u5f15\u7528\u8ddf\u540e\u9762\u7684\u64cd\u4f5c\u6ca1\u6709\u5173\u8054\u6027\n\n\/\/\u6a21\u62df\uff1a\u5faa\u73af\u521b\u5efa\u6570\u7ec4\nJNIEXPORT void JNICALL Java_cn_appblog_jni_JniTest_localRef(JNIEnv *env, jobject jobj) {\n    int i = 0;\n    for (; i &lt; 5; i++) {\n        \/\/\u521b\u5efaDate\u5bf9\u8c61\n        jclass cls = (*env)-&gt;FindClass(env, &quot;java\/util\/Date&quot;);\n        jmethodID constructor_mid = (*env)-&gt;GetMethodID(env, cls, &quot;&lt;init&gt;&quot;, &quot;()V&quot;);\n        jobject obj = (*env)-&gt;NewObject(env, cls, constructor_mid);\n        \/\/\u6b64\u5904\u7701\u7565\u4e00\u767e\u884c\u4ee3\u7801...\n\n        \/\/\u4e0d\u518d\u4f7f\u7528jobject\u5bf9\u8c61\n        \/\/\u901a\u77e5\u5783\u573e\u56de\u6536\u5668\u56de\u6536\u8fd9\u4e9b\u5bf9\u8c61\n        (*env)-&gt;DeleteLocalRef(env, obj);\n        \/\/\u6b64\u5904\u7701\u7565\u4e00\u767e\u884c\u4ee3\u7801...\n    }\n}<\/code><\/pre>\n<h3>\u5168\u5c40\u5f15\u7528<\/h3>\n<pre><code class=\"language-c\">\/\/\u5168\u5c40\u5f15\u7528\n\/\/\u5171\u4eab(\u53ef\u4ee5\u8de8\u591a\u4e2a\u7ebf\u7a0b)\uff0c\u624b\u52a8\u63a7\u5236\u5185\u5b58\u4f7f\u7528\njstring global_str;\n\n\/\/\u521b\u5efa\nJNIEXPORT void JNICALL Java_cn_appblog_jni_JniTest_createGlobalRef(JNIEnv *env, jobject jobj) {\n    jstring obj = (*env)-&gt;NewStringUTF(env, &quot;Jni development is powerful!&quot;);\n    global_str = (*env)-&gt;NewGlobalRef(env, obj);\n}\n\n\/\/\u83b7\u5f97\nJNIEXPORT jstring JNICALL Java_cn_appblog_jni_JniTest_getGlobalRef(JNIEnv *env, jobject jobj) {\n    return global_str;\n}\n\n\/\/\u91ca\u653e\nJNIEXPORT void JNICALL Java_cn_appblog_jni_JniTest_deleteGlobalRef(JNIEnv *env, jobject jobj) {\n    (*env)-&gt;DeleteGlobalRef(env, global_str);\n}<\/code><\/pre>\n<h3>\u5f31\u5168\u5c40\u5f15\u7528<\/h3>\n<pre><code>\/\/\u8282\u7701\u5185\u5b58\uff0c\u5728\u5185\u5b58\u4e0d\u8db3\u65f6\u53ef\u4ee5\u662f\u91ca\u653e\u6240\u5f15\u7528\u7684\u5bf9\u8c61\n\/\/\u53ef\u4ee5\u5f15\u7528\u4e00\u4e2a\u4e0d\u5e38\u7528\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u4e3aNULL\uff0c\u4e34\u65f6\u521b\u5efa\n\/\/\u521b\u5efa\uff1aNewWeakGlobalRef\uff0c\u9500\u6bc1\uff1aDeleteGlobalWeakRef<\/code><\/pre>\n<h3>Java\u8c03\u7528<\/h3>\n<pre><code class=\"language-c\">public class JniTest {\n\n    public native void localRef();\n\n    public native void createGlobalRef();\n    public native String getGlobalRef();\n    public native void deleteGlobalRef();\n\n    public static void main(String[] args) {\n        JniTest t = new JniTest();\n        t.createGlobalRef();\n        System.out.println(t.getGlobalRef());\n        \/\/\u7528\u5b8c\u4e4b\u540e\u91ca\u653e\n        t.deleteGlobalRef();\n        System.out.println(&quot;\u5df2\u91ca\u653e&quot;);\n        \/\/System.out.println(t.getGlobalRef());\n    }\n\n    \/\/\u52a0\u8f7d\u52a8\u6001\u5e93\n    static {    \n        System.loadLibrary(&quot;JniTest&quot;);\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JNI\u6570\u7ec4\u5904\u7406 \u4f20\u5165\u6570\u7ec4 public class JniTest { public native void  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[133],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-android-ndk","tag-jni"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=412"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}