{"id":6658,"date":"2023-08-17T11:07:29","date_gmt":"2023-08-17T03:07:29","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=6658"},"modified":"2023-08-17T11:07:29","modified_gmt":"2023-08-17T03:07:29","slug":"c%e8%af%ad%e8%a8%80%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app%e6%96%b9%e6%b3%95%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/6658\/","title":{"rendered":"c+\u8bed\u8a00\u5f00\u53d1\u5b89\u5353app\u65b9\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"

\u5728\u5b89\u5353\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528Java\u548cKotlin\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u662f\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u4f46\u662f\uff0c\u6709\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528C\u6216C++\u7f16\u5199\u4e00\u4e9b\u6838\u5fc3\u7b97\u6cd5\u6216\u5e93\uff0c\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u6216\u590d\u7528\u73b0\u6709\u7684C\/C++\u4ee3\u7801\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5728\u5b89\u5353\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528C\/C++\u4ee3\u7801\u5462\uff1f\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u8981\u7684\u4ecb\u7ecd\u3002<\/p>\n

\u5b89\u5353\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u79f0\u4e3aNDK\uff08Native Development Kit\uff09\u7684\u5de5\u5177\u5305\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b89\u5353\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528C\/C++\u4ee3\u7801\u3002NDK\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u7684\u5de5\u5177\u548c\u5e93\uff0c\u534f\u52a9\u6211\u4eec\u5c06C\/C++\u4ee3\u7801\u7f16\u8bd1\u6210\u53ef\u5728\u5b89\u5353\u8bbe\u5907\u4e0a\u8fd0\u884c\u7684\u672c\u5730\u5e93\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u5c06C\/C++\u4ee3\u7801\u7f16\u5199\u5230\u4e00\u4e2a\u5355\u72ec\u7684C\/C++\u6e90\u6587\u4ef6\u4e2d\uff08\u6bd4\u5982\uff1a.c\uff0c.cpp\uff0c.cc\u7b49\uff09\uff0c\u7f16\u8bd1\u751f\u6210\u76f8\u5e94\u7684\u672c\u5730\u5e93\u6587\u4ef6\uff08\u6bd4\u5982\uff1a.so\u6587\u4ef6\uff09\uff0c\u7136\u540e\u5728Java\u4ee3\u7801\u4e2d\u901a\u8fc7JNI\uff08Java Native Interface\uff09\u63a5\u53e3\u8c03\u7528\u76f8\u5e94\u7684C\/C++\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u6848\u4f8b\uff1a<\/p>\n

C++\u6e90\u6587\u4ef6\uff08test.cpp\uff09\uff1a<\/p>\n

“`cpp<\/p>\n

#include <\/p>\n

JNIEXPORT jstring JNICALL<\/p>\n

Java_com_example_myapplication_MainActivity_helloFromCpp(JNIEnv *env, jobject instance) {<\/p>\n

return env->NewStringUTF(“Hello from C++”);<\/p>\n

}<\/p>\n

“`<\/p>\n

Java\u6e90\u6587\u4ef6\uff08MainActivity.java\uff09\uff1a<\/p>\n

“`java<\/p>\n

public class MainActivity extends AppCompatActivity {<\/p>\n

static {<\/p>\n

System.loadLibrary(“test”);<\/p>\n

}<\/p>\n

public native String helloFromCpp();<\/p>\n

@Override<\/p>\n

protec<\/p>\n

<\/figure>\n<\/p>\n

ted void onCreate(Bundle savedInstanceState) {<\/p>\n

super.onCreate(savedInstanceState);<\/p>\n

setContentView(R.layout.activity_main);<\/p>\n<\/p>\n

String strFromCpp = helloFromCpp();<\/p>\n

Log.d(“MyApp”, strFromCpp);<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u6ce8\u610f\uff1a<\/p>\n

– C++\u4ee3\u7801\u4e2d\u7528\u5230\u7684JNI\u51fd\u6570\u9700\u8981\u8fdb\u884c\u58f0\u660e\uff0c\u4ee5\u65b9\u4fbfJava\u4ee3\u7801\u8c03\u7528\uff1b<\/p>\n

– \u5728Java\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b89\u5353app\u5f00\u53d1\u5de5\u5177<\/a>`System.loadLibrary()`\u65b9\u6cd5\u52a0\u8f7d\u672c\u5730\u5e93\u6587\u4ef6\uff1b<\/p>\n

– Java\u4ee3\u7801\u4e2d\u901a\u8fc7`native`\u6807\u8bb0\u58f0\u660e\u672c\u5730\u65b9\u6cd5\uff1b<\/p>\n

– Java\u4ee3\u7801\u4e2d\u4f7f\u7528JNI\u51fd\u6570\u8c03\u7528C++\u5b89\u5353APP\u5f00\u53d1<\/a>\u4ee3\u7801\u3002<\/p>\n

\u4ee5\u4e0a\u662f\u4f7f\u7528NDK\u5c06C\/C++\u4ee3\u7801\u96c6\u6210\u5230\u5b89\u5353\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u601d\u8def\u548c\u6b65\u9aa4\u3002\u5f53\u7136\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u8fd8\u9700\u8981\u8003\u8651\u5f88\u591a\u65b9\u9762\uff0c\u6bd4\u5982\u5e93\u7684\u6784\u5efa\u548c\u94fe\u63a5\u3001\u5f02\u5e38\u5904\u7406\u3001\u5185\u5b58\u7ba1\u7406\u7b49\u7b49\u3002\u540c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5404\u7c7b\u6846\u67b6\u548c\u5de5\u5177\u7b80\u5316\u96c6\u6210\u6d41\u7a0b\uff0c\u6bd4\u5982\uff1aCMake\u3001Gradle\u548cCLion\u7b49\u3002<\/p>\n

\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u4e86\u89e3\u5728\u5b89\u5353\u5f00\u53d1\u4e2d\u4f7f\u7528C\/C++\u7684\u57fa\u672c\u539f\u7406\u548c\u6b65\u9aa4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u5b89\u5353\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528Java\u548cKotlin\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u662f\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u4f46\u662f\uff0c\u6709\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528C\u6216C++\u7f16\u5199\u4e00\u4e9b\u6838\u5fc3\u7b97\u6cd5\u6216\u5e93\uff0c\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u6216\u590d\u7528\u73b0\u6709\u7684C\/C++\u4ee3\u7801\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5728\u5b89\u5353\u5e94\u7528\u7a0b\u5e8f\u4e2d<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,12236,12237,14,4134],"class_list":["post-6658","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-81","tag-app-","tag-app","tag-14","tag-4134"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/6658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=6658"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/6658\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=6658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=6658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=6658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}