Soong is one of the build systems used in Android, which is controlled by files called `Android.bp`.
There is also the legacy Make-based build system that is controlled by files called `Android.mk`.
`Android.bp` file are JSON-like declarative descriptions of"modules"to build;
a "module"is the basic unitof building that Soong understands, similarly to
how "target"is the basic unitof building for Make.
For simple changes (fixing typos, obvious optimizations, etc.), sending a code
review request is enough. For more substantial changes, file a bug in our
[bug tracker](https://issuetracker.google.com/issues/new?component=381517) or orwrite us at android-building@googlegroups.com .
## Android.bp fileformat
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.
### Modules
A modulein an Android.bp file starts with a moduletype, followed by a setof
properties in `name: value,` format:
Properties that take a list of files can also take glob patterns and output path
expansions.
* Glob patterns can contain the normal Unix wildcard `*`, for example `"*.java"`.
Glob patterns can also contain a single `**` wildcard as a path element, which
will match zero or more path elements. For example, `java/**/*.java` will match
`java/Main.java` and `java/com/android/Main.java`.
* Output path expansions take the format `:module` or `:module{.tag}`, where
`module` is the name of a module that produces output files, and it expands to
a list of those output files. With the optional `{.tag}` suffix, the module
may produce a different list of outputs according to `tag`.
For example, a `droiddoc` module with the name "my-docs" would return its
`.stubs.srcjar` output with `":my-docs"`, and its `.doc.zip` file with
`":my-docs{.doc.zip}"`.
This is commonly used to reference `filegroup` modules, whose output files
consist of their `srcs`.
### Variables
An Android.bp file may contain top-level variable assignments:
```
gzip_srcs = ["src/test/minigzip.c"],
Variables are scoped to the remainder of the file they are declared in, as well asany child Android.bp 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 are dynamically typed
based on the first assignment, and properties are statically typed by the moduletype. The supported types are: * Bool (`true` or `false`) * Integers (`int`) * Strings (`"string"`) * Lists of strings (`["string1", "string2"]`) * Maps (`{key1: "value1", key2: ["value2"]}`)
Maps may contain values ofanytype, including nested maps. Lists and maps may
have trailing commas after the last value.
Strings can contain double quotes using `\"`, for example `"cat \"a b\""`.
### Operators
The `+` operator: * Sums integers. * Concatenates strings and lists. * Produces the union of maps.
Concatenating maps produces a map whose keys are the union of the given maps'
keys, and whose mapped values are the union of the given maps' corresponding
mapped values.
### Defaults modules
A `defaults` module can be used to repeat the same properties in multiple
modules. For example:
The build is organized into packages where each package is a collection of related files and a
specification of the dependencies among them in the formof modules.
A package is defined as a directory containing a file named `Android.bp`, residing beneath the
top-level directory in the build and its name is its path relative to the top-level directory. A
package includes all files in its directory, plus all subdirectories beneath it, except those which
themselves contain an `Android.bp` file.
The modules in a package's `Android.bp` and included files are part of the package.
For example, in the following directory tree (where `.../android/` is the top-level Android
directory) there are two packages, `my/app`, and the subpackage `my/app/tests`. Note that
`my/app/data` is not a package, but a directory belonging to package `my/app`.
The `package` moduletype allows information to be specified about a package. Only a single
`package` module can be specified per package andin the casewhere there are multiple `.bp` files in the same package directory it is highly recommended that the `package` module (if required) is specifiedin the `Android.bp` file.
Unlike most moduletype `package` does not have a `name` property. Instead the name issetto the
name of the package, e.g. if the package isin `top/intermediate/package` then the package name is
`//top/intermediate/package`.
E.g. The following will set the default visibility for all the modules defined in the package and any subpackages that do not set their own default visibility (irrespective of whether they are in
the same `.bp` fileas the `package` module) to be visible to all the subpackages by default.
Obviously, this works only if there is only one `libfoo` modulein the source
tree. Ensuring such name uniqueness for larger trees may become problematic. We
might also want to use the same name in multiple mutually exclusive subtrees
(for example, implementing different devices) deliberately inordertodescribe
a functionally equivalent module. Enter Soong namespaces.
#### Namespaces
The presence of the `soong_namespace {..}` in an Android.bp file defines a **namespace**. For instance, having
* File -> Open, open the `build/soong` directory. It will be opened as a new project. * File -> Settings, then Languages & Frameworks -> Go -> GOROOT, then set it to `prebuilts/go/linux-x86` * File -> Project Structure, then, Project Settings -> Modules, then Add ContentRoot,thenaddthe`build/blueprint`directory. * Optional: also add the `external/golang-protobuf` directory. In practice, IntelliJseemstoworkwellenoughwithoutthis,too.
* Run -> Edit Configurations... * Choose "Go Remote" on the left * Click on the "+" buttion on the top-left * Give it a nice _name_ and set "Host" to `localhost` and "Port" to the port in the environmentvariable(`SOONG_UI_DELVE`for`soong_ui`,`SOONG_DELVE`for `soong_build`) * Set the breakpoints where you want application to stop * Run the build from the command line * In IntelliJ, click Run -> Debug _name_ * Observe _Connecting..._ message in the debugger pane. It changes to _Connected_oncethecommunicationwiththedebuggerhasbeenestablished;the terminalwindowwherethebuildstartedwilldisplay `APIserverlisteningat...`message