{"id":410,"date":"2023-02-25T10:55:16","date_gmt":"2023-02-25T02:55:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=410"},"modified":"2023-04-29T21:29:16","modified_gmt":"2023-04-29T13:29:16","slug":"android-ndk-basic-jni-development-process-jnienv","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-jni-development-process-jnienv\/","title":{"rendered":"Android NDK\u57fa\u784010\uff1aJNI\u5f00\u53d1\u6d41\u7a0b_JNIEnv"},"content":{"rendered":"<h2>JNI<\/h2>\n<p>JNI\uff08Java Native Interface\uff09<br \/>\nJava\u8c03\u7528C\/C++\uff0cC\/C++\u8c03\u7528Java\u7684\u4e00\u5957API<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>1.\u7f16\u5199native\u65b9\u6cd5<\/li>\n<li>2.javah\u547d\u4ee4\uff0c\u751f\u6210.h\u5934\u6587\u4ef6\uff0c\u751f\u6210\u7684C\/C++\u7684\u51fd\u6570\u58f0\u660e\u540d\u79f0\uff1aJava_\u5b8c\u6574\u7c7b\u540d_\u51fd\u6570\u540d<\/li>\n<li>3.\u590d\u5236.h\u5934\u6587\u4ef6\u5230C\/C++\u5de5\u7a0b\u4e2d<\/li>\n<li>4.\u590d\u5236jni.h\u548cjni_md.h\u6587\u4ef6\u5230C\/C++\u5de5\u7a0b\u4e2d<\/li>\n<li>5.\u5b9e\u73b0.h\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\u51fd\u6570<\/li>\n<li>6.\u751f\u6210dll\u6587\u4ef6<\/li>\n<li>7.\u5c06dll\u6587\u4ef6\u653e\u7f6e\u5728\u9879\u76ee\u5de5\u7a0b\u6839\u76ee\u5f55\u4e0b<\/li>\n<li>8.\u91cd\u542fEclipse<\/li>\n<\/ul>\n<pre><code>jni.h\uff1aC:\\Program Files\\Java\\jdk1.8.0_112\\include\\jni.h\njni_md.h\uff1aC:\\Program Files\\Java\\jdk1.8.0_112\\include\\win32\\jni_md.h<\/code><\/pre>\n<p>cn.appblog.JniTest<\/p>\n<pre><code class=\"language-c\">public class JniTest {\n\n    public native static String getStringFromC();\n\n    public static void main(String[] args) {\n        String text = getStringFromC();\n        System.out.println(text);\n    }\n\n    \/\/\u52a0\u8f7d\u52a8\u6001\u5e93\n    static {\n        System.loadLibrary(&quot;JniTest&quot;);\n        \/\/System.load(&quot;D:\/NDK\/JniTest\/JniTest.dll&quot;);\n    }\n\n}<\/code><\/pre>\n<p>cn_appblog_jni_JniTest.h<\/p>\n<pre><code class=\"language-c\">\/* DO NOT EDIT THIS FILE - it is machine generated *\/\n#include &lt;jni.h&gt;\n\/* Header for class cn_appblog_jni_JniTest *\/\n\n#ifndef _Included_cn_appblog_jni_JniTest\n#define _Included_cn_appblog_jni_JniTest\n#ifdef __cplusplus\nextern &quot;C&quot; {\n#endif\n\/*\n * Class:     cn_appblog_jni_JniTest\n * Method:    getStringFromC\n * Signature: ()Ljava\/lang\/String;\n *\/\nJNIEXPORT jstring JNICALL Java_cn_appblog_jni_JniTest_getStringFromC\n  (JNIEnv *, jclass);\n\n#ifdef __cplusplus\n}\n#endif\n#endif<\/code><\/pre>\n<p>C\u5b9e\u73b0\uff1amain.c<\/p>\n<pre><code class=\"language-c\">\/\/\u751f\u6210DLL\u52a8\u6001\u94fe\u63a5\u5e93\uff1a\n\/\/\u5e73\u53f0\u9009\u62e9x64\u6216x86\n\/\/\u9879\u76ee\u5c5e\u6027-&gt;\u914d\u7f6e\u5c5e\u6027-&gt;\u5e38\u89c4-&gt;\u9879\u76ee\u9ed8\u8ba4\u503c-&gt;\u914d\u7f6e\u7c7b\u578b\uff1a\u52a8\u6001\u5e93(.dll)\n#include &quot;cn_appblog_jni_JniTest.h&quot;\n\n\/\/\u51fd\u6570\u5b9e\u73b0\nJNIEXPORT jstring JNICALL Java_cn_appblog_jni_JniTest_getStringFromC\n  (JNIEnv *env, jclass jcls) {\n    \/\/JNIEnv \u7ed3\u6784\u4f53\u6307\u9488\n    \/\/env \u4e8c\u7ea7\u6307\u9488\n    \/\/\u4ee3\u8868Java\u8fd0\u884c\u73af\u5883\uff0c\u8c03\u7528Java\u4e2d\u7684\u4ee3\u7801\n\n    \/\/\u7b80\u5355\u7684\u5b9e\u73b0\n    \/\/\u5c06C\u7684\u5b57\u7b26\u4e32\u8f6c\u4e3a\u4e00\u4e2ajava\u5b57\u7b26\u4e32\n    return (*env)-&gt;NewStringUTF(env, &quot;C String&quot;);\n}<\/code><\/pre>\n<p>C++\u5b9e\u73b0\uff1amain.cpp<\/p>\n<pre><code class=\"language-c\">#include &quot;cn_appblog_jni_JniTest.h&quot;\n\nJNIEXPORT jstring JNICALL Java_cn_appblog_jni_JniTest_getStringFromC\n  (JNIEnv *env, jclass jcls) {\n    \/\/JNIEnv \u5728C++\u4e2d\u5c31\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u522b\u540d\n    \/\/env \u5728C++\u4e2d\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u4e00\u7ea7\u6307\u9488\n    return env-&gt;NewStringUTF(&quot;C String&quot;);\n}<\/code><\/pre>\n<h2>\u52a8\u6001\u5e93\u4e0e\u9759\u6001\u5e93<\/h2>\n<ul>\n<li>\u9759\u6001\u5e93\uff1a\u5728\u7f16\u8bd1\u7684\u65f6\u5019\u52a0\u8f7d\u751f\u6210\u76ee\u6807\u6587\u4ef6\uff0c\u5728\u8fd0\u884c\u65f6\u4e0d\u7528\u52a0\u8f7d\u5e93\uff0c\u5728\u8fd0\u884c\u65f6\u5bf9\u5e93\u6ca1\u6709\u4f9d\u8d56\u6027\u3002 <\/li>\n<li>\u52a8\u6001\u5e93\uff1a\u5728\u76ee\u6807\u6587\u4ef6\u8fd0\u884c\u65f6\u52a0\u8f7d\uff0c\u624b\u52a8\u52a0\u8f7d\uff0c\u4e14\u5bf9\u5e93\u6709\u4f9d\u8d56\u6027\u3002<\/li>\n<\/ul>\n<p>\u7b80\u5355\u8bb2\uff0c\u9759\u6001\u5e93\u5c31\u662f\u76f4\u63a5\u5c06\u9700\u8981\u7684\u4ee3\u7801\u8fde\u63a5\u8fdb\u53ef\u6267\u884c\u7a0b\u5e8f\uff1b\u52a8\u6001\u5e93\u5c31\u662f\u5728\u9700\u8981\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u65f6\uff0c\u6839\u636e\u51fd\u6570\u6620\u5c04\u8868\u627e\u5230\u8be5\u51fd\u6570\u7136\u540e\u8c03\u5165\u5806\u6808\u6267\u884c\u3002<\/p>\n<p>\u505a\u6210\u9759\u6001\u5e93\u53ef\u6267\u884c\u6587\u4ef6\u672c\u8eab\u6bd4\u8f83\u5927\uff0c\u4f46\u4e0d\u5fc5\u9644\u5e26\u52a8\u6001\u5e93\uff1b\u505a\u6210\u52a8\u6001\u5e93\u53ef\u6267\u884c\u6587\u4ef6\u672c\u8eab\u6bd4\u8f83\u5c0f\uff0c\u4f46\u9700\u8981\u9644\u5e26\u52a8\u6001\u5e93<\/p>\n<p>Windows\u4e2d\u9759\u6001\u5e93\u662f\u4ee5 .lib \u4e3a\u540e\u7f00\u7684\u6587\u4ef6\uff0c\u5171\u4eab\u5e93\u662f\u4ee5 .dll \u4e3a\u540e\u7f00\u7684\u6587\u4ef6\u3002<\/p>\n<p>\u5728Linux\u4e2d\u9759\u6001\u5e93\u662f\u4ee5 .a \u4e3a\u540e\u7f00\u7684\u6587\u4ef6\uff0c\u5171\u4eab\u5e93\u662f\u4ee5 .so\u4e3a\u540e\u7f00\u7684\u6587\u4ef6\u3002<\/p>\n<h2>JNIEnv<\/h2>\n<p>\u5728C\u4e2d\uff1aJNIEnv \u7ed3\u6784\u4f53\u6307\u9488\u522b\u540d\uff0c\u53c2\u6570env\u662f\u4e8c\u7ea7\u6307\u9488<\/p>\n<p>\u5728C++\u4e2d\uff1aJNIEnv \u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u522b\u540d\uff0c\u53c2\u6570env\u662f\u4e00\u7ea7\u6307\u9488<\/p>\n<ul>\n<li>1.\u4e3a\u4ec0\u4e48\u9700\u8981\u4f20\u5165JNIEnv\uff0c\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\u4e2d\u9700\u8981JNIEnv<\/li>\n<li>2.C++\u4e3a\u4ec0\u4e48\u6ca1\u6709\u4f20\u5165\uff1f\u56e0\u4e3a\u5b58\u5728this\u5bf9\u8c61\u76f4\u63a5\u8c03\u7528<\/li>\n<li>3.C++\u53ea\u662f\u5bf9C\u7684\u90a3\u4e00\u5957\u8fdb\u884c\u7684\u5c01\u88c5\uff0c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c\u4e3a\u6307\u9488\uff0c\u8fd9\u4e2a\u53d8\u91cf\u662f\u4e8c\u7ea7\u6307\u9488<\/li>\n<\/ul>\n<p>C\/C++\u4e2d\u4e3a\u4ec0\u4e48\u6709\u533a\u522b\uff1f<\/p>\n<pre><code class=\"language-c\">\/\/JNIEnv\u7ed3\u6784\u4f53\u7684\u6307\u9488\u522b\u540d\ntypedef struct JNINativeInterface_* JNIEnv;\n\n\/\/\u7ed3\u6784\u4f53\nstruct JNINativeInterface_ {\n    char* (*NewStringUTF)(JNIEnv*, char*);\n};\n\n\/\/\u51fd\u6570\u5b9e\u73b0\nchar* NewStringUTF(JNIEnv* env, char* str) {\n    \/\/\u5728NewStringUTF\u6267\u884c\u8fc7\u7a0b\uff0c\u4ecd\u7136\u9700\u8981JNIEnv\n    return str;\n}\n\nvoid main() {\n    \/\/\u5b9e\u4f8b\u5316\u7ed3\u6784\u4f53\n    struct JNINativeInterface_ struct_env;\n    struct_env.NewStringUTF = NewStringUTF;\n\n    \/\/\u7ed3\u6784\u4f53\u6307\u9488\n    JNIEnv e = &amp;struct_env;\n\n    \/\/\u7ed3\u6784\u4f53\u7684\u4e8c\u7ea7\u6307\u9488\n    JNIEnv *env = &amp;e;\n\n    \/\/\u901a\u8fc7\u4e8c\u7ea7\u6307\u9488\u8c03\u7528\u51fd\u6570\n    char* str = (*env)-&gt;NewStringUTF(env,&quot;abc&quot;);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JNI JNI\uff08Java Native Interface\uff09 Java\u8c03\u7528C\/C++\uff0cC\/C++\u8c03\u7528Java\u7684 [&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-410","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\/410","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=410"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}