自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
轉換 HAL 模組
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以轉換 hardware/libhardware/include/hardware
中的標頭,將現有的 HAL 模組更新為 HIDL HAL 模組。
使用 c2hal
c2hal
工具會處理大部分轉換作業,減少需要手動變更的次數。舉例來說,如要為 NFC HAL 產生 HIDL .hal
檔案:
make c2hal
c2hal -r android.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport -p android.hardware.nfc@1.0 hardware/libhardware/include/hardware/nfc.h
這些指令會在 hardware/interfaces/nfc/1.0/
中新增檔案。從 $ANDROID_BUILD_TOP
目錄執行 hardware/interfaces/update-makefiles.sh
時,也會將必要的 makefile 新增至 HAL。您可以在此手動變更,全面轉換 HAL。
c2hal 活動
執行 c2hal
時,標頭檔案中的所有內容都會轉移至 .hal
檔案。
c2hal
會在提供的標頭檔案中找出包含函式指標的結構體,並將每個結構體轉換為個別的介面檔案。舉例來說,alloc_device_t
會轉換為 IAllocDevice
HAL 模組 (位於檔案 IAllocDevice.hal
中)。
所有其他資料類型都會複製到 types.hal
檔案中。磅定義會移至列舉,而非 HIDL 的項目或無法轉換的項目 (例如靜態函式宣告) 會複製至註解,並標示為「NOTE
」。
手動活動
c2hal
工具在遇到特定結構時,不知道該如何處理。舉例來說,HIDL 沒有原始指標的概念,因此當 c2hal
在標頭檔案中遇到指標時,它不知道指標應解讀為陣列,還是另一個物件的參照。同樣地,空值指標也無法提供任何資訊。
在轉換至 HIDL 的過程中,必須手動移除 int reserved[7]
等欄位。傳回值的名稱等項目應更新為更有意義的名稱,例如將 NFC 中 write
等方法的傳回參數,從自動產生的 int32_t write_ret
轉換為 Status status
(Status
是包含可能 NFC 狀態的新枚舉)。
實作 HAL
建立代表 HAL 的 .hal
檔案後,您必須產生 Makefile (Make 或 Soong),以便在 C++ 和 Java 中建立語言支援 (除非 HAL 使用 Java 不支援的功能)。./hardware/interfaces/update-makefiles.sh
指令碼可自動為位於 hardware/interfaces
目錄中的 HAL 產生 makefile (如果是位於其他位置的 HAL,只需更新指令碼即可)。
更新 makefile 後,您就可以產生標頭檔案並實作方法。如要進一步瞭解如何實作產生的介面,請參閱 HIDL C++ (適用於 C++ 實作) 或 HIDL Java (適用於 Java 實作)。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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-27 (世界標準時間)。"],[],[],null,["# Convert HAL modules\n\nYou can update preexisting HAL modules to HIDL HAL modules by converting the\nheader in `hardware/libhardware/include/hardware`.\n\nUse c2hal\n---------\n\nThe\n[c2hal](https://android.googlesource.com/platform/system/tools/hidl/+/android16-release/c2hal/)\ntool handles most of the conversion work, reducing the number of required manual\nchanges. For example, to generate a HIDL `.hal` file for the NFC\nHAL: \n\n make c2hal\n c2hal -r android.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport -p android.hardware.nfc@1.0 hardware/libhardware/include/hardware/nfc.h\n\nThese commands add files in `hardware/interfaces/nfc/1.0/`. Running\n`hardware/interfaces/update-makefiles.sh` from the `$ANDROID_BUILD_TOP`\ndirectory also adds the required makefile to the HAL. From here, you can\nmake manual changes to fully convert the HAL.\n\nc2hal activities\n----------------\n\nWhen you run `c2hal`, everything in the header file is transferred\nto `.hal` files.\n\n`c2hal` identifies structs that contain function pointers in the\nprovided header file and converts each struct into a separate interface file.\nFor example, `alloc_device_t` is converted to the\n`IAllocDevice` HAL module (in the file\n`IAllocDevice.hal`).\n\nAll other data types are copied over into a `types.hal` file.\nPound-defines are moved into enums, and items not a part of HIDL or not\nconvertible (such as static-function declarations) are copied into comments\nmarked with the text \"`NOTE`\".\n\nManual activities\n-----------------\n\nThe `c2hal` tool doesn't know what to do when it encounters\ncertain constructs. For example, HIDL has no concept of raw pointers; because of\nthis, when `c2hal` encounters a pointer in header files, it doesn't\nknow whether the pointer should be interpreted as an array or as a reference to\nanother object. Void pointers are also similarly opaque.\n\nField such as `int reserved[7]` must be manually removed during\nthe transition to HIDL. Items such as the name of the return value should be\nupdated to something more meaningful; for example, converting the return\nparameter of methods such as `write` in NFC from the autogenerated\n`int32_t write_ret` to `Status status` (where\n`Status` is a new enum containing possible NFC statuses).\n\nImplement the HAL\n-----------------\n\nAfter you have created `.hal` files to represent your HAL, you\nmust generate the makefiles (Make or Soong) that create the language support in\nC++ and Java (unless the HAL uses a feature unsupported in Java). The\n`./hardware/interfaces/update-makefiles.sh` script can automatically\ngenerate makefiles for HALs located in the `hardware/interfaces`\ndirectory (for HALs in other locations, simply update the script).\n\nWhen the makefiles are up to date, you are ready to generate header files and\nimplement methods. For details on implementing the generated interface, see\n[HIDL C++](/docs/core/architecture/hidl-cpp) (for C++\nimplementations) or [HIDL\nJava](/docs/core/architecture/hidl-java) (for Java implementations)."]]