自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
Android.bp 檔案格式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android.bp
檔案的設計簡單,這些檔案不含條件或控制流程陳述式;所有複雜性都由以 Go 編寫的建構邏輯處理。如果可行,Android.bp
檔案的語法和語意會與 Bazel BUILD 檔案相似。
模組
Android.bp
檔案中的模組會以模組類型開頭,後面接著 name: "value",
格式的一組屬性:
cc_binary {
name: "gzip",
srcs: ["src/test/minigzip.c"],
shared_libs: ["libz"],
stl: "none",
}
每個模組都必須有 name
屬性,且在所有 Android.bp
檔案中,該值都必須是唯一的,但 namespace 和預先建構模組中的 name
屬性值可以重複。
srcs
屬性會將用於建構模組的來源檔案指定為字串清單。您可以使用模組參照語法 ":<module-name>"
,參照產生來源檔案 (例如 genrule
或 filegroup
) 的其他模組輸出內容。
如需有效模組類型及其屬性的清單,請參閱 Soong 模組參考資料。
類型
變數和屬性具有強型別,變數會根據第一次指派動態運作,而屬性則由模組類型靜態設定。支援的類型如下:
- 布林值 (
true
或 false
)
- 整數 (
int
)
- 字串 (
"string"
)
- 字串清單 (
["string1", "string2"]
)
- 地圖 (
{key1: "value1", key2: ["value2"]}
)
對應可包含任何類型的值,包括巢狀對應。清單和地圖的最後一個值後面可能會加上半形逗號。
Globs
接收檔案清單的屬性 (例如 srcs
) 也能使用 glob 模式。Glob 模式可包含一般 UNIX 萬用字元 *
,例如 *.java
。Glob 模式也可以包含單一 **
萬用字元做為路徑元素,這可比對零或多個路徑元素。例如,java/**/*.java
可同時符合 java/Main.java
和 java/com/android/Main.java
模式。
變數
Android.bp
檔案可能包含頂層變數指派:
gzip_srcs = ["src/test/minigzip.c"],
cc_binary {
name: "gzip",
srcs: gzip_srcs,
shared_libs: ["libz"],
stl: "none",
}
變數的範圍會限制在其宣告所在檔案的其餘部分,以及任何子藍圖檔案。變數不可變動,但有一個例外:可以透過 +=
指派附加至變數,但只能在變數被參照之前。
Android.bp
檔案可包含 C 樣式的多行 /* */
和 C++ 樣式的單行 //
註解。
運算子
您可以使用 + 運算子附加字串、字串清單和地圖。您可以使用 +
運算子來求出整數的總和。附加對應會產生兩個對應中的鍵的聯集,並附加兩個對應中存在的任何鍵的值。
條件式
Soong 不支援 Android.bp
檔案中的條件式。相反地,在需要條件式功能的建構規則中,我們會在 Go 中處理複雜性,這樣一來,您就能使用高階語言功能,並追蹤條件式功能引進的隱含依附元件。大多數的條件會轉換為對應屬性,其中會選取對應中的其中一個值,並附加至頂層屬性。
舉例來說,如要支援特定架構的檔案:
cc_library {
...
srcs: ["generic.cpp"],
arch: {
arm: {
srcs: ["arm.cpp"],
},
x86: {
srcs: ["x86.cpp"],
},
},
}
Soong 包含藍圖檔案的標準格式化工具,類似於 gofmt。如要遞迴重新格式化目前目錄中的所有 Android.bp
檔案,請執行:
bpfmt -w .
標準格式包括四個空格的縮排、多元素清單的每個元素後面都有新行,以及清單和對應項目中的尾隨半形逗號。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Android.bp file format\n\nBy design, `Android.bp` files are simple. They don't contain conditionals or\ncontrol flow statements; all complexity is handled by build logic written in\nGo. When possible, the syntax and semantics of `Android.bp` files are similar to\n[Bazel BUILD files](https://bazel.build/reference/be/overview).\n\n### Modules\n\nA module in an `Android.bp` file starts with a\n[module type](https://ci.android.com/builds/latest/branches/aosp-build-tools/targets/linux/view/soong_build.html)\nfollowed by a set of properties in `name: \"value\",` format: \n\n cc_binary {\n name: \"gzip\",\n srcs: [\"src/test/minigzip.c\"],\n shared_libs: [\"libz\"],\n stl: \"none\",\n }\n\nEvery module must have a `name` property, and the value must be unique across\nall `Android.bp` files, except for the `name` property values in\nnamespaces and prebuilt modules, which may repeat.\n\nThe `srcs` property specifies the source files used to build the module, as a\nlist of strings. You can reference the output of other modules that produce\nsource files, like `genrule` or `filegroup`, by using the module reference\nsyntax `\":\u003cmodule-name\u003e\"`.\n\nFor a list of valid module types and their properties, see the\n[Soong Modules Reference](https://ci.android.com/builds/latest/branches/aosp-build-tools/targets/linux/view/soong_build.html).\n\n### Types\n\nVariables and properties are strongly typed, with variables dynamically based on\nthe first assignment, and properties set statically by the module type. The\nsupported types are:\n\n- Booleans (`true` or `false`)\n- Integers (`int`)\n- Strings (`\"string\"`)\n- Lists of strings (`[\"string1\", \"string2\"]`)\n- Maps (`{key1: \"value1\", key2: [\"value2\"]}`)\n\nMaps may contain values of any type, including nested maps. Lists and maps may\nhave trailing commas after the last value.\n\n### Globs\n\nProperties that take a list of files, such as `srcs`, can also take glob\npatterns. Glob patterns can contain the normal UNIX wildcard `*`, for example\n`*.java`. Glob patterns can also contain a single `**` wildcard as a path\nelement, which matches zero or more path elements. For example,\n`java/**/*.java` matches both the `java/Main.java` and\n`java/com/android/Main.java` patterns.\n\n### Variables\n\nAn `Android.bp` file may contain top-level variable assignments: \n\n gzip_srcs = [\"src/test/minigzip.c\"],\n cc_binary {\n name: \"gzip\",\n srcs: gzip_srcs,\n shared_libs: [\"libz\"],\n stl: \"none\",\n }\n\nVariables are scoped to the remainder of the file they are declared in, as well\nas any child Blueprint files. Variables are immutable with one exception: they\ncan be appended to with a `+=` assignment, but only before they've been\nreferenced.\n\n### Comments\n\n`Android.bp` files can contain C-style multiline `/* */` and C++ style\nsingle-line `//` comments.\n\n### Operators\n\nStrings, lists of strings, and maps can be appended using the + operator.\nIntegers can be summed up using the `+` operator. Appending a map produces the\nunion of keys in both maps, appending the values of any keys that are present in\nboth maps.\n\n### Conditionals\n\nSoong doesn't support conditionals in `Android.bp` files. Instead,\ncomplexity in build rules that would require conditionals are handled in Go,\nwhere high-level language features can be used, and implicit dependencies\nintroduced by conditionals can be tracked. Most conditionals are converted to a\nmap property, where one of the values in the map is selected and appended\nto the top-level properties.\n\nFor example, to support architecture-specific files: \n\n cc_library {\n ...\n srcs: [\"generic.cpp\"],\n arch: {\n arm: {\n srcs: [\"arm.cpp\"],\n },\n x86: {\n srcs: [\"x86.cpp\"],\n },\n },\n }\n\n### Formatter\n\nSoong includes a canonical formatter for Blueprint files, similar to\n[gofmt](https://golang.org/cmd/gofmt/). To recursively reformat all\n`Android.bp` files in the current directory, run: \n\n bpfmt -w .\n\nThe canonical format includes four-space indents, new lines after every element\nof a multielement list, and a trailing comma in lists and maps."]]