{"rsdb":{"rid":"387730","subhead":"","postdate":"0","aid":"270084","fid":"98","uid":"1","topic":"1","content":"
\n

\u76ee\u5f55<\/p> \n

\u7528\u6cd5\u89e3\u6790<\/a>
\u251c\u2500\u2500 1\u3001JNI\u51fd\u6570<\/a>
\u2502 \u251c\u2500\u2500 1.1\u3001extern "C"<\/a>
\u2502 \u251c\u2500\u2500 1.2\u3001JNIEXPORT\u3001JNICALL<\/a>
\u2502 \u251c\u2500\u2500 1.3\u3001\u51fd\u6570\u540d<\/a>
\u2502 \u251c\u2500\u2500 1.4\u3001JNIEnv<\/a>
\u2502 \u251c\u2500\u2500 1.5\u3001jobject<\/a>
\u251c\u2500\u2500 2\u3001Java\u3001JNI\u3001C\/C++<\/a>\u57fa\u672c\u7c7b\u578b\u6620\u5c04\u5173\u7cfb<\/a>
\u251c\u2500\u2500 3\u3001JNI\u63cf\u8ff0\u7b26\uff08\u7b7e\u540d\uff09<\/a>
\u251c\u2500\u2500 4\u3001\u51fd\u6570\u9759\u6001\u6ce8\u518c\u3001\u52a8\u6001\u6ce8\u518c<\/a>
\u2502 \u251c\u2500\u2500 4.1\u3001\u52a8\u6001\u6ce8\u518c\u539f\u7406<\/a>
\u2502 \u251c\u2500\u2500 4.2\u3001\u9759\u6001\u6ce8\u518c\u539f\u7406<\/a>
\u2502 \u251c\u2500\u2500 4.3\u3001Java\u8c03\u7528native\u7684\u6d41\u7a0b<\/a><\/p> \n

\u5f53\u901a\u8fc7AndroidStudio\u521b\u5efa\u4e86Native C++<\/a>\u5de5\u7a0b\u540e\uff0c\u9996\u5148\u9762\u5bf9\u7684\u662f*.cpp\u6587\u4ef6\uff0c\u5bf9\u4e8e\u4e0d\u719f\u6089C\/C++\u7684\u5f00\u53d1\u4eba\u5458\u800c\u8a00\uff0c\u5f80\u5f80\u662f\u671b\u201c\u7c7b\u201d\u5174\u53f9\uff0c\u65e0\u4ece\u4e0b\u624b\u3002\u4e3a\u6b64\uff0c\u54b1\u4eec\u7cfb\u7edf\u7684\u68b3\u7406\u4e00\u4e0bJNI\u7684\u7528\u6cd5\uff0c\u4e3a\u540e\u7eedNative\u5f00\u53d1\u505a\u94fa\u57ab\u3002<\/p> \n

1\u3001JNI\u51fd\u6570<\/h4> \n
#include <jni.h>\n#include <string>\n\nextern "C" JNIEXPORT jstring JNICALL\nJava_com_qxc_testnativec_MainActivity_stringFromJNI(\n        JNIEnv* env,\n        jobject \/* this *\/) {\n    std::string hello = "Hello from C++";\n    return env->NewStringUTF(hello.c_str());\n}<\/code><\/pre> \n 

\u901a\u5e38\uff0c\u5927\u5bb6\u770b\u5230\u7684JNI\u65b9\u6cd5\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u65b9\u6cd5\u7ed3\u6784\u4e0eJava\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u540c\u6837\u5305\u542b\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u3001\u8fd4\u56de\u7c7b\u578b\uff0c\u53ea\u4e0d\u8fc7\u591a\u4e86\u4e00\u4e9b\u4fee\u9970\u8bcd\u3001\u7279\u5b9a\u53c2\u6570\u7c7b\u578b\u800c\u5df2\u3002<\/p> \n

1.1\u3001extern "C"<\/h6> \n
\n

\u4f5c\u7528\uff1a\u907f\u514d\u7f16\u7ece\u5668\u6309\u7167C++\u7684\u65b9\u5f0f\u53bb\u7f16\u7eceC\u51fd\u6570<\/p> \n <\/blockquote> \n

\u8be5\u5173\u952e\u5b57\u53ef\u4ee5\u5220\u6389\u5417\uff1f
\u6211\u4eec\u4e0d\u59a8\u52a8\u624b\u6d4b\u8bd5\u4e00\u4e0b\uff1a\u53bb\u6389extern \u201cC\u201d , \u91cd\u65b0\u751f\u6210so\uff0c\u8fd0\u884capp\uff0c\u7ed3\u679c\u76f4\u63a5\u95ea\u9000\u4e86\uff1a<\/p> \n

<\/p> \n

\u54b1\u4eec\u53cd\u7f16\u8bd1so\u6587\u4ef6\u770b\u4e00\u4e0b\uff0c\u539f\u6765\u53bb\u6389extern \u201cC\u201d \u540e\uff0c\u51fd\u6570\u540d\u5b57\u7adf\u7136\u88ab\u4fee\u6539\u4e86\uff1a<\/p> \n

\/\/\u4fdd\u7559extern "C"\n000000000000ea98 T \nJava_com_qxc_testnativec_MainActivity_stringFromJNI\n\n\/\/\u53bb\u6389extern "C"\n000000000000eab8 T \n_Z40Java_com_qxc_testnativec_MainActivity_stringFromJNIP7_JNIEnvP8_jobject<\/code><\/pre> \n 

\u539f\u56e0\u662f\u4ec0\u4e48\u5462\uff1f
\u5176\u5b9e\u8fd9\u8ddfC\u548cC++\u7684\u51fd\u6570\u91cd\u8f7d\u5dee\u5f02\u6709\u5173\u7cfb\uff1a<\/p> \n

1\u3001C\u4e0d\u652f\u6301\u51fd\u6570\u7684\u91cd\u8f7d\uff0c\u7f16\u8bd1\u4e4b\u540e\u51fd\u6570\u540d\u4e0d\u53d8\uff1b\n2\u3001C++\u652f\u6301\u51fd\u6570\u7684\u91cd\u8f7d\uff08\u8fd9\u70b9\u4e0eJava\u4e00\u81f4\uff09\uff0c\u7f16\u8bd1\u4e4b\u540e\u51fd\u6570\u540d\u4f1a\u6539\u53d8\uff1b\n\n\u539f\u56e0\uff1a\u5728C++\u4e2d\uff0c\u5b58\u5728\u51fd\u6570\u7684\u91cd\u8f7d\u95ee\u9898\uff0c\u51fd\u6570\u7684\u8bc6\u522b\u65b9\u5f0f\u662f\u901a\u8fc7\uff1a\u51fd\u6570\u540d\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u51fd\u6570\u53c2\u6570\u5217\u8868\n\u4e09\u8005\u7ec4\u5408\u6765\u5b8c\u6210\u7684\u3002<\/code><\/pre> \n 

\u6240\u4ee5\uff0c\u5982\u679c\u5e0c\u671b\u7f16\u8bd1\u540e\u7684\u51fd\u6570\u540d\u4e0d\u53d8\uff0c\u5e94\u901a\u77e5\u7f16\u8bd1\u5668\u4f7f\u7528C\u7684\u7f16\u8bd1\u65b9\u5f0f\u7f16\u8bd1\u8be5\u51fd\u6570\uff08\u5373\uff1a\u52a0\u4e0a\u5173\u952e\u5b57\uff1aextern \u201cC\u201d\uff09\u3002<\/p> \n

\u6269\u5c55\uff1a\n\u5982\u679c\u5373\u60f3\u53bb\u6389\u5173\u952e\u5b57 extern \u201cC\u201d\uff0c\u53c8\u5e0c\u671b\u65b9\u6cd5\u80fd\u88ab\u6b63\u5e38\u8c03\u7528\uff0c\u771f\u7684\u4e0d\u80fd\u5b9e\u73b0\u5417\uff1f\n\n\u975e\u4e5f\uff0c\u8fd8\u662f\u6709\u89e3\u51b3\u529e\u6cd5\u7684\uff1a\u201c\u51fd\u6570\u7684\u52a8\u6001\u6ce8\u518c\u201d\uff0c\u8fd9\u4e2a\u540e\u9762\u518d\u4ecb\u7ecd\u5427\uff01\uff01\uff01<\/code><\/pre> \n 
1.2\u3001JNIEXPORT\u3001JNICALL<\/h6> \n
\n
\u4f5c\u7528\uff1a<\/h6> \n

JNIEXPORT \u7528\u6765\u8868\u793a\u8be5\u51fd\u6570\u662f\u5426\u53ef\u5bfc\u51fa\uff08\u5373\uff1a\u65b9\u6cd5\u7684\u53ef\u89c1\u6027\uff09
JNICALL \u7528\u6765\u8868\u793a\u51fd\u6570\u7684\u8c03\u7528\u89c4\u8303\uff08\u5982\uff1a__stdcall\uff09<\/p> \n <\/blockquote> \n

\u6211\u4eec\u901a\u8fc7JNIEXPORT\u3001JNICALL\u5173\u952e\u5b57\u8df3\u8f6c\u5230jni.h\u4e2d\u7684\u5b9a\u4e49\uff0c\u5982\u4e0b\u56fe\uff1a<\/p> \n

<\/p> \n

\u901a\u8fc7\u67e5\u770b jni.h \u4e2d\u7684\u6e90\u7801\uff0c\u539f\u6765JNIEXPORT\u3001JNICALL\u662f\u4e24\u4e2a\u5b8f\u5b9a\u4e49<\/p> \n

\u5bf9\u4e8e\u5b89\u5353\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u5b8f\u53ef\u8fd9\u6837\u7406\u89e3\uff1a\n\n\u251c\u2500\u2500 \u5b8f JNIEXPORT \u4ee3\u8868\u7684\u5c31\u662f\u53f3\u4fa7\u7684\u8868\u8fbe\u5f0f\uff1a __attribute__ ((visibility ("default")))\n\u251c\u2500\u2500 \u6216\u8005\u4e5f\u53ef\u4ee5\u8bf4\uff1a JNIEXPORT \u662f\u53f3\u4fa7\u8868\u8fbe\u5f0f\u7684\u522b\u540d\n\n\u5b8f\u53ef\u8868\u8fbe\u7684\u5185\u5bb9\u5f88\u591a\uff0c\u5982\uff1a\u4e00\u4e2a\u5177\u4f53\u7684\u6570\u503c\u3001\u4e00\u4e2a\u89c4\u5219\u3001\u4e00\u6bb5\u903b\u8f91\u4ee3\u7801\u7b49\uff1b<\/code><\/pre> \n 

attribute___((visibility ("default"))) \u63cf\u8ff0\u7684\u662f\u201c\u53ef\u89c1\u6027\u201d\u5c5e\u6027 visibility<\/p> \n

1\u3001default \uff1a\u8868\u793a\u5916\u90e8\u53ef\u89c1\uff0c\u7c7b\u4f3c\u4e8epublic\u4fee\u9970\u7b26 (\u5373\uff1a\u53ef\u4ee5\u88ab\u5916\u90e8\u8c03\u7528)\n2\u3001hidden \uff1a\u8868\u793a\u9690\u85cf\uff0c\u7c7b\u4f3c\u4e8eprivate\u4fee\u9970\u7b26 (\u5373\uff1a\u53ea\u80fd\u88ab\u5185\u90e8\u8c03\u7528)\n3\u3001\u5176\u4ed6 \uff1a\u7565<\/code><\/pre> \n 

\u5982\u679c\uff0c\u6211\u4eec\u60f3\u4f7f\u7528hidden\uff0c\u9690\u85cf\u6211\u4eec\u5199\u7684\u65b9\u6cd5\uff0c\u53ef\u8fd9\u4e48\u5199\uff1a<\/p> \n

#include <jni.h>\n#include <string>\n\nextern "C" __attribute__ ((visibility ("hidden"))) jstring JNICALL\nJava_com_qxc_testnativec_MainActivity_stringFromJNI(\n        JNIEnv* env,\n        jobject \/* this *\/) {\n    std::string hello = "Hello from C++";\n    return env->NewStringUTF(hello.c_str());\n}<\/code><\/pre> \n 

\u91cd\u65b0\u7f16\u8bd1\u3001\u8fd0\u884c\uff0c\u7ed3\u679c\u95ea\u9000\u4e86\u3002
\u539f\u56e0\uff1a\u51fd\u6570Java_com_qxc_testnativec_MainActivity_stringFromJNI\u5df2\u88ab\u9690\u85cf\uff0c\u800c\u6211\u4eec\u5728java<\/a>\u4e2d\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u627e\u4e0d\u5230\u8be5\u51fd\u6570\uff0c\u6240\u4ee5\u629b\u51fa\u4e86\u5f02\u5e38\uff0c\u5982\u4e0b\u56fe\uff1a<\/p> \n

<\/p> \n

\u5b8fJNICALL \u53f3\u8fb9\u662f\u7a7a\u7684\uff0c\u8bf4\u660e\u53ea\u662f\u4e2a\u7a7a\u5b9a\u4e49\u3002\u4e0a\u9762\u8bb2\u4e86\uff0c\u5b8fJNICALL\u4ee3\u8868\u7684\u662f\u53f3\u8fb9\u5b9a\u4e49\u7684\u5185\u5bb9\uff0c\u90a3\u4e48\uff0c\u6211\u4eec\u4ee3\u7801\u4e5f\u53ef\u76f4\u63a5\u4f7f\u7528\u53f3\u8fb9\u7684\u5185\u5bb9\uff08\u7a7a\uff09\u66ff\u6362\u8c03JNICALL\uff08\u5373\uff1a\u53bb\u6389JNICALL\u5173\u952e\u5b57\uff09\uff0c\u7f16\u8bd1\u540e\u8fd0\u884c\uff0c\u8c03\u7528so\u4ecd\u7136\u662f\u6b63\u786e\u7684\uff1a<\/p> \n

#include <jni.h>\n#include <string>\n\nextern "C" JNIEXPORT jstring\nJava_com_qxc_testnativec_MainActivity_stringFromJNI(\n        JNIEnv* env,\n        jobject \/* this *\/) {\n    std::string hello = "Hello from C++";\n    return env->NewStringUTF(hello.c_str());\n}<\/code><\/pre> \n 
JNICALL \u77e5\u8bc6\u6269\u5c55\uff1a\n\nJNICALL\u7684\u5b9a\u4e49\uff0c\u5e76\u975e\u6240\u6709\u5e73\u53f0\u90fd\u50cfLinux\u4e00\u6837\u662f\u7a7a\u7684\uff0c\u5982windows\u5e73\u53f0\uff1a\n#ifndef _JAVASOFT_JNI_MD_H_  \n#define _JAVASOFT_JNI_MD_H_  \n#define JNIEXPORT __declspec(dllexport)  \n#define JNIIMPORT __declspec(dllimport)  \n#define JNICALL __stdcall  \ntypedef long jint;  \ntypedef __int64 jlong;  \ntypedef signed char jbyte;  \n#endif<\/code><\/pre> \n 
1.3\u3001\u51fd\u6570\u540d<\/h6> \n

\u770b\u5230.cpp\u4e2d\u7684\u51fd\u6570"Java_com_qxc_testnativec_MainActivity_stringFromJNI"\uff0c\u5927\u90e8\u5206\u5f00\u53d1\u4eba\u5458\u90fd\u4f1a\u6709\u7591\u95ee\uff1a\u6211\u4eec\u5b9a\u4e49\u7684native\u51fd\u6570\u540dstring","orderid":"0","title":"\u5b89\u5353JNI\u7cbe\u7ec6\u5316\u8bb2\u89e3\uff0c\u8ba9\u4f60\u5f7b\u5e95\u4e86\u89e3JNI\uff08\u4e8c\uff09\uff1a\u7528\u6cd5\u89e3\u6790(\u4e00)","smalltitle":"","mid":"0","fname":"Android","special_id":"0","bak_id":"0","info":"0","hits":"49","pages":"5","comments":"0","posttime":"2019-11-22 18:12:19","list":"1574417539","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u5b89\u5353<\/A> JNI<\/A> \u7cbe\u7ec6\u5316<\/A> \u8bb2\u89e3<\/A> \u5f7b\u5e95<\/A> \u4e86\u89e3<\/A> \u7528\u6cd5<\/A> \u89e3\u6790<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.37","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u5b89\u5353JNI\u7cbe\u7ec6\u5316\u8bb2\u89e3\uff0c\u8ba9\u4f60\u5f7b\u5e95\u4e86\u89e3JNI\uff08\u4e8c\uff09\uff1a\u7528\u6cd5\u89e3\u6790","lastview":"1576096531","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}