{"id":7013,"date":"2026-02-27T10:22:27","date_gmt":"2026-02-27T02:22:27","guid":{"rendered":"https:\/\/t.n-years.com\/?p=7013"},"modified":"2026-02-27T10:23:46","modified_gmt":"2026-02-27T02:23:46","slug":"%e8%b7%a8%e8%af%ad%e8%a8%80%e6%a1%a5%e6%8e%a5%ef%bc%88bridge%ef%bc%89%e6%9e%b6%e6%9e%84","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=7013","title":{"rendered":"\u8de8\u8bed\u8a00\u6865\u63a5\uff08Bridge\uff09\u67b6\u6784"},"content":{"rendered":"<blockquote>\n<p>Gemini \u751f\u6210\uff0c\u95ee\u9898\uff1a\u5982\u679c native \u7684\u7f51\u7edc\u5e93\u652f\u6301\u88ab \u5404\u7c7b\u8bed\u8a00\u76f4\u63a5\u8c03\u7528\uff0c\u6bd4\u5982 js\u3001oc\u3001swift\u3001java\u3001kotlin\uff0c\u9700\u8981\u600e\u6837\u8bbe\u8ba1\uff0c\u4fdd\u969c\u9ad8\u6548\u5f00\u53d1<\/p>\n<\/blockquote>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684<strong>\u201c\u8de8\u8bed\u8a00\u6865\u63a5\uff08Bridge\uff09\u201d\u67b6\u6784\u95ee\u9898<\/strong>\u3002\u5728\u62e5\u6709 15 \u5e74\u7ecf\u9a8c\u7684\u67b6\u6784\u5e08\u773c\u4e2d\uff0c\u8fd9\u4e0d\u4ec5\u4ec5\u662f\u5199\u51e0\u4e2a API \u7684\u95ee\u9898\uff0c\u800c\u662f<strong>\u5185\u5b58\u751f\u547d\u5468\u671f\u7ba1\u7406\uff08Memory Management\uff09<\/strong>\u3001<strong>\u6570\u636e\u5e8f\u5217\u5316\u4ee3\u4ef7\uff08Marshaling Overhead\uff09<\/strong>\u4ee5\u53ca<strong>\u7ebf\u7a0b\u6a21\u578b\u517c\u5bb9\u6027\uff08Threading Model\uff09<\/strong>\u7684\u4e09\u91cd\u6311\u6218\u3002<\/p>\n<p>\u5728\u7ed9\u51fa\u5177\u4f53\u7684\u67b6\u6784\u5efa\u8bae\u524d\uff0c\u4e3a\u4e86\u786e\u4fdd\u6211\u63a8\u8350\u7684\u5de5\u5177\u94fe\uff08\u5982\u4f7f\u7528 JNI \u8fd8\u662f FFM\uff0cFFI \u8fd8\u662f JSI\uff09\u6700\u9002\u5408\u4f60\u7684\u9879\u76ee\uff0c\u6211\u9700\u8981\u786e\u8ba4\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n<ol>\n<li><strong>\u4f60\u7684\u5e94\u7528\u573a\u666f\uff1a<\/strong> \u662f\u9ad8\u6027\u80fd\u97f3\u89c6\u9891\u63a8\u6d41\uff0c\u8fd8\u662f\u901a\u7528\u7684 HTTP \u4e1a\u52a1\u63a5\u53e3\uff1f<\/li>\n<li><strong>\u5f53\u524d\u6280\u672f\u6808\uff1a<\/strong> \u4f60\u7684\u5bbf\u4e3b\u73af\u5883\u4e3b\u8981\u662f React Native\u3001Flutter\uff0c\u8fd8\u662f\u539f\u751f\uff08Android+iOS\uff09\uff1f<\/li>\n<li><strong>\u6570\u636e\u590d\u6742\u5ea6\uff1a<\/strong> \u6570\u636e\u4f20\u8f93\u91cf\u7ea7\u5982\u4f55\uff1f\u662f\u5426\u5305\u542b\u5927\u91cf\u7684\u5c0f\u5bf9\u8c61\u9891\u7e41\u4ea4\u6362\uff1f<\/li>\n<\/ol>\n<hr \/>\n<p>\u4e3a\u4e86\u8ba9\u4f60\u6709\u4e00\u4e2a\u5168\u5c40\u89c6\u89d2\uff0c\u6211\u4e3a\u4f60\u68b3\u7406\u4e86\u4e00\u5957\u5de5\u4e1a\u754c\u6807\u51c6\u7684\u201c\u7edf\u4e00\u6865\u63a5\u67b6\u6784\u201d\u3002<\/p>\n<h3>1. \u67b6\u6784\u6838\u5fc3\u7b56\u7565\uff1a\u57fa\u4e8e C-ABI \u7684\u5185\u6838\u9a71\u52a8<\/h3>\n<p>\u8981\u4fdd\u969c\u9ad8\u6548\u4e14\u53ef\u7ef4\u62a4\uff0c<strong>\u7edd\u4e0d\u8981<\/strong>\u4e3a\u6bcf\u79cd\u8bed\u8a00\u5355\u72ec\u5199\u4e00\u5957\u5c01\u88c5\u3002\u6838\u5fc3\u539f\u5219\u662f\uff1a<strong>Core in C\/C++ -&gt; C-ABI Layer -&gt; Language Adapters<\/strong>\u3002<\/p>\n<p>\u901a\u8fc7\u5c06\u6838\u5fc3\u903b\u8f91\u7528 C++ \u5b9e\u73b0\u5e76\u5bfc\u51fa\u7eaf C \u63a5\u53e3\uff08C-ABI\uff09\uff0c\u4f60\u53ef\u4ee5\u8ba9\u6240\u6709\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\uff08Obj-C, Swift, Java, Kotlin, JS\uff09\u901a\u8fc7\u5404\u81ea\u7684 FFI\uff08Foreign Function Interface\uff09\u673a\u5236\u76f4\u63a5\u8c03\u7528\uff0c\u65e0\u9700\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n<h3>2. \u6280\u672f\u65b9\u6848\u9009\u578b\u5bf9\u6bd4<\/h3>\n<p>\u4e0d\u540c\u8bed\u8a00\u5bf9\u5e95\u5c42\u7684\u8c03\u7528\u65b9\u5f0f\u4e0d\u540c\uff0c\u4ee5\u4e0b\u662f\u5404\u8bed\u8a00\u5bf9\u63a5\u6027\u80fd\u4e0e\u5f00\u53d1\u6548\u7387\u7684\u5bf9\u6bd4\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6865\u63a5\u6280\u672f<\/th>\n<th>\u8bed\u8a00\u9002\u7528\u8303\u56f4<\/th>\n<th>\u5f00\u53d1\u6210\u672c<\/th>\n<th>\u6027\u80fd\u5f00\u9500<\/th>\n<th>\u5173\u952e\u75db\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>C-ABI (FFI)<\/strong><\/td>\n<td>C, Swift, Kotlin, Go<\/td>\n<td>\u4e2d<\/td>\n<td>\u6781\u4f4e<\/td>\n<td>\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u751f\u547d\u5468\u671f<\/td>\n<\/tr>\n<tr>\n<td><strong>JNI (Java)<\/strong><\/td>\n<td>Java, Kotlin<\/td>\n<td>\u9ad8<\/td>\n<td>\u4e2d<\/td>\n<td>\u4ee3\u7801\u5197\u957f\uff0c\u5bb9\u6613\u5f15\u8d77\u5185\u5b58\u6cc4\u6f0f<\/td>\n<\/tr>\n<tr>\n<td><strong>JSI (RN)<\/strong><\/td>\n<td>JavaScript<\/td>\n<td>\u4e2d<\/td>\n<td>\u6781\u4f4e<\/td>\n<td>\u4ec5\u9650 React Native \u67b6\u6784<\/td>\n<\/tr>\n<tr>\n<td><strong>Objective-C++<\/strong><\/td>\n<td>iOS (Obj-C\/Swift)<\/td>\n<td>\u4f4e<\/td>\n<td>\u96f6<\/td>\n<td>\u4ec5\u9650 Apple \u751f\u6001<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3. \u67b6\u6784\u843d\u5730\u5173\u952e\u539f\u5219<\/h3>\n<h4>A. \u53e5\u67c4\u5f0f\u8bbe\u8ba1 (Opaque Handle Pattern)<\/h4>\n<p>\u4e0d\u8981\u76f4\u63a5\u5728\u8de8\u8bed\u8a00\u8fb9\u754c\u4f20\u9012\u590d\u6742\u7684\u5bf9\u8c61\u6307\u9488\u3002\u5e94\u4f7f\u7528\u201c\u53e5\u67c4\uff08Handle\/ID\uff09\u201d\u6765\u7ba1\u7406\u3002<\/p>\n<ul>\n<li><strong>\u8bbe\u8ba1\u539f\u5219\uff1a<\/strong> \u6838\u5fc3\u5c42\u6301\u6709\u5bf9\u8c61\uff0c\u5411\u5916\u66b4\u9732 <code>uintptr_t<\/code> (Handle)\u3002<\/li>\n<li><strong>\u597d\u5904\uff1a<\/strong> \u907f\u514d\u4e86\u591a\u8bed\u8a00\u5185\u5b58\u7ba1\u7406\u673a\u5236\uff08ARC vs GC\uff09\u51b2\u7a81\u5bfc\u81f4\u7684\u5d29\u6e83\u3002<\/li>\n<\/ul>\n<h4>B. \u5e8f\u5217\u5316\u65b9\u6848 (Protobuf \/ FlatBuffers)<\/h4>\n<p>\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u4f20\u8f93\uff0c\u4e0d\u8981\u4f7f\u7528 JSON\u3002JSON \u89e3\u6790\u5728\u8de8\u8bed\u8a00\u65f6\u975e\u5e38\u6602\u8d35\u3002<\/p>\n<ul>\n<li><strong>\u63a8\u8350\uff1a<\/strong> \u4f7f\u7528 <strong>FlatBuffers<\/strong>\u3002\u5b83\u5141\u8bb8\u5728\u4e0d\u53cd\u5e8f\u5217\u5316\u7684\u524d\u63d0\u4e0b\u76f4\u63a5\u8bbf\u95ee\u6570\u636e\uff0c\u6027\u80fd\u635f\u8017\u51e0\u4e4e\u4e3a\u96f6\u3002<\/li>\n<\/ul>\n<h4>C. \u4ee3\u7801\u793a\u4f8b\uff1a\u5982\u4f55\u6784\u5efa\u6e05\u6670\u7684 C-ABI \u5c42<\/h4>\n<p>\u8fd9\u91cc\u662f\u4e00\u4e2a\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u7684 C \u63a5\u53e3\u5b9a\u4e49\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u66b4\u9732\u7ed9\u6240\u6709\u5bbf\u4e3b\u8bed\u8a00\uff1a<\/p>\n<pre><code class=\"language-cpp\">\/\/ network_api.h\n#ifdef __cplusplus\nextern &quot;C&quot; {\n#endif\n\n\/\/ \u4f7f\u7528 Handle \u4f20\u9012\u5bf9\u8c61\uff0c\u907f\u514d\u76f4\u63a5\u66b4\u6f0f C++ \u7c7b\ntypedef void* NetClientHandle;\n\n\/\/ \u521d\u59cb\u5316\uff08\u521b\u5efa\u5bf9\u8c61\uff09\nNetClientHandle CreateClient(const char* config);\n\n\/\/ \u6267\u884c\u8bf7\u6c42\uff08\u4f7f\u7528\u56de\u8c03\u5904\u7406\u5f02\u6b65\u7ed3\u679c\uff09\n\/\/ \u9075\u5faa SOLID \u539f\u5219\uff0c\u5c06\u884c\u4e3a\u4e0e\u5b9e\u73b0\u89e3\u8026\nvoid SendRequest(NetClientHandle handle, const char* url, void (*callback)(int status, const char* response));\n\n\/\/ \u8d44\u6e90\u91ca\u653e\nvoid DestroyClient(NetClientHandle handle);\n\n#ifdef __cplusplus\n}\n#endif\n<\/code><\/pre>\n<h3>4. \u987e\u95ee\u7684\u6279\u5224\u6027\u601d\u7ef4 (\u5751\u4e0e\u8b66\u793a)<\/h3>\n<p>\u5728\u5b9e\u9645\u843d\u5730\u4e2d\uff0c\u65b0\u624b\u6700\u5bb9\u6613\u72af\u7684\u4e09\u4e2a\u9519\u8bef\uff1a<\/p>\n<ol>\n<li><strong>\u9891\u7e41\u8de8\u754c\u8c03\u7528\uff08The Chatty Interface\uff09\uff1a<\/strong> \u5982\u679c\u4f60\u8bbe\u8ba1\u7684\u63a5\u53e3\u50cf <code>GetProperty(&quot;a&quot;)<\/code>, <code>GetProperty(&quot;b&quot;)<\/code> \u8fd9\u6837\u9891\u7e41\u8c03\u7528\uff0cFFI \u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\uff08Context Switch\uff09\u4f1a\u77ac\u95f4\u62d6\u57ae\u6027\u80fd\u3002<strong>\u6539\u8fdb\uff1a<\/strong> \u8bbe\u8ba1\u201c\u6279\u91cf\u540c\u6b65\u201d\u63a5\u53e3\uff0c\u4e00\u6b21\u8c03\u7528\u5904\u7406\u4e00\u6279\u6570\u636e\u3002<\/li>\n<li><strong>\u5ffd\u7565\u751f\u547d\u5468\u671f\uff08Memory Ownership\uff09\uff1a<\/strong> \u5728 Java \u4e2d\u521b\u5efa\u4e86\u5bf9\u8c61\uff0c\u4f20\u9012\u7ed9 C++ \u540e\uff0cJava \u7684 GC \u53ef\u80fd\u5728 C++ \u8fd8\u5728\u4f7f\u7528\u8be5\u5bf9\u8c61\u65f6\u5c06\u5176\u56de\u6536\u3002<strong>\u6539\u8fdb\uff1a<\/strong> \u5fc5\u987b\u8bbe\u8ba1\u663e\u5f0f\u7684 <code>Release<\/code> \u673a\u5236\uff0c\u6216\u8005\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\uff08Ref Counting\uff09\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\u5207\u6362\u5b89\u5168\uff1a<\/strong> C++ \u7684\u7f51\u7edc\u56de\u8c03\u901a\u5e38\u5728\u5b50\u7ebf\u7a0b\uff0c\u800c UI \u6846\u67b6\uff08\u5982 Swift\/UI \u6216 Android View\uff09\u8981\u6c42\u5728\u4e3b\u7ebf\u7a0b\u66f4\u65b0\u3002<strong>\u6539\u8fdb\uff1a<\/strong> \u5728\u6865\u63a5\u5c42\uff08Adapter\uff09\u5185\u90e8\u7ef4\u62a4\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u5ea6\u961f\u5217\uff0c\u786e\u4fdd\u56de\u8c03\u88ab\u6d3e\u53d1\u5230\u5bbf\u4e3b\u8bed\u8a00\u7684\u6b63\u786e\u7ebf\u7a0b\u3002<\/li>\n<\/ol>\n<hr \/>\n<p><strong>\u4e0b\u4e00\u6b65\u5efa\u8bae\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u6b63\u5904\u4e8e\u9009\u578b\u6216\u91cd\u6784\u9636\u6bb5\uff0c\u4e3a\u4e86\u7ed9\u51fa\u6700\u5177\u4f53\u7684\u5efa\u8bae\uff0c<strong>\u4f60\u53ef\u4ee5\u544a\u8bc9\u6211\u4f60\u76ee\u524d\u5bbf\u4e3b\u73af\u5883\u7684\u4e3b\u6d41\u8bed\u8a00\uff08\u6bd4\u5982\u4e3b\u8981\u4f9d\u8d56 Flutter \u8fd8\u662f\u539f\u751f\u5f00\u53d1\uff09\u5417\uff1f<\/strong> \u6211\u53ef\u4ee5\u4e3a\u4f60\u5199\u4e00\u4efd\u66f4\u5177\u4f53\u7684 <code>JNI<\/code> \u6216 <code>FFI<\/code> \u63a5\u53e3\u5b9e\u73b0\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gemini \u751f\u6210\uff0c\u95ee\u9898\uff1a\u5982\u679c native \u7684\u7f51\u7edc\u5e93\u652f\u6301\u88ab \u5404\u7c7b\u8bed\u8a00\u76f4\u63a5\u8c03\u7528\uff0c\u6bd4\u5982 js\u3001oc\u3001swift\u3001&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=7013\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-7013","post","type-post","status-publish","format-standard","hentry","category-135"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7013"}],"version-history":[{"count":2,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7013\/revisions"}],"predecessor-version":[{"id":7015,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7013\/revisions\/7015"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}