{"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
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<\/figure>\n<\/p>\n