{"id":4861,"date":"2023-07-13T23:11:31","date_gmt":"2023-07-13T15:11:31","guid":{"rendered":"https:\/\/blog.coderfan.org\/?p=4861"},"modified":"2023-10-30T11:45:22","modified_gmt":"2023-10-30T03:45:22","slug":"android-aidl-usage","status":"publish","type":"post","link":"https:\/\/blog.coderfan.org\/en\/android-aidl-usage.html","title":{"rendered":"Android AIDL\u4f7f\u7528\u6982\u8ff0"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>4 Minute, 47 Second                <\/div>\n\n            <\/div>\n<h2 class=\"wp-block-heading\">\u80cc\u666f\u6982\u8ff0<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">AIDL:Android Interface Definition Language\uff0c\u4e0e\u5176\u4ed6IDL\u7c7b\u8bed\u8a00\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u5ba2\u6237\u7aef\u3001\u670d\u52a1\u7aef\u5747\u8ba4\u53ef\u7684\u8fdc\u7a0b\u8c03\u7528\u63a5\u53e3(RPC)\u3002AIDL\u57fa\u4e8ebinder\u5b9e\u73b0\u8de8\u8fdb\u7a0b\u7684\u63a5\u53e3\u8c03\u7528\u3002\u5728\u9002\u7528\u573a\u666f\u4e0a\u662f\u5904\u7406\u591a\u7ebf\u7a0b\u3001\u591a\u5ba2\u6237\u7aef\u5e76\u53d1\u8bbf\u95ee\u670d\u52a1\u7aef\u7684\u60c5\u51b5\u3002\u901a\u8fc7AIDL\u89c4\u8303\uff0c\u53ef\u4ee5\u751f\u6210\u8de8\u8bed\u8a00\u7684\u3001\u89c4\u8303\u9002\u7528\u7684\u4e2d\u95f4\u4ee3\u7801\uff0c\u4e3aIPC\u901a\u4fe1\u63d0\u4f9b\u4fbf\u5229\u3002\u5b89\u5353\u4e2d\u9664\u4e86AIDL\uff0c\u8fd8\u6709\u4e00\u5957HIDL\u901a\u4fe1\u6846\u67b6\uff0c\u4e0d\u8fc7\u5176\u4e3b\u8981\u7528\u4e8eNative Service\u4e0eHAL\u5c42\u4e4b\u95f4\u7684\u63a5\u53e3\u901a\u4fe1\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\">\u7531\u4e8eAIDL\u901a\u4fe1\u7684\u5e95\u5c42\u4ecd\u662f\u57fa\u4e8ebinder\uff0c\u6240\u4ee5\u4ecd\u7136\u9075\u5b88C-S\u67b6\u6784\uff0c\u5176\u4e2d\u63d0\u4f9b\u63a5\u53e3\u4f9b\u5916\u90e8\u8c03\u7528\u7684\u6211\u4eec\u79f0\u4e4b\u4e3a\u670d\u52a1\u7aef\uff0c\u800c\u8c03\u7528\u65b9\u5219\u4e3a\u5ba2\u6237\u7aef\u3002\u57fa\u4e8eAIDL\u53ef\u4ee5\u751f\u6210Java\/C++\/Rust\u7684\u4ee3\u7801\u6765\u52a0\u901f\u6211\u4eec\u7684\u5f00\u53d1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bed\u6cd5\u89c4\u5219<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">AIDL\u63a5\u53e3\u5b9a\u4e49\u6587\u4ef6\u4ee5.aidl\u5f62\u5f0f\u7ed3\u5c3e,\u5141\u8bb8\u901a\u8fc7\u4e00\u4e2a\u6216\u591a\u4e2a\u65b9\u6cd5\uff08\u53ef\u63a5\u6536\u53c2\u6570\u6216\u8fd4\u56de\u503c\uff09\u6765\u58f0\u660e\u63a5\u53e3\uff0c\u53c2\u6570\u548c\u8fd4\u56de\u503c\u53ef\u4ee5\u4e3a\u4efb\u610f\u7c7b\u578b\uff0c\u751a\u81f3\u662fAIDL\u5b9a\u4e49\u7684\u5176\u4ed6\u63a5\u53e3\u3002AIDL\u7684\u63a5\u53e3\u5b9a\u4e49\u5927\u81f4\u4e0a\u57fa\u4e8eJava\u8bed\u8a00\uff0c\u6bcf\u4e2a.aidl\u6587\u4ef6\u5747\u9700\u5b9a\u4e49\u5355\u4e2a\u63a5\u53e3\uff0c\u5e76\u4e14\u53ea\u9700\u8981\u63a5\u53e3\u58f0\u660e\u548c\u65b9\u6cd5\u7b7e\u540d\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u5305\u540d<\/strong>:\u4e0eJava\u8bed\u8a00\u7c7b\u4f3c,aidl\u4e2d\u4e5f\u9700\u8981\u5b9a\u4e49\u5305\u540d\uff0c\u5728\u751f\u6210\u5bf9\u5e94\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f1a\u8f6c\u6362\u4e3a\u547d\u540d\u7a7a\u95f4\uff1b\u5728\u5b9a\u4e49\u5305\u540d\u65f6\u9700\u8981\u4f7f\u7528package\u5173\u952e\u5b57\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u5176\u4ed6\u5305\uff0c\u5219\u9700\u8981\u4f7f\u7528import\u5173\u952e\u5b57\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1aAIDL\u4e2d\u652f\u6301Java\u6240\u6709\u7684\u539f\u751f\u6570\u636e\u7c7b\u578b\uff0c\u5982byte\u3001short\u3001int\u3001long\u3001boolean\u7b49\uff0c\u4e5f\u652f\u6301String\u3001List\u3001CharSequence\u3001Map\u7b49\u590d\u5408\u7c7b\u578b\u3002\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u7684AIDL\u63a5\u53e3\u662f\u8de8\u8bed\u8a00\u7684\uff0c\u6b64\u5904\u8fd8\u9700\u8981\u989d\u5916\u6ce8\u610f\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u9f50\uff0c\u5177\u4f53\u53ef\u53c2\u8003\u4e0b\u8868\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td><strong>Java\/AIDL <\/strong><strong>\u7c7b\u578b<\/strong><strong><\/strong><\/td><td><strong>C++ <\/strong><strong>\u7c7b\u578b<\/strong><strong><\/strong><\/td><td><strong>NDK <\/strong><strong>\u7c7b\u578b<\/strong><strong><\/strong><\/td><td><strong>Rust <\/strong><strong>\u7c7b\u578b<\/strong><strong><\/strong><\/td><\/tr><\/thead><tbody><tr><td>boolean<\/td><td>bool<\/td><td>bool<\/td><td>bool<\/td><\/tr><tr><td>byte<\/td><td>int8_t<\/td><td>int8_t<\/td><td>i8<\/td><\/tr><tr><td>char<\/td><td>char16_t<\/td><td>char16_t<\/td><td>u16<\/td><\/tr><tr><td>int<\/td><td>int32_t<\/td><td>int32_t<\/td><td>i32<\/td><\/tr><tr><td>long<\/td><td>int64_t<\/td><td>int64_t<\/td><td>i64<\/td><\/tr><tr><td>float<\/td><td>float<\/td><td>float<\/td><td>f32<\/td><\/tr><tr><td>double<\/td><td>double<\/td><td>double<\/td><td>f64<\/td><\/tr><tr><td>String<\/td><td>android::String16<\/td><td>std::string<\/td><td>String<\/td><\/tr><tr><td>android.os.Parcelable<\/td><td>android::Parcelable<\/td><td>\u4e0d\u9002\u7528<\/td><td>\u4e0d\u9002\u7528<\/td><\/tr><tr><td>IBinder<\/td><td>android::IBinder<\/td><td>ndk::SpAIBinder<\/td><td>binder::SpIBinder<\/td><\/tr><tr><td>T[]<\/td><td>std::vector&lt;T&gt;<\/td><td>std::vector&lt;T&gt;<\/td><td>In: &amp;[T]<br>Out: Vec&lt;T&gt;<\/td><\/tr><tr><td>byte[]<\/td><td>std::vector&lt;uint8_t&gt;<\/td><td>std::vector&lt;int8_t&gt;<sup>1<\/sup><\/td><td>In: &amp;[u8]<br>Out: Vec&lt;u8&gt;<\/td><\/tr><tr><td>List&lt;T&gt;<\/td><td>std::vector&lt;T&gt;<sup>2<\/sup><\/td><td>std::vector&lt;T&gt;<sup>3<\/sup><\/td><td>In: &amp;[T]<sup>4<\/sup><br>Out: Vec&lt;T&gt;<\/td><\/tr><tr><td>FileDescriptor<\/td><td>android::base::unique_fd<\/td><td>\u4e0d\u9002\u7528<\/td><td>binder::parcel::ParcelFileDescriptor<\/td><\/tr><tr><td>ParcelFileDescriptor<\/td><td>android::os::ParcelFileDescriptor<\/td><td>ndk::ScopedFileDescriptor<\/td><td>binder::parcel::ParcelFileDescriptor<\/td><\/tr><tr><td>interface \u7c7b\u578b (T)<\/td><td>android::sp&lt;T&gt;<\/td><td>std::shared_ptr&lt;T&gt;<\/td><td>binder::Strong<\/td><\/tr><tr><td>parcelable \u7c7b\u578b (T)<\/td><td>T<\/td><td>T<\/td><td>T<\/td><\/tr><tr><td>union \u7c7b\u578b (T)<sup>5<\/sup><\/td><td>T<\/td><td>T<\/td><td>T<\/td><\/tr><tr><td>T[N]&nbsp;<sup>6<\/sup><\/td><td>std::array&lt;T, N&gt;<\/td><td>std::array&lt;T, N&gt;<\/td><td>[T; N]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u6ce8\u91ca<\/strong>\uff1a\u5728AIDL\u4e2d\u5bf9\u6ce8\u91ca\u7684\u4f7f\u7528\u540c\u6837\u9075\u5faaJava\u4e2d\u4f7f\u7528\u6ce8\u91ca\u7684\u89c4\u5219,\u4f7f\u7528\/\/\u8fdb\u884c\u5355\u884c\u6ce8\u91ca\uff0c\u4f7f\u7528\/**\/\u8fdb\u884c\u5757\u6ce8\u91ca\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u89e3<\/strong>\uff1a\u5728AIDL\u4e2d\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u6ce8\u89e3\uff0cAIDL\u4e2d\u652f\u6301\u7684\u6ce8\u89e3\u53c2\u8003\u8be5<a href=\"https:\/\/source.android.com\/docs\/core\/architecture\/aidl\/aidl-annotations\" class=\"ek-link\">Link<\/a>\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u8054\u5408\u4f53\uff1a<\/strong>\u5728Andrid12\u4e4b\u540e\uff0c\u53ef\u4ee5\u5728aidl\u4e2d\u5b9a\u4e49\u8054\u5408\u4f53\uff0c\u4f7f\u7528union\u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">package android.com.example;\n\nimport android.com.foo;\nimport android.com.bar;\nunion Settings\n {\n      FooSettings settingfoo;\n      BarSettings settingbar;\n      @utf8InCpp String name;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u679a\u4e3e\u4f53\uff1a<\/strong>\u5728Android11\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u652f\u6301\u679a\u4e3e\u4f53\u5b9a\u4e49\uff0c\u4f7f\u7528enum\u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">package android.com.example;\n\nenum Boo {\n   A=1,\n   B=2,\n   C=3\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u7ed3\u6784\u4f53<\/strong>\uff1a\u7ed3\u6784\u4f53\u5b9a\u4e49\u4f7f\u7528<strong>struct\u5173\u952e\u5b57\uff0c<\/strong>\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">package android.hardware.automotive.evs@1.1;\n\n\/**\n * Structure that describes informative events occurred during EVS is streaming\n *\/\nstruct EvsEventDesc {\n    \/**\n     * Type of an informative event\n     *\/\n    EvsEventType aType;\n    \/**\n     * Device identifier\n     *\/\n    string deviceId;\n    \/**\n     * Possible additional information\n     *\/\n    uint32_t[4] payload;\n};<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u63a5\u53e3\u5b9a\u4e49<\/strong>\uff1a\u5728AIDL\u4e2d\u6211\u4eec\u4e3b\u4f53\u90e8\u5206\u662f\u5b9a\u4e49\u63a5\u53e3\uff0c\u6b64\u65f6\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57interface\uff0c\u5e76\u4e14\u786e\u4fdd\u63a5\u53e3\u540d\u4e0e\u6587\u4ef6\u540d\u4fdd\u6301\u4e00\u81f4,\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/IHidlExample.aidl\npackage android.com.example;\n\ninterface IHidlExample {\n    void Foo(int para1);\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u5e38\u91cf<\/strong>\uff1a\u5728AIDL\u63a5\u53e3\u4e2d\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49 String\u4e0eint\u5e38\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">package example.car.evs;\n\ninterface ICarEvsType {\n\tconst String EXAMPLE_CAR_EVS_INTERFACE_VERSION=\u201d0.0.1\u201d;\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u81ea\u5b9a\u4e49\u901a\u4fe1\u6570\u636e<\/strong>\uff1a\u5728AIDL\u4e2d\uff0c\u5982\u679c\u9700\u8981\u5728\u6211\u4eec\u7684\u63a5\u53e3\u4e2d\u4f20\u9012\u81ea\u5b9a\u4e49\u6570\u636e\uff0c\u6b64\u65f6\u6211\u4eec\u4f7f\u7528parcelable\u5173\u952e\u5b57\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/ExampleMessage.aidl->ExampleMessage.java\npackage android.com.example;\nparcelable ExampleMessage;\n\n\/\/ExampleMessage.aidl->ExampleMessage.h\npackage android.com.example;\nparcelable ExampleMessage cpp_header \"ExampleMessage.h\";<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728AIDL\u4e2d\u5b9a\u4e49\u63a5\u53e3\u65f6\uff0c\u6709\u4e00\u4e9b\u503c\u5f97\u6ce8\u610f\u7684\u70b9\uff1a<\/p>\n\n\n\n<p>1.\u5b9a\u4e49\u65b9\u6cd5\u53ef\u4ee5\u5e26\u96f6\u4e2a\u6216\u8005\u591a\u4e2a\u53c2\u6570\u3001\u8fd4\u56de\u503c\u6216\u7a7a\u503c<\/p>\n\n\n\n<p class=\"has-text-align-justify\">2.\u6240\u6709\u975e\u539f\u751f\u53c2\u6570\u5747\u9700\u8981\u6307\u793a\u6570\u636e\u8d70\u5411\u7684\u65b9\u5411\u6807\u8bb0\uff0c\u8fd9\u7c7b\u6807\u8bb0\u53ef\u4ee5\u662fin\u3001out\u6216\u8005inout\uff1b\u539f\u751f\u7c7b\u578b\u3001String\u3001IBinder\u4ee5\u53caAIDL\u751f\u6210\u7684\u63a5\u53e3\u9ed8\u8ba4\u4e3ain<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u540e\u7aef\u652f\u6301<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">\u57fa\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0cAIDL\u652f\u6301\u4e0d\u540c\u7684\u540e\u7aef\u8bed\u8a00\uff0c\u76ee\u524d\u5305\u62ecJava\/C++\/Rust\u8fd9\u4e09\u79cd\u8bed\u8a00\uff0c\u57fa\u4e8e\u8fd9\u4e09\u79cd\u8bed\u8a00\u7ec6\u5206\u4e3a\u56db\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td><strong>Backend<\/strong><\/td><td><strong>Language<\/strong><\/td><td><strong>API surface<\/strong><\/td><td><strong>Build systems<\/strong><\/td><\/tr><\/thead><tbody><tr><td>Java<\/td><td>Java<\/td><td>SDK\/SystemApi (stable*)<\/td><td>all<\/td><\/tr><tr><td>NDK<\/td><td>C++<\/td><td>libbinder_ndk (stable*)<\/td><td>aidl_interface<\/td><\/tr><tr><td>CPP<\/td><td>C++<\/td><td>libbinder (unstable)<\/td><td>all<\/td><\/tr><tr><td>Rust<\/td><td>Rust<\/td><td>libbinder_rs (unstable)<\/td><td>aidl_interface<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">\u4ee5IRemoteService.aidl\u4e3a\u4f8b\uff0c\u751f\u6210\u7684Java\u540e\u7aef\u6587\u4ef6\u4e3a\uff1aIRemoteService.java\uff1b\u5728AOSP\u6e90\u7801\u73af\u5883\u4e0b\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7aidl\u547d\u4ee4\u884c\u5de5\u5177\u6765\u751f\u6210\u5bf9\u5e94\u7684\u540e\u7aef\u6587\u4ef6\uff1b\u5982\u4e0b\u547d\u4ee4\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">aidl --lang java IRemoteService.aidl --out .<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5982\u679c\u6211\u4eec\u9700\u8981\u5728Android\u6e90\u7801\u73af\u5883\u4e0b\u901a\u8fc7Android.bp\u6765\u751f\u6210\u5bf9\u5e94\u7684\u6e90\u7801\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528aidl\u5b57\u6bb5\uff0c\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">java_library {\nname: \"aidlproxy\",\n\/\/\u6307\u5b9aaidl\u6240\u5728\u8def\u5f84\n    aidl: {\n        local_include_dirs: [\n\t        \"aidl\/Core\",\n\t    ],\n},\n\/\/\u52a1\u5fc5\u5728src\u4e2d\u8fdb\u884c\u56ca\u62ec\uff0c\u8fd9\u6837\u624d\u4f1a\u5bfc\u5165aidl\u6240\u751f\u6210\u7684java\u6e90\u7801\n    srcs: [\"src\/java\/**\/*.java\"] + [\"aidl\/**\/I*.aidl\"],\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728\u751f\u6210\u7684IRemoteService.java\u6587\u4ef6\u5185\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aIRemoteService\u7684interface\uff0c\u5176\u7ee7\u627fIInterface\uff08android.os.IInterface\uff09\uff0c\u540c\u65f6\u5728\u8be5\u63a5\u53e3\u5185\u90e8\uff0c\u8fd8\u5305\u542b\u4e24\u4e2a\u5185\u90e8\u7c7b\uff0c\u5206\u522b\u540d\u4e3aDefault\u4e0eStub\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">package android.com.example;\n\nimport android.os.Binder;\nimport android.os.IBinder;\nimport android.os.IInterface;\nimport android.os.Parcel;\nimport android.os.RemoteException;\n\npublic interface IRemoteService extends IInterface {\n\/\/interface methods declaration\n\n\/\/Stub\u7c7b\npublic abstract static class Stub extends Binder implements IRemoteService {\n\n    \/\/asInterface\u65b9\u6cd5,\u975e\u5e38\u91cd\u8981\uff0c\u7528\u4e8e\u83b7\u53d6\u5c06binder\u5bf9\u8c61\u8f6c\u6362\u4e3ajava\u5c42\u7684\u5bf9\u8c61\u5b9e\u4f8b\n    public static IRemoteService asInterface(IBinder obj) {\n    }\n    \/\/asBinder\u65b9\u6cd5\n    public IBinder asBinder() { return this;}\n    \/\/onTransact\u65b9\u6cd5\n    public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws \n    RemoteException {\n    }\n    \/\/ Proxy\u7c7b\n    private static class Proxy implements IRemoteService {\n    }\n }\n\/\/Default\u7c7b\npublic static class Default implements IRemoteService {\n }\n\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u540c\u6837\u662fIRemoteService.aidl,\u751f\u6210\u7684C++\u540e\u7aef\u6587\u4ef6\u5305\u62ec\uff1aIRemoteService.h\\IRemoteService.cpp\u3001BpRemoteService.h\u3001BnRemoteService.h,\u5728AOSP\u6e90\u7801\u73af\u5883\u4e0b\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528aidl-cpp\u547d\u4ee4\u6765\u624b\u52a8\u751f\u6210\u3002\u5982\u679c\u4f7f\u7528Android.bp\u5728\u6784\u5efa\u65f6\u751f\u6210\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528aidl\u914d\u7f6e\u5b57\u6bb5\uff0c\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">cc_binary {\n      \/\/\u8bbe\u5b9aaidl\u6587\u4ef6\u7684\u8def\u5f84\n      aidl: {\n        local_include_dirs: [\n\t        \"proxy\/aidl\/Core\",\n\t    ],\n    },\n     \/\/\u5728src\u4e2d\u5bfc\u5165\u5bf9\u5e94\u751f\u6210\u7684\u6e90\u7801\n    srcs: [\n       \"proxy\/aidl\/Core\/*.aidl\",\n    ]\n}<\/code><\/pre>\n\n\n\n<p>\u751f\u6210\u7684\u5404\u4e2a\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/BnRemoteservice.h\nclass BnRemoteService \uff1apublic ::android::BnInterface&lt;IRemoteService>\n{\n  ......\n  explicit BnRemoteService();\n  ::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel&amp; _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) override;\n\n}; \n\n\/\/BpRemoteService.h\nclass BpRemoteService : public ::android::BpInterface&lt;IRemoteService> {\npublic:\n  explicit BpRemoteService(const ::android::sp&lt;::android::IBinder>&amp; _aidl_impl);\n  virtual ~BpRemoteService() = default;\n  .....\n}; \n\n\/\/IRemoteService.h\nclass IRemoteService : public ::android::IInterface {\npublic:\n  DECLARE_META_INTERFACE(RemoteService)\n  .....\n};  \/\/ class IRemoteService\n\nclass IRemoteServiceDefault : public IRemoteService {\npublic:\n  ::android::IBinder* onAsBinder() override {\n    return nullptr;\n  }\n   .....\n  }\n};  \/\/ class IRemoteServiceDefault\n<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u901a\u5e38\u6211\u4eec\u5728C++\u4fa7\u5b9e\u73b0aidl\u4e2d\u6240\u5b9a\u4e49\u7684\u63a5\u53e3\u65f6\uff0c\u9700\u8981\u7ee7\u627fBnRemoteService\u7c7b\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Tips:\u5982\u679c\u6211\u4eec\u4e0d\u9700\u8981\u5c06\u81ea\u5df1\u7684\u6e90\u7801\u4e0eaidl\u5b9a\u4e49\u6587\u4ef6\u4e00\u8d77\u8fdb\u884c\u7f16\u8bd1\uff0c\u800c\u8fd9\u4e9baidl\u5b9a\u4e49\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u7ec4\u4ef6\u5bf9\u5916\u63d0\u4f9b\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Android.bp\u4e2d\u4f7f\u7528aidl_interface\u5b57\u6bb5\u6765\u5bf9\u4e00\u7ec4aidl\u5b9a\u4e49\u6587\u4ef6\u8fdb\u884c\u7f16\u8bd1\u8bbe\u5b9a\uff0c\u4f7f\u5176\u7f16\u8bd1\u6210\u4e3a\u72ec\u7acb\u7684\u7ec4\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">aidl_interface {\n    name: \"android.automotive.watchdog.internal\",\n    unstable: true,\n    vendor_available: false,\n    srcs: [\n        \"android\/automotive\/watchdog\/internal\/*.aidl\",\n    ],\n    backend: {\n        java: {\n            platform_apis: true,\n            enabled: true,\n        },\n    },\n    imports: [\n        \"android.automotive.watchdog-V3\",\n    ],\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u670d\u52a1\u5b9e\u73b0<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91cc\u6211\u4eec\u8bb2\u8bb2\u670d\u52a1\u5b9e\u73b0\u7684\u8fc7\u7a0b\uff0c\u4ee5IRemoteService.aidl\u4e3a\u4f8b\uff0c\u5176\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/ IRemoteService.aidl\npackage com.example.android;\n\ninterface IRemoteService {\n    \/** Request the process ID of this service, to do evil things with it. *\/\n    int getServicePid();\n    \/**\n     * Returns a current status of service.\n     *\/\n    int getCurrentStatus();\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u540e\u7aef\u4e3aJava\u670d\u52a1\uff0c\u5176\u670d\u52a1\u5b9e\u73b0\u9700\u8981\u5b8c\u6210\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<p class=\"has-text-align-justify\">1.\u5b9e\u73b0\u670d\u52a1\u7c7b\u5e76\u7ee7\u627f\u81easervice\uff0c\u5b9e\u73b0onBind\u63a5\u53e3<\/p>\n\n\n\n<p class=\"has-text-align-justify\">2.\u5728\u670d\u52a1\u7c7b\u5185\u90e8\u5b9e\u73b0aidl\u4e2d\u6240\u5b9a\u4e49\u7684\u63a5\u53e3<\/p>\n\n\n\n<p>\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">public class RemoteService extends Service {\n    @Override\n    public void onCreate() {\n        super.onCreate();\n    }\n\n    @Override\n    public IBinder onBind(Intent intent) {\n        \/\/ Return the interface\n        return binder;\n    }\n\n    private final IRemoteService.Stub binder = new IRemoteService.Stub() {\n        public int getServicePid(){\n            return Process.myPid();\n        }\n        public int getCurrentStatus() {\n            \/\/ Does nothing\n        }\n    };\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5982\u679c\u540e\u7aef\u4e3aC++\u670d\u52a1\uff0c\u5176\u670d\u52a1\u5b9e\u73b0\u9700\u8981\u5b8c\u6210\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<p class=\"has-text-align-justify\">1.\u5b9e\u73b0\u670d\u52a1\u7c7b\u5e76\u7ee7\u627fBnRemoteService\u7c7b<\/p>\n\n\n\n<p class=\"has-text-align-justify\">2.\u5b9e\u73b0\u76f8\u5e94\u7684\u63a5\u53e3\uff0c\u5e76\u6ce8\u518c\u81f3serviceManager<\/p>\n\n\n\n<p>\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">#include &lt;xxx\/xxxxx\/BnRemoteService.h>\n\nclass RemoteService : public BnRemoteService{\n\nandroid::binder::Status getServicePid(int32_t* out) override{\n   \/\/do something\n}\n\nandroid::binder::Status getCurrentStatus(int32_t* out) override{\n   \/\/do something\n}\n}<\/code><\/pre>\n\n\n\n<p>\u5173\u4e8eC++\u670d\u52a1\u6ce8\u518c\u5230serviceManager\u7684\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u53c2\u8003\u6211\u7684\u8fd9\u7bc7<a href=\"https:\/\/blog.coderfan.org\/en\/android-native-service-registeration.html\/\" class=\"ek-link\">\u6587\u7ae0<\/a>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u670d\u52a1\u83b7\u53d6<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">AIDL\u901a\u4fe1\u7684\u672c\u8d28\u4ecd\u65e7\u662fBinder\u901a\u4fe1\uff0c\u6545\u800c\u670d\u52a1\u83b7\u53d6\u4e5f\u901a\u5e38\u901a\u8fc7ServiceManager\u8fdb\u884c\u83b7\u53d6\u3002\u4ee5\u4e0b\u5c06\u5206\u522b\u4ee5Java\u548cC++\u4e3a\u4f8b\uff0c\u4e3a\u5927\u5bb6\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u83b7\u53d6\u670d\u52a1\u3002<\/p>\n\n\n\n<p>Java\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">import android.os.ServiceManager;\n\/\/ registering\nServiceManager.addService(\"service-name\", myService);\n\/\/ getService for existing service\nmyService = IFoo.Stub.asInterface(ServiceManager.getService(\"service-name\"));\n\/\/ return if service is started now\nmyService = IFoo.Stub.asInterface(ServiceManager.checkService(\"service-name\"));\n\/\/ waiting until service comes up (new in Android 11)\nmyService = IFoo.Stub.asInterface(ServiceManager.waitForService(\"service-name\"));\n\/\/ waiting for declared (VINTF) service to come up (new in Android 11)\nmyService = IFoo.Stub.asInterface(ServiceManager.waitForDeclaredService(\"service-name\"));<\/code><\/pre>\n\n\n\n<p>C++\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">#include &lt;binder\/IServiceManager.h>\n\/\/ registering\ndefaultServiceManager()->addService(String16(\"service-name\"), myService);\n\/\/ return if service is started now\nstatus_t err = checkService&lt;IFoo>(String16(\"service-name\"), &amp;myService);\n\/\/ waiting until service comes up (new in Android 11)\nmyService = waitForService&lt;IFoo>(String16(\"service-name\"));\n\/\/ waiting for declared (VINTF) service to come up (new in Android 11)\nmyService = waitForDeclaredService&lt;IFoo>(String16(\"service-name\"));<\/code><\/pre>\n\n\n\n<p>\u66f4\u591a\u5185\u5bb9\uff0c\u540e\u7eed\u8fdb\u884c\u5b8c\u5584~<\/p>\n        <div class=\"booster-block booster-reactions-block\">\n            <div class=\"twp-reactions-icons\">\n                \n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-1\" post-id=\"4861\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/happy.svg\" alt=\"Happy\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Happy                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">1<\/span>\n                        \n                                                <span class=\"twp-react-percent\"><span>100<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-2\" post-id=\"4861\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/sad.svg\" alt=\"Sad\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Sad                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-3\" post-id=\"4861\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/excited.svg\" alt=\"Excited\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Excited                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-6\" post-id=\"4861\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/sleepy.svg\" alt=\"Sleepy\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Sleepy                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                        \n                                                <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-4\" post-id=\"4861\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/angry.svg\" alt=\"Angry\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">Angry<\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                        \n                    <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-5\" post-id=\"4861\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/surprise.svg\" alt=\"Surprise\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">Surprise<\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n            <\/div>\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>Android\u4e2dAIDL\u7684\u4e00\u4e9b\u57fa\u7840\u4f7f\u7528<\/p>","protected":false},"author":1,"featured_media":4483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":2,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18],"tags":[71,46,72],"class_list":["post-4861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-operating-system","tag-aidl","tag-android","tag-binder"],"blocksy_meta":[],"aioseo_notices":[],"featured_image_urls":{"full":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"thumbnail":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-150x150.jpg",150,150,true],"medium":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-300x158.jpg",300,158,true],"medium_large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"1536x1536":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"2048x2048":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"trp-custom-language-flag":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-18x9.jpg",18,9,true]},"post_excerpt_stackable":"<p>Android\u4e2dAIDL\u7684\u4e00\u4e9b\u57fa\u7840\u4f7f\u7528<\/p>\n","category_list":"<a href=\"https:\/\/blog.coderfan.org\/en\/category\/operating-system\" rel=\"category tag\">\u64cd\u4f5c\u7cfb\u7edf<\/a>","author_info":{"name":"FranzKafka95","url":"https:\/\/blog.coderfan.org\/en\/author\/yushenglonely95"},"comments_num":"0 comments","featured_image_urls_v2":{"full":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"thumbnail":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-150x150.jpg",150,150,true],"medium":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-300x158.jpg",300,158,true],"medium_large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"1536x1536":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"2048x2048":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"trp-custom-language-flag":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-18x9.jpg",18,9,true]},"post_excerpt_stackable_v2":"<p>Android\u4e2dAIDL\u7684\u4e00\u4e9b\u57fa\u7840\u4f7f\u7528<\/p>\n","category_list_v2":"<a href=\"https:\/\/blog.coderfan.org\/en\/category\/operating-system\" rel=\"category tag\">\u64cd\u4f5c\u7cfb\u7edf<\/a>","author_info_v2":{"name":"FranzKafka95","url":"https:\/\/blog.coderfan.org\/en\/author\/yushenglonely95"},"comments_num_v2":"0 comments","_links":{"self":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts\/4861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/comments?post=4861"}],"version-history":[{"count":34,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts\/4861\/revisions"}],"predecessor-version":[{"id":5022,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts\/4861\/revisions\/5022"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/media\/4483"}],"wp:attachment":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/media?parent=4861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/categories?post=4861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/tags?post=4861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}