自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
啟用 VNDK
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
供應商原生開發套件 (VNDK) 需要對程式碼庫進行幾項變更,才能將供應商和系統之間的疑慮區隔開來。請參閱以下指南,在供應商/原始設備製造商程式碼集中啟用 VNDK。
建構系統程式庫
建構系統包含多種物件類型,包括程式庫 (共用、靜態或標頭) 和二進位檔。
圖 1. 建構系統程式庫。
core
程式庫會由系統映像檔在系統映像檔上使用。這些程式庫無法由 vendor
、vendor_available
、vndk
或 vndk-sp
程式庫使用。cc_library {
name: "libThatIsCore",
...
}
- 供應商映像檔會在供應商映像檔上使用
vendor-only
(或 proprietary
) 程式庫。cc_library {
name: "libThatIsVendorOnly",
proprietary: true,
# or: vendor: true, # (for things in AOSP)
...
}
vendor_available
程式庫會由供應商映像檔在供應商映像檔上使用 (可能包含 core
的複本)。cc_library {
name: "libThatIsVendorAvailable",
vendor_available: true,
...
}
vndk
程式庫會在系統映像檔上由供應商映像檔使用。cc_library {
name: "libThatIsVndk",
vendor_available: true,
vndk: {
enabled: true,
}
...
}
vndk-sp
程式庫會由供應商映像檔使用,也會間接由系統映像檔使用。cc_library {
name: "libThatIsVndkSp",
vendor_available: true,
vndk: {
enabled: true,
support_system_process: true,
}
...
}
llndk
程式庫會由系統和供應商映像檔使用。cc_library {
name: "libThatIsLlndk",
llndk: {
symbol_file: "libthatisllndk.map.txt"
}
...
}
當程式庫標示為 vendor_available:true
時,會進行兩次建構:
- 平台一次 (因此安裝到
/system/lib
)
- 供應商一次 (因此安裝至
/vendor/lib
或 VNDK APEX)
供應商版本的程式庫是使用 -D__ANDROID_VNDK__
建構。這個旗標會停用私人系統元件,這些元件可能會在日後的 Android 版本中大幅變更。此外,不同程式庫會匯出不同的標頭組合 (例如 liblog
)。您可以在以下位置的 Android.bp
檔案中指定目標供應商變化版本專屬的選項:
target: { vendor: { … } }
為程式碼集啟用 VNDK
如要為程式碼集啟用 VNDK,請按照下列步驟操作:
- 計算
vendor.img
和 system.img
分區的必要大小,判斷是否符合資格。
- 啟用
BOARD_VNDK_VERSION=current
。您可以將 BoardConfig.mk
新增至 m -j BOARD_VNDK_VERSION=current MY-LIB
,或直接使用該元件建構元件 (例如 m -j BOARD_VNDK_VERSION=current MY-LIB
)。
啟用 BOARD_VNDK_VERSION=current
後,建構系統會強制執行下列依附元件和標頭規定。
管理依附元件
如果 vendor
物件依附於 core
元件,而該元件不存在於 vndk
中,或以 vendor
物件形式存在,則必須使用下列其中一個選項進行解析:
- 可以移除依附元件。
- 如果
core
元件由 vendor
擁有,則可標示為 vendor_available
或 vendor
。
- 變更內容可能會將核心物件納入
vndk
,並上傳至 Google。
此外,如果 core
元件依附於 vendor
元件,則必須將 vendor
元件設為 core
元件,或者必須以其他方式移除依附元件 (例如移除依附元件,或將依附元件移至 vendor
元件)。
必須移除全域標頭依附元件,讓建構系統知道是否要使用 -D__ANDROID_VNDK__
建構標頭。舉例來說,您仍可使用標頭程式庫 libutils_headers
存取 utils/StrongPointer.h
等 libutils 標頭。
部分標頭 (例如 unistd.h
) 無法再透過轉介方式納入,但可在本機納入。
最後,private/android_filesystem_config.h
的公開部分已移至 cutils/android_filesystem_config.h
。如要管理這些標頭,請執行下列任一操作:
- 盡可能將所有
AID_*
巨集替換為 getgrnam
/getpwnam
呼叫,以移除對 private/android_filesystem_config.h
的依附元件。例如:
(uid_t)AID_WIFI
會變成 getpwnam("wifi")->pw_uid
。
(gid_t)AID_SDCARD_R
會變成 getgrnam("sdcard_r")->gr_gid
。
詳情請參閱 private/android_filesystem_config.h
。
- 如果是硬式編碼的 AIS,請加入
cutils/android_filesystem_config.h
。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Enable VNDK\n\nThe Vendor Native Development Kit (VNDK) requires several changes to a codebase to separate\nconcerns between vendor and system. Use the following guide to enable VNDK in a vendor/OEM\ncodebase.\n\nBuild system libraries\n----------------------\n\nThe build system contains several types of objects including libraries\n(shared, static, or header) and binaries.\n\n**Figure 1.** Build system libraries.\n\n- `core` libraries are used by the system image, on the system image. These libraries can't be used by `vendor`, `vendor_available`, `vndk`, or `vndk-sp` libraries. \n\n ```carbon\n cc_library {\n name: \"libThatIsCore\",\n ...\n }\n ```\n- `vendor-only` (or `proprietary`) libraries are used by the vendor image, on the vendor image. \n\n ```carbon\n cc_library {\n name: \"libThatIsVendorOnly\",\n proprietary: true,\n # or: vendor: true, # (for things in AOSP)\n ...\n }\n ```\n- `vendor_available` libraries are used by the vendor image, on the vendor image (may contain duplicates of `core`). \n\n ```carbon\n cc_library {\n name: \"libThatIsVendorAvailable\",\n vendor_available: true,\n ...\n }\n ```\n- `vndk` libraries are used by the vendor image, on the system image. \n\n ```carbon\n cc_library {\n name: \"libThatIsVndk\",\n vendor_available: true,\n vndk: {\n enabled: true,\n }\n ...\n }\n ```\n- `vndk-sp` libraries are used by the vendor image, and also by the system image indirectly. \n\n ```carbon\n cc_library {\n name: \"libThatIsVndkSp\",\n vendor_available: true,\n vndk: {\n enabled: true,\n support_system_process: true,\n }\n ...\n }\n ```\n- `llndk` libraries are used by both the system and vendor images. \n\n ```carbon\n cc_library {\n name: \"libThatIsLlndk\",\n llndk: {\n symbol_file: \"libthatisllndk.map.txt\"\n }\n ...\n }\n ```\n\nWhen a lib is marked as `vendor_available:true`, it's built\ntwice:\n\n- Once for platform (and thus installed to `/system/lib`)\n- Once for vendor (and thus installed to `/vendor/lib` or VNDK APEX)\n\nThe vendor versions of libs are built with `-D__ANDROID_VNDK__`.\nPrivate system components that may change significantly in future versions of\nAndroid are disabled with this flag. In addition, different libraries export a\ndifferent set of headers (such as `liblog`). Options specific to a\nvendor variant of a target can be specified in an `Android.bp` file\nin: \n\n```text\ntarget: { vendor: { … } }\n```\n\nEnable VNDK for a codebase\n--------------------------\n\nTo enable the VNDK for a codebase:\n\n1. Determine eligibility by calculating the required sizes of `vendor.img` and `system.img` partitions.\n2. Enable `BOARD_VNDK_VERSION=current`. You can add to `BoardConfig.mk` or build components with it directly (for example, `m -j BOARD_VNDK_VERSION=current `\u003cvar translate=\"no\"\u003eMY-LIB\u003c/var\u003e).\n\nAfter enabling `BOARD_VNDK_VERSION=current`, the build system\nenforces the following dependency and header requirements.\n\n### Manage dependencies\n\nA `vendor` object that depends on a `core` component\nthat doesn't exist in `vndk` or as a `vendor` object\nmust be resolved using one of the following options:\n\n- The dependency can be removed.\n- If the `core` component is owned by `vendor`, it can be marked as `vendor_available` or `vendor`.\n- A change making the core object part of the `vndk` may be upstreamed to Google.\n\nIn addition, if a `core` component has dependencies on a\n`vendor` component, the `vendor` component must be made\ninto a `core` component **or** the dependency must be\nremoved in another way (for example, by removing the dependency or by moving the\ndependency into a `vendor` component).\n\n### Manage headers\n\nGlobal header dependencies must be removed to enable the build system to know\nwhether to build the headers with or without `-D__ANDROID_VNDK__`.\nFor example, libutils headers such as `utils/StrongPointer.h` can\nstill be accessed using the header library\n[`libutils_headers`](https://android.googlesource.com/platform/system/core/+/android16-release/libutils/include/utils).\n\nSome headers (such as `unistd.h`) can no longer be included transitively\nbut can be included locally.\n\nFinally, the public part of `private/android_filesystem_config.h`\nhas been moved to `cutils/android_filesystem_config.h`. To manage\nthese headers, do one of the following:\n\n- Remove the dependency to `private/android_filesystem_config.h` by replacing all `AID_*` macros with [getgrnam](http://man7.org/linux/man-pages/man3/getgrnam.3.html)/ [getpwnam](http://man7.org/linux/man-pages/man3/getpwnam.3.html) calls if possible. For example:\n - `(uid_t)AID_WIFI` becomes `getpwnam(\"wifi\")-\u003epw_uid`.\n - `(gid_t)AID_SDCARD_R` becomes `getgrnam(\"sdcard_r\")-\u003egr_gid`.\n\n For details, refer to [private/android_filesystem_config.h](https://android.googlesource.com/platform/system/core/+/android16-release/libcutils/include/private/android_filesystem_config.h).\n- For hard-coded AIS, include `cutils/android_filesystem_config.h`."]]