如何在 Android.bp 中添加条件代码
How to add conditional code in Android.bp
AOSP 现在有了新的构建系统,文件 Android.bp
已经在很多地方取代了 Android.mk
。
现在我想根据平台有条件地列出源文件。
这样说:
if(atom)
{
src: [
.......list of files.......
],
exclude_srcs: [
.......list of files.......
]
} else
{
src: [
.......list of files.......
],
exclude_srcs: [
.......list of files.......
]
}
有什么建议可以实现吗?
另外,如何在条件语句中实现 NOT
、OR
等逻辑运算?
提前致谢。
引用文档:
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.
您可以阅读here。
这是一个关于如何在 bp 文件中添加条件的示例:
cc_library {
...
srcs: ["generic.cpp"],
arch: {
arm: {
srcs: ["arm.cpp"],
},
x86: {
srcs: ["x86.cpp"],
},
},
}
GO
你可以根据你的条件写一个go脚本。关注
Is there a way to add/remove module in Android.bp?
条件编译
如果您只想有条件地包含一个模块,您可以定义许多 Android.bp
个模块并根据 Android.mk
文件中的条件包含它们。
https://android.googlesource.com/platform/build/soong/+/HEAD/docs/best_practices.md#removing-conditionals
soong_config_modules
如果您的任何 Android.bp
文件包含您的 AOSP 不支持的库,您将面临解析错误。
要解决这个问题,您可以使用 soong_config_modules
。请注意,这仅在 Android 11 R
之后受支持。
不过,我给你举个例子。
此处我在 AOSP 为 Android12 时包含特殊库。由于这些库可能不存在于较低版本中,因此我不会在 PRODUCT_PACKAGES
中包含 iot-camera-system.mk
,因此它不会作为预装应用包含在内。
我要用 soong_config_modules
实现的具体目标是消除这些库不存在时的解析错误(Android 解析所有 Android.bp
文件以形成解析树,它还检查因为它们的依赖关系存在,当它们不存在时构建失败)。
iot-camera-system.mk
# CameraApp Android Application Package
ifeq ($(PLATFORM_VERSION), $(filter $(PLATFORM_VERSION),S 12))
PRODUCT_PACKAGES += CameraApp
SOONG_CONFIG_NAMESPACES += camera
SOONG_CONFIG_camera += camtargets
SOONG_CONFIG_camera_camtargets := newCameraTarget
else
SOONG_CONFIG_NAMESPACES += camera
SOONG_CONFIG_camera += camtargets
SOONG_CONFIG_camera_camtargets := oldCameraTarget
endif
Android.bp
// This introduces the module type camera_cc_defaults
// If target.mk file contained:
//
// SOONG_CONFIG_NAMESPACES += camera
// SOONG_CONFIG_camera += camtargets
// SOONG_CONFIG_camera_camtargets := newCameraTarget
//
// Then our libs would build with static_libs
soong_config_module_type {
name: "camera_cc_defaults",
module_type: "cc_defaults",
config_namespace: "camera",
variables: ["camtargets"],
properties: ["static_libs"],
}
soong_config_string_variable {
name: "camtargets",
values: ["oldCameraTarget", "newCameraTarget"],
}
camera_cc_defaults {
name: "camera_defaults",
soong_config_variables: {
camtargets: {
oldCameraTarget: {
static_libs: [
],
},
newCameraTarget: {
static_libs: [
"androidx.core_core-ktx",
"androidx.fragment_fragment-ktx",
"androidx.navigation_navigation-fragment-ktx",
"androidx.navigation_navigation-ui-ktx",
"androidx.lifecycle_lifecycle-runtime-ktx",
"kotlinx_coroutines",
"kotlinx_coroutines_android",
],
},
},
},
}
android_library {
name: "utils",
defaults: ["camera_defaults"],
manifest: "utils/src/main/AndroidManifest.xml",
platform_apis: true,
srcs: [
"utils/src/main/java/com/example/android/camera/utils/*.kt",
],
resource_dirs: [
"utils/src/main/res/",
],
static_libs: [
"androidx-constraintlayout_constraintlayout",
"androidx.appcompat_appcompat",
"androidx.localbroadcastmanager_localbroadcastmanager",
"com.google.android.material_material",
"androidx.exifinterface_exifinterface",
"androidx.core_core",
"androidx.preference_preference",
"androidx.fragment_fragment",
"androidx.recyclerview_recyclerview",
"androidx.lifecycle_lifecycle-runtime",
"androidx.lifecycle_lifecycle-extensions",
"kotlin-stdlib",
"kotlin-reflect",
"gson-prebuilt-jar",
],
optimize: {
enabled: false,
},
dex_preopt: {
enabled: false,
},
}
android_app {
name: "CameraApp",
defaults: ["camera_defaults"],
manifest: "app/src/main/AndroidManifest.xml",
privileged: true,
platform_apis: true,
certificate: "platform",
srcs: [
"app/src/main/java/com/example/android/camera2/video/*.kt",
"app/src/main/java/com/example/android/camera2/video/fragments/*.kt",
"app/src/main/java/com/example/android/camera2/video/overlay/*.kt",
],
resource_dirs: [
"app/src/main/res/",
],
static_libs: [
"androidx-constraintlayout_constraintlayout",
"androidx.appcompat_appcompat",
"androidx.localbroadcastmanager_localbroadcastmanager",
"com.google.android.material_material",
"androidx.exifinterface_exifinterface",
"androidx.core_core",
"androidx.preference_preference",
"androidx.fragment_fragment",
"androidx.recyclerview_recyclerview",
"androidx.lifecycle_lifecycle-runtime",
"androidx.lifecycle_lifecycle-extensions",
"kotlin-stdlib",
"kotlin-reflect",
"gson-prebuilt-jar",
"utils",
],
optimize: {
enabled: false,
},
dex_preopt: {
enabled: false,
},
}
AOSP 现在有了新的构建系统,文件 Android.bp
已经在很多地方取代了 Android.mk
。
现在我想根据平台有条件地列出源文件。
这样说:
if(atom)
{
src: [
.......list of files.......
],
exclude_srcs: [
.......list of files.......
]
} else
{
src: [
.......list of files.......
],
exclude_srcs: [
.......list of files.......
]
}
有什么建议可以实现吗?
另外,如何在条件语句中实现 NOT
、OR
等逻辑运算?
提前致谢。
引用文档:
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.
您可以阅读here。
这是一个关于如何在 bp 文件中添加条件的示例:
cc_library {
...
srcs: ["generic.cpp"],
arch: {
arm: {
srcs: ["arm.cpp"],
},
x86: {
srcs: ["x86.cpp"],
},
},
}
GO
你可以根据你的条件写一个go脚本。关注
Is there a way to add/remove module in Android.bp?
条件编译
如果您只想有条件地包含一个模块,您可以定义许多 Android.bp
个模块并根据 Android.mk
文件中的条件包含它们。
https://android.googlesource.com/platform/build/soong/+/HEAD/docs/best_practices.md#removing-conditionals
soong_config_modules
如果您的任何 Android.bp
文件包含您的 AOSP 不支持的库,您将面临解析错误。
要解决这个问题,您可以使用 soong_config_modules
。请注意,这仅在 Android 11 R
之后受支持。
不过,我给你举个例子。
此处我在 AOSP 为 Android12 时包含特殊库。由于这些库可能不存在于较低版本中,因此我不会在 PRODUCT_PACKAGES
中包含 iot-camera-system.mk
,因此它不会作为预装应用包含在内。
我要用 soong_config_modules
实现的具体目标是消除这些库不存在时的解析错误(Android 解析所有 Android.bp
文件以形成解析树,它还检查因为它们的依赖关系存在,当它们不存在时构建失败)。
iot-camera-system.mk
# CameraApp Android Application Package
ifeq ($(PLATFORM_VERSION), $(filter $(PLATFORM_VERSION),S 12))
PRODUCT_PACKAGES += CameraApp
SOONG_CONFIG_NAMESPACES += camera
SOONG_CONFIG_camera += camtargets
SOONG_CONFIG_camera_camtargets := newCameraTarget
else
SOONG_CONFIG_NAMESPACES += camera
SOONG_CONFIG_camera += camtargets
SOONG_CONFIG_camera_camtargets := oldCameraTarget
endif
Android.bp
// This introduces the module type camera_cc_defaults
// If target.mk file contained:
//
// SOONG_CONFIG_NAMESPACES += camera
// SOONG_CONFIG_camera += camtargets
// SOONG_CONFIG_camera_camtargets := newCameraTarget
//
// Then our libs would build with static_libs
soong_config_module_type {
name: "camera_cc_defaults",
module_type: "cc_defaults",
config_namespace: "camera",
variables: ["camtargets"],
properties: ["static_libs"],
}
soong_config_string_variable {
name: "camtargets",
values: ["oldCameraTarget", "newCameraTarget"],
}
camera_cc_defaults {
name: "camera_defaults",
soong_config_variables: {
camtargets: {
oldCameraTarget: {
static_libs: [
],
},
newCameraTarget: {
static_libs: [
"androidx.core_core-ktx",
"androidx.fragment_fragment-ktx",
"androidx.navigation_navigation-fragment-ktx",
"androidx.navigation_navigation-ui-ktx",
"androidx.lifecycle_lifecycle-runtime-ktx",
"kotlinx_coroutines",
"kotlinx_coroutines_android",
],
},
},
},
}
android_library {
name: "utils",
defaults: ["camera_defaults"],
manifest: "utils/src/main/AndroidManifest.xml",
platform_apis: true,
srcs: [
"utils/src/main/java/com/example/android/camera/utils/*.kt",
],
resource_dirs: [
"utils/src/main/res/",
],
static_libs: [
"androidx-constraintlayout_constraintlayout",
"androidx.appcompat_appcompat",
"androidx.localbroadcastmanager_localbroadcastmanager",
"com.google.android.material_material",
"androidx.exifinterface_exifinterface",
"androidx.core_core",
"androidx.preference_preference",
"androidx.fragment_fragment",
"androidx.recyclerview_recyclerview",
"androidx.lifecycle_lifecycle-runtime",
"androidx.lifecycle_lifecycle-extensions",
"kotlin-stdlib",
"kotlin-reflect",
"gson-prebuilt-jar",
],
optimize: {
enabled: false,
},
dex_preopt: {
enabled: false,
},
}
android_app {
name: "CameraApp",
defaults: ["camera_defaults"],
manifest: "app/src/main/AndroidManifest.xml",
privileged: true,
platform_apis: true,
certificate: "platform",
srcs: [
"app/src/main/java/com/example/android/camera2/video/*.kt",
"app/src/main/java/com/example/android/camera2/video/fragments/*.kt",
"app/src/main/java/com/example/android/camera2/video/overlay/*.kt",
],
resource_dirs: [
"app/src/main/res/",
],
static_libs: [
"androidx-constraintlayout_constraintlayout",
"androidx.appcompat_appcompat",
"androidx.localbroadcastmanager_localbroadcastmanager",
"com.google.android.material_material",
"androidx.exifinterface_exifinterface",
"androidx.core_core",
"androidx.preference_preference",
"androidx.fragment_fragment",
"androidx.recyclerview_recyclerview",
"androidx.lifecycle_lifecycle-runtime",
"androidx.lifecycle_lifecycle-extensions",
"kotlin-stdlib",
"kotlin-reflect",
"gson-prebuilt-jar",
"utils",
],
optimize: {
enabled: false,
},
dex_preopt: {
enabled: false,
},
}