Soong

Soong 是 Android 新一代的构建系统,替代了传统的 Android.mk 文件,采用更简洁的 Android.bp 文件格式。本文介绍了 Soong 的核心概念,包括模块定义、变量使用、条件表达式的实现方式以及默认模块的使用等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Soong

Soong is the replacement for the old Android make-based build system. It replaces Android.mk files with Android.bp files, which are JSON-like simple declarative descriptions of modules to build.

Android.bp file format

By design, Android.bp files are very simple. There are no conditionals or control flow statements - any complexity is handled in build logic written in Go. The syntax and semantics of Android.bp files are intentionally similar to Bazel BUILD files when possible.

Modules

A module in an Android.bp file starts with a module type, followed by a set of properties in name: value, format:

cc_binary {
    name: "gzip",
    srcs: ["src/test/minigzip.c"],
    shared_libs: ["libz"],
    stl: "none",
}

Every module must have a name property, and the value must be unique across all Android.bp files.

For a list of valid module types and their properties see

$OUT_DIR/soong/.bootstrap/docs/soong_build.html

Variables

An Android.bp file may contain top-level variable assignments:

gzip_srcs = ["src/test/minigzip.c"],

cc_binary {
    name: "gzip",
    srcs: gzip_srcs,
    shared_libs: ["libz"],
    stl: "none",
}

Variables are scoped to the remainder of the file they are declared in, as well as any child blueprint files. Variables are immutable with one exception - they can be appended to with a += assignment, but only before they have been referenced.

Comments

Android.bp files can contain C-style multiline /* */ and C++ style single-line // comments.

Types

Variables and properties are strongly typed, variables dynamically based on the first assignment, and properties statically by the module type. The supported types are:

Bool (true or false)
Strings (“string”)
Lists of strings ([“string1”, “string2”])
Maps ({key1: “value1”, key2: [“value2”]})
Maps may values of any type, including nested maps. Lists and maps may have trailing commas after the last value.

Operators

Strings, lists of strings, and maps can be appended using the + operator. Appending a map produces the union of keys in both maps, appending the values of any keys that are present in both maps.

Defaults modules

A defaults module can be used to repeat the same properties in multiple modules. For example:

cc_defaults {
    name: "gzip_defaults",
    shared_libs: ["libz"],
    stl: "none",
}

cc_binary {
    name: "gzip",
    defaults: ["gzip_defaults"],
    srcs: ["src/test/minigzip.c"],
}

Formatter

Soong includes a canonical formatter for blueprint files, similar to gofmt. To recursively reformat all Android.bp files in the current directory:

bpfmt -w .
The canonical format includes 4 space indents, newlines after every element of a multi-element list, and always includes a trailing comma in lists and maps.

Convert Android.mk files

Soong includes a tool perform a first pass at converting Android.mk files to Android.bp files:

androidmk Android.mk > Android.bp
The tool converts variables, modules, comments, and some conditionals, but any custom Makefile rules, complex conditionals or extra includes must be converted by hand.

Differences between Android.mk and Android.bp

Android.mk files often have multiple modules with the same name (for example for static and shared version of a library, or for host and device versions). Android.bp files require unique names for every module, but a single module can be built in multiple variants, for example by adding host_supported: true. The androidmk converter will produce multiple conflicting modules, which must be resolved by hand to a single module with any differences inside target: { android: { }, host: { } } blocks.
Build logic

The build logic is written in Go using the blueprint framework. Build logic receives module definitions parsed into Go structures using reflection and produces build rules. The build rules are collected by blueprint and written to a ninja build file.

FAQ

How do I write conditionals?

Soong deliberately does not support conditionals in Android.bp files. Instead, complexity in build rules that would require conditionals are handled in Go, where high level language features can be used and implicit dependencies introduced by conditionals can be tracked. Most conditionals are converted to a map property, where one of the values in the map will be selected and appended to the top level properties.

For example, to support architecture specific files:

cc_library {
    ...
    srcs: ["generic.cpp"],
    arch: {
        arm: {
            srcs: ["arm.cpp"],
        },
        x86: {
            srcs: ["x86.cpp"],
        },
    },
}

See art/build/art.go or external/llvm/soong/llvm.go for examples of more complex conditionals on product variables or environment variables.

### Soong 构建系统在 Android 开发中的应用 SoongAndroid 中用于替代旧版 Makefile 的新一代构建工具。其设计目标是为了简化和加速大型项目的构建过程,提供更清晰的依赖关系管理以及更好的并行化支持。 #### 特点与优势 Soong 使用基于 Go 语言实现,并采用 `.bp` 文件作为配置描述文件,相比传统的 `Android.mk` 或者 `Makefile` 更加直观易读[^1]。通过 JSON-like 格式的定义方式来声明模块及其属性,使得开发者可以更容易理解和维护项目结构。 对于预编译资源(如 APK、JAR 和其他二进制文件),Soong 提供了一种机制可以在最终镜像中自动包含这些静态库或应用程序而无需额外的手动操作。这不仅提高了工作效率也减少了人为错误的可能性。 当遇到找不到特定模块下的 `Android.mk` 错误时,通常是因为当前路径下确实不存在对应的构建脚本或者是环境变量设置不正确导致查找失败。此时应该确认源码树内是否存在相应的 .bp 文件并且确保所有必要的环境已经正确初始化[^2]。 如果遇到了类似于 "depends on undefined module" 这样的错误,则可能是由于某些模块未被正确定义或是存在拼写上的差异所引起的。建议仔细检查相关模块名称是否一致,并且按照官方文档指南调整 blueprints 配置以解决问题[^4]。 ```bash # 查看 soong_ui 帮助信息 soong_ui help ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值