自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
AIDL 總覽
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 介面定義語言 (AIDL) 是一種工具,可讓使用者抽象化 IPC。指定介面 (位於 .aidl
檔案中) 後,各種建構系統會使用 aidl
二進位檔建構 C++ 或 Java 繫結,以便在程序間使用這個介面,不受執行階段或位元數影響。
AIDL 可用於 Android 中的任何程序,包括平台元件之間或應用程式之間。但絕不會做為應用程式的 API 使用。舉例來說,AIDL 可用於在平台中實作 SDK API,但 SDK API 介面絕不會直接包含 AIDL API。如需有關如何直接在應用程式之間使用 AIDL 的說明文件,請參閱相應的 Android 開發人員說明文件。如果 AIDL 用於可獨立更新的平台元件之間,例如 APEX (Android 10 以上版本) 或 HAL (Android 11 以上版本),則必須使用稱為「穩定 AIDL」的版本控管系統。
範例
以下是 AIDL 介面範例:
package my.package;
import my.package.Baz; // defined elsewhere
interface IFoo {
void doFoo(Baz baz);
}
伺服器程序會註冊介面並處理對該介面的呼叫,而用戶端程序則會呼叫這些介面。在許多情況下,程序會同時做為用戶端和伺服器,因為程序可能會參照多個介面。如要進一步瞭解 AIDL 語言,請參閱「AIDL 語言」。如要進一步瞭解可使用這些介面的各種執行階段,請參閱「AIDL 後端」。這些型別宣告與特定語言中的類別宣告完全相同,但可跨程序運作。
運作方式
AIDL 會使用繫結器核心驅動程式進行呼叫。撥打電話時,方法 ID 和所有物件都會封裝到緩衝區,並複製到遠端程序,繫結器執行緒會在該處等待讀取資料。繫結器執行緒收到交易資料後,會在本機程序中查閱原生存根物件,而這個類別會解壓縮資料,並在本機介面物件上發出呼叫。這個本機介面物件是伺服器程序建立及註冊的物件。在相同程序和相同後端中進行呼叫時,不會有任何 Proxy 物件,因此呼叫是直接進行,不會有任何封裝或解除封裝作業。詳情請參閱「Binder 總覽」。
與裝置上的服務互動
Android 內建幾項指令,可與裝置上的服務互動。請嘗試:
adb shell dumpsys --help # listing and dumping services
adb shell service --help # sending commands to services for testing
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-30 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-30 (世界標準時間)。"],[],[],null,["# AIDL overview\n\nThe Android Interface Definition Language (AIDL) is a tool that lets users\nabstract away IPC. Given an interface (specified in a `.aidl`\nfile), various build systems use the `aidl` binary to construct C++ or Java\nbindings so that this interface can be used across processes, regardless of the\nruntime or bitness there.\n\nAIDL can be used between any process in Android: between platform components\nor between apps. However, it is never used as an API for apps. AIDL may be used\nto implement an SDK API in the platform, for example, but the SDK API surface\nnever contains AIDL APIs directly. For documentation about how to use AIDL\nbetween apps directly, see corresponding\n[Android developers\ndocumentation](https://developer.android.com/guide/components/aidl).\nWhen AIDL is used between platform components that are updated separately, such\nas APEXes (starting in Android 10) or HALs (starting in\nAndroid 11), the versioning system known as\n[Stable AIDL](/docs/core/architecture/aidl/stable-aidl) must be used.\n\nExample\n-------\n\nHere is an example AIDL interface: \n\n package my.package;\n\n import my.package.Baz; // defined elsewhere\n\n interface IFoo {\n void doFoo(Baz baz);\n }\n\nA *server* process registers an interface and serves calls to it, and a *client*\nprocess makes calls to those interfaces. In many cases, a process acts as both a\nclient and a server since it may be referencing multiple interfaces. For more\ndetails about the AIDL language, see\n[AIDL language](/docs/core/architecture/aidl/aidl-language). For more details\nabout the various runtimes available to use these interfaces, see\n[AIDL backends](/docs/core/architecture/aidl/aidl-backends). These type\ndeclarations are exactly like a class declaration in a given language, but they\nwork across processes.\n\nHow it works\n------------\n\nAIDL uses the binder kernel driver to make calls. When you make a call, a\nmethod identifier and all of the objects are packed onto a buffer and copied to\na remote process where a binder thread waits to read the data. Once a binder\nthread receives data for a transaction, the thread looks up a native stub object\nin the local process, and this class unpacks the data and makes a call on a\nlocal interface object. This local interface object is the one a server process\ncreates and registers. When calls are made in the same process and the same\nbackend, no proxy objects exist, and so calls are direct without any\npacking or unpacking. For further information, see the\n[Binder overview](/docs/core/architecture/ipc/binder-overview).\n\nInteract with services on the device\n------------------------------------\n\nAndroid comes with a few commands to allow interacting with services on the\ndevice. Try: \n\n adb shell dumpsys --help # listing and dumping services\n adb shell service --help # sending commands to services for testing"]]