注意:关于Android.bp的权威解释可以参见 android.bp权威文档
Google官方语法文档https://android.googlesource.com/platform/build/soong
一. Android.bp语法初识
1.1 模块
从前面的列子可以看出定义一个模块从模块的类型开始,模块有不同的类型,如前面例子中的cc_library_shared,当然类型还有很多种,譬如cc_binary、android_app 、cc_library_static
等等。模块包含一些属性格式为“property-name:property-value”,其中name属性必须指定,其属性值必须是全局唯一的。
其中默认模块可用于在多个模块中重复相同的属性
cc_defaults {// //默认模块名称
name: "default_module",
shared_libs: ["libz"],
stl: "none",
}
cc_binary {
name: "test1",
defaults: ["default_module"], //引用默认模块名称
srcs: ["src/test/test.c"],
}
srcs 属性以字符串列表的形式指定用于编译模块的源文件。您可以使用模块引用语法 “:” 来引用生成源文件的其他模块的输出,如 genrule 或 filegroup。
实例说明:
~/aosp$ cd frameworks/base/core/java/
~/aosp/frameworks/base/core/java$ vi Android.bp
filegroup {
name: "IKeyAttestationApplicationIdProvider.aidl",
srcs: ["android/security/keymaster/IKeyAttestationApplicationIdProvider.aidl"],
}
filegroup {
name: "IDropBoxManagerService.aidl",
srcs: ["com/android/internal/os/IDropBoxManagerService.aidl"],
~/aosp/frameworks/base/core$ cd ..
~/aosp/frameworks/base$ vi libs/services/Android.bp
cc_library_shared {
name: "libservices",
srcs: [
":IDropBoxManagerService.aidl", //这里引用了上面定义的模板
"src/os/DropBoxManager.cpp",
"src/os/StatsDimensionsValue.cpp",
"src/os/StatsLogEventWrapper.cpp",
],
shared_libs: [
"libbinder",
"liblog",
"libcutils",
"libutils",
]
}
1.2 变量
我们知道Android.mk中可以定义变量,当然作为新编译系统中替代Android.mk的Android.bp也是一定存在,更加何况Android.mk还可以一定条件的转换成Android.bp。
变量范围限定为声明它们的文件的其余部分,可以使用 “=” 号赋值, 但是不能使用 “:=” 赋值。变量是不可变的,但有一个例外它们可以附上+= 赋值,但仅在变量被引用之前。
error: packages/apps/Bluetooth/jni/Android.bp:2:15: expected "=" or "+=" or "{" or "(", found ":"
ninja: error: rebuilding 'out/soong/.minibootstrap/build.ninja': subcommand failed
10:28:15 soong failed with: exit status 1
下面我们看一下正确使用变量的列子:
gzip_srcs = ["src/minigzip.c"],
cc_binary {
name: "gzip",
srcs: gzip_srcs,
shared_libs: ["libz"],
stl: "none",
}
1.3 注释
我们知道Android.mk中可以进行注释,当然Android.bp里面也可以,Android.mk中使用#
然后添加注释,Android.bp使用单行注释//
和多行注释/* */
两种方式。
1.4类型
具体支持以下几种类型:
- Bool(
true
orfalse
) - Integers(
int
) - Strings(
"string"
) - Listsof strings ([
"string1"
,"string2"
]) - Maps(
{key1: "value1", key2: ["value2"]}
)
1.5 操作符
String类型、字符串列表类型和Map类型支持操作符+
。
1.6 支持模块类型 *
Android.bp可以支持android_app、cc_binary、cc_binary_host等多种类型,具体定义在Android源码的build/soong/androidmk/cmd/androidmk/android.go
可以查看,具体如下:
var moduleTypes = map[string]string{
"BUILD_SHARED_LIBRARY": "cc_library_shared",
"BUILD_STATIC_LIBRARY": "cc_library_static",
"BUILD_HOST_SHARED_LIBRARY": "cc_library_host_shared",
"BUILD_HOST_STATIC_LIBRARY": "cc_library_host_static",
"BUILD_HEADER_LIBRARY": "cc_library_headers",
"BUILD_EXECUTABLE": "cc_binary",
"BUILD_HOST_EXECUTABLE": "cc_binary_host",
"BUILD_NATIVE_TEST": "cc_test",
"BUILD_HOST_NATIVE_TEST": "cc_test_host",
"BUILD_NATIVE_BENCHMARK": "cc_benchmark",
"BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host",
"BUILD_JAVA_LIBRARY": "java_library",
"BUILD_STATIC_JAVA_LIBRARY": "java_library_static",
"BUILD_HOST_JAVA_LIBRARY": "java_library_host",
"BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
"BUILD_PACKAGE": "android_app"
}
1.7 支持预编译类型
Android.bp可以支持多种预编译类型,具体定义在Android源码的build/soong/androidmk/cmd/androidmk/android.go
可以查看,如下图所示:
var prebuiltTypes = map[string]string{
"SHARED_LIBRARIES": "cc_prebuilt_library_shared",
"STATIC_LIBRARIES": "cc_prebuilt_library_static",
"EXECUTABLES": "cc_prebuilt_binary",
"JAVA_LIBRARIES": "prebuilt_java_library",
}
1.8 条件式编译
例如:system/core/libusbhost/Android.bp
aosp9.0开始
cc_library {
name: "libusbhost",
vendor_available: true,
vndk: {
enabled: true,
},
host_supported: true,
srcs: ["usbhost.c"],
cflags: ["-Werror"],
export_include_dirs: ["include"],
target: {
android: { //编译Android上运行的程序
cflags: [
"-g",
"-DUSE_LIBLOG",
],
shared_libs: ["liblog"],
},
darwin: { //编译darwin上运行的程序
enabled: false,
},
},
}
二. Android.bp 举个例子
Android.bp是一门实战性的东西,光说不练没有啥用,说再多不如直接开练来得舒服。那就直接开始手撕实例了,让我们开战吗!
下面几种库编译类型:
2.1 动态库类型
最终编译为so包
cc_library_shared { //编译成动态库,类似于Android.mk中的BUILD_SHARED_LIBRARY
name: "libbluetooth_jni", //编译出的模块的名称,类似于Android.mk中的LOCAL_MODULE
srcs: [ //源文件,类似于Android.mk中的LOCAL_SRC_FILES
"com_android_bluetooth_btservice_AdapterService.cpp",
"com_android_bluetooth_hfp.cpp",
"com_android_bluetooth_hfpclient.cpp",
"com_android_bluetooth_a2dp.cpp",
"com_android_bluetooth_a2dp_sink.cpp",
"com_android_bluetooth_avrcp.cpp",
"com_android_bluetooth_avrcp_controller.cpp",
"com_android_bluetooth_hid.cpp",
"com_android_bluetooth_hidd.cpp",
"com_android_bluetooth_hdp.cpp",
"com_android_bluetooth_pan.cpp",
"com_android_bluetooth_gatt.cpp",
"com_android_bluetooth_sdp.cpp",
],
include_dirs: [ //用户指定的头文件查找路径,类似于Android.mk中的LOCAL_C_INCLUDES
"libnativehelper/include/nativehelper",
"system/bt/types",
],
shared_libs: [ //编译所依赖的动态库,类似于Android.mk中的LOCAL_SHARED_LIBRARIES
"libandroid_runtime",
"libchrome",
"libnativehelper",
"libcutils",
"libutils",
"liblog",
"libhardware",
],
static_libs: [ //编译所依赖的静态库,类似于Android.mk中的LOCAL_STATIC_LIBRARIES
"libbluetooth-types",
],
cflags: [ ///编译flag,类似于Android.mk中的LOCAL_CFLAGS
"-Wall",
"-Wextra",
"-Wno-unused-parameter",
],
}
**2.2 java库类型: **
最终编译为jar包
java_library { /编译成java库
name: "services",//编译出的模块的名称
dex_preopt: {
app_image: true,
profile: "art-profile",
},
srcs: [ //源文件
"java/**/*.java",
],
// The convention is to name each service module 'services.$(module_name)'
static_libs: [ //编译所依赖的静态库
"services.core",
"services.accessibility",
"services.appwidget",
"services.autofill",
"services.backup",
"services.companion",
"services.coverage",
"services.devicepolicy",
"services.midi",
"services.net",
"services.print",
"services.restrictions",
"services.usage",
"services.usb",
"services.voiceinteraction",
"android.hidl.base-V1.0-java",
],
libs: [
"android.hidl.manager-V1.0-java",
],
// Uncomment to enable output of certain warnings (deprecated, unchecked)
//javacflags: ["-Xlint"],
}
动态库:可以被 install/copy 到应用程序包(apk)
静态库:可以被链接入动态库
- name : 模块的名称
- src : 模块的源码
- include_dirs : 指定的头文件查找路径
- shared_libs : 编译时依赖的动态库
- static_libs : 编译时依赖的静态库
- subdirs : 是一个文件级的顶层属性,指定后会查找次级目录下的Android.bp。
2.3 Andorid应用类型
最终编译为apk包
android_app {
name: "NFC",
srcs: [
"src/**/*.java",
"nci/**/*.java",
":statslog-Nfc-java-gen",
],
platform_apis: true,
certificate: "platform",
jni_libs: ["libsn100nfc_nci_jni"],
libs: ["com.nxp.nfc.nq"],
static_libs: [
"androidx.appcompat_appcompat",
"nearme_nfc",
],
optimize: {
enabled: true,
obfuscate: true,
proguard_flags_files: ["proguard.flags"],
},
}
java_import {
name: "nearme_nfc",
host_supported: true,
jars: [
"libs/env.jar",
],
}
- platform_apis : 用 sdk 的 hide 的 api 來编译
- certificate : 指定用的是什么签名,如上用的是 platform 签名。
- jni_libs : 依赖使用的 JNI 库
- libs : 工程中的 libs 库
- static_libs : 静态库,其中 nearme_nfc 为下方定义的:java_import
- optimize : 压缩配置,enabled 是否开启,obfuscate 是否开启混淆,proguard_flags_files 混淆规则配置文件
- host_supported : Android.bp 文件要求每个模块拥有唯一的名称,但每个模块可以内置多种变化,例如可以添加 host_supported: true。