规则
- android_binary
- aar_import
- android_library
- android_instrument_test
- android_local_test
- android_device
- android_ndk_repo
- android_sdk_repo
android_binary
android_binary(name, deps, srcs, assets, assets_dir, compatible_with, crunch_png, custom_package, debug_key, debug_signing_keys, debug_signing_lineage_file, densities, deprecation, dex_shards, dexopts, distribs, enable_data_binding, exec_compatible_with, exec_properties, features, incremental_dexing, instruments, javacopts, key_rotation_min_sdk, licenses, main_dex_list, main_dex_list_opts, main_dex_proguard_specs, manifest, manifest_values, multidex, nocompress_extensions, package_id, plugins, proguard_apply_dictionary, proguard_apply_mapping, proguard_generate_mapping, proguard_specs, resource_configuration_filters, resource_files, restricted_to, shrink_resources, tags, target_compatible_with, testonly, visibility)
生成 Android 应用软件包文件 (.apk)。
隐式输出目标
name.apk
:使用调试密钥签名的 Android 应用软件包文件并进行 压缩对齐,可用于开发和调试应用。使用调试密钥进行签名时,您无法发布应用。name_unsigned.apk
:上述文件的未签名版本,可以在公开发布之前使用发布密钥进行签名。name_deploy.jar
:包含此目标的传递闭包的 Java 归档。部署 jar 包含类加载器从头到尾搜索此目标的运行时类路径时能找到的所有类。
name_proguard.jar
:包含对name_deploy.jar
运行 ProGuard 的结果的 Java 归档。 只有在指定了 proguard_specs 属性时,系统才会生成此输出。name_proguard.map
:在name_deploy.jar
上运行 ProGuard 的映射文件结果。只有在指定了 proguard_specs 属性且设置了 proguard_generate_mapping 或 shrink_resources 的情况下,系统才会生成此输出。
示例
您可以在 Bazel 源代码树的 examples/android
目录中找到 Android 规则示例。
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
deps
|
android_library 、java_library (具有 android 约束条件)以及 cc_library (针对 Android 目标平台封装或生成 .so 原生库)。
|
srcs
|
系统会编译 系统会解压缩并编译类型为 |
assets
|
assets 目录下所有文件的 glob 。您还可以引用其他软件包中的其他规则(任何生成文件的规则)或导出文件,只要所有这些文件都在相应软件包的 assets_dir 目录下即可。
|
assets_dir
|
assets 中文件的路径的字符串。
assets 和 assets_dir 对描述打包的资产,应同时提供这两个属性,否则一个也不提供。
|
crunch_png
|
|
custom_package
|
|
debug_key
|
警告:请勿使用生产密钥,它们应受到严格保护,且不应保留在源代码树中。 |
debug_signing_keys
|
警告:请勿使用生产密钥,它们应受到严格保护,且不应保留在源代码树中。 |
debug_signing_lineage_file
|
警告:请勿使用生产密钥,它们应受到严格保护,且不应保留在源代码树中。 |
densities
|
|
dex_shards
|
请注意,每个分片都将在最终应用中生成至少一个 dex。因此,不建议将此值设为大于 1 的值。 |
dexopts
|
|
enable_data_binding
|
如需构建具有数据绑定的 Android 应用,您还必须执行以下操作:
|
incremental_dexing
|
|
instruments
|
要插桩的 如果设置了该属性,则此 |
javacopts
|
这些编译器选项在全局编译器选项之后传递给 javac。 |
key_rotation_min_sdk
|
|
main_dex_list
|
android/support/multidex/MultiDex$V19.class android/support/multidex/MultiDex.class android/support/multidex/MultiDexApplication.class com/google/common/base/Objects.class必须与 multidex="manual_main_dex" 搭配使用。
|
main_dex_list_opts
|
|
main_dex_proguard_specs
|
multidex 属性设置为 legacy 时允许。
|
manifest
|
AndroidManifest.xml 。如果定义了 resource_files 或 assets,则必须定义它。
|
manifest_values
|
|
multidex
|
可能的值:
|
nocompress_extensions
|
|
package_id
|
如需了解详情,请参阅 AAPT2 的 |
plugins
|
java_plugin 都将运行。插件生成的资源将包含在目标的结果 jar 中。
|
proguard_apply_dictionary
|
|
proguard_apply_mapping
|
proguard_generate_mapping 生成的映射文件,可重复使用,以将同一映射应用于新 build。
|
proguard_generate_mapping
|
proguard_specs 时,系统才会生成映射文件。此文件将列出原始与经过混淆处理的类、方法和字段名称之间的映射。
警告:如果使用此属性,Proguard 规范不应包含 |
proguard_specs
|
|
resource_configuration_filters
|
en_XA 和/或 ar_XB 伪语言区域。
|
resource_files
|
res 目录下所有文件的 glob 。
生成的文件(来自 genrules)也可以由 Label 在此处引用。唯一的限制是,生成的输出必须与包含的其他资源文件位于同一“ res ”目录下。
|
shrink_resources
|
manifest 和 resource_files 属性)的规则支持此功能,并且需要 ProGuard。其运行方式与 Gradle 资源缩减器 (https://developer.android.com/studio/build/shrink-code.html#shrink-resources) 基本相同。
显著差异:
name_files/resource_shrinker.log ,其中详细说明了执行的分析和删除操作。
可能的值包括:
|
aar_import
aar_import(name, deps, data, aar, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, exports, features, licenses, restricted_to, srcjar, tags, target_compatible_with, testonly, visibility)
此规则允许使用 .aar
文件作为 android_library
和 android_binary
规则的库。
示例
aar_import( name = "google-vr-sdk", aar = "gvr-android-sdk/libraries/sdk-common-1.10.0.aar", ) android_binary( name = "app", manifest = "AndroidManifest.xml", srcs = glob(["**.java"]), deps = [":google-vr-sdk"], )
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
aar
|
.aar 文件。
|
exports
|
|
srcjar
|
|
android_library
android_library(name, deps, srcs, data, assets, assets_dir, compatible_with, custom_package, deprecation, distribs, enable_data_binding, exec_compatible_with, exec_properties, exported_plugins, exports, exports_manifest, features, idl_import_root, idl_parcelables, idl_preprocessed, idl_srcs, javacopts, licenses, manifest, neverlink, plugins, proguard_specs, resource_files, restricted_to, tags, target_compatible_with, testonly, visibility)
此规则会将其源文件编译并归档到 .jar
文件中。Android 运行时库 android.jar
隐式置于编译类路径中。
隐式输出目标
libname.jar
:Java 归档。libname-src.jar
:包含源代码的归档(“源 jar”)。name.aar
:一个 Android“aar”软件包,其中包含此目标的 Java 归档和资源。它不包含传递闭包。
示例
您可以在 Bazel 源代码树的 examples/android
目录中找到 Android 规则示例。
以下示例展示了如何设置 idl_import_root
。让 //java/bazel/helloandroid/BUILD
包含:
android_library( name = "parcelable", srcs = ["MyParcelable.java"], # bazel.helloandroid.MyParcelable # MyParcelable.aidl will be used as import for other .aidl # files that depend on it, but will not be compiled. idl_parcelables = ["MyParcelable.aidl"] # bazel.helloandroid.MyParcelable # We don't need to specify idl_import_root since the aidl file # which declares bazel.helloandroid.MyParcelable # is present at java/bazel/helloandroid/MyParcelable.aidl # underneath a java root (java/). ) android_library( name = "foreign_parcelable", srcs = ["src/android/helloandroid/OtherParcelable.java"], # android.helloandroid.OtherParcelable idl_parcelables = [ "src/android/helloandroid/OtherParcelable.aidl" # android.helloandroid.OtherParcelable ], # We need to specify idl_import_root because the aidl file which # declares android.helloandroid.OtherParcelable is not positioned # at android/helloandroid/OtherParcelable.aidl under a normal java root. # Setting idl_import_root to "src" in //java/bazel/helloandroid # adds java/bazel/helloandroid/src to the list of roots # the aidl compiler will search for imported types. idl_import_root = "src", ) # Here, OtherInterface.aidl has an "import android.helloandroid.CallbackInterface;" statement. android_library( name = "foreign_interface", idl_srcs = [ "src/android/helloandroid/OtherInterface.aidl" # android.helloandroid.OtherInterface "src/android/helloandroid/CallbackInterface.aidl" # android.helloandroid.CallbackInterface ], # As above, idl_srcs which are not correctly positioned under a java root # must have idl_import_root set. Otherwise, OtherInterface (or any other # interface in a library which depends on this one) will not be able # to find CallbackInterface when it is imported. idl_import_root = "src", ) # MyParcelable.aidl is imported by MyInterface.aidl, so the generated # MyInterface.java requires MyParcelable.class at compile time. # Depending on :parcelable ensures that aidl compilation of MyInterface.aidl # specifies the correct import roots and can access MyParcelable.aidl, and # makes MyParcelable.class available to Java compilation of MyInterface.java # as usual. android_library( name = "idl", idl_srcs = ["MyInterface.aidl"], deps = [":parcelable"], ) # Here, ServiceParcelable uses and thus depends on ParcelableService, # when it's compiled, but ParcelableService also uses ServiceParcelable, # which creates a circular dependency. # As a result, these files must be compiled together, in the same android_library. android_library( name = "circular_dependencies", srcs = ["ServiceParcelable.java"], idl_srcs = ["ParcelableService.aidl"], idl_parcelables = ["ServiceParcelable.aidl"], )
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
deps
|
android_library 、带有 android 约束的 java_library ,以及针对 Android 目标平台使用 cc_library 封装或生成 .so 原生库。
|
srcs
|
.java 或 .srcjar 文件的列表。系统会编译 系统会解压缩并编译类型为 如果省略 |
assets
|
assets 目录下所有文件的 glob 。您还可以引用其他软件包中的其他规则(任何生成文件的规则)或导出文件,只要所有这些文件都在相应软件包的 assets_dir 目录下即可。
|
assets_dir
|
assets 中文件的路径的字符串。
assets 和 assets_dir 对描述打包的资产,应同时提供这两个属性,否则一个也不提供。
|
custom_package
|
|
enable_data_binding
|
如需构建具有数据绑定的 Android 应用,您还必须执行以下操作:
|
exported_plugins
|
java_plugin (例如注解处理器)列表。
指定的 |
exports
|
exports 属性到达的所有规则的闭包被视为直接依赖于具有 exports 的目标的任何规则的直接依赖项。
|
exports_manifest
|
android_binary 目标。uses-permissions 属性绝不会导出。
|
idl_import_root
|
在处理依赖于此库的 idl 源文件时,此路径将用作导入根目录。 指定 查看 示例。 |
idl_parcelables
|
android_library 目标的导入项提供(直接或通过其传递闭包),但不会转换为 Java 或编译。应仅包含与此库中的 这些文件必须放置在适当的位置,以便 AI 编译器找到它们。如需了解其含义,请参阅 idl_import_root 的说明。 |
idl_preprocessed
|
android_library 目标的导入项提供(直接或通过其传递闭包),但不会转换为 Java 或编译。只能包含与此库中的 |
idl_srcs
|
srcs 的内容一起编译。
这些文件将作为依赖于此库的任何 这些文件必须放置在适当的位置,以便 AI 编译器找到它们。如需了解其含义,请参阅 idl_import_root 的说明。 |
javacopts
|
这些编译器选项在全局编译器选项之后传递给 javac。 |
manifest
|
AndroidManifest.xml 。如果定义了 resource_files 或 assets,则必须定义它。
|
neverlink
|
neverlink 的规则的输出将不会在创建 .apk 时使用。如果库将在执行期间由运行时环境提供,则此属性非常有用。
|
plugins
|
java_plugin 都将运行。插件生成的资源将包含在目标的结果 jar 中。
|
proguard_specs
|
android_binary 目标中,具体取决于此库。
此处包含的文件只能包含幂等规则,即 -dontnote、-dontwarn、承担任何副作用,以及以 -keep 开头的规则。其他选项只能出现在 android_binary 的 proguard_specs 中,以确保实现非同义合并。
|
resource_files
|
res 目录下所有文件的 glob 。
生成的文件(来自 genrules)也可以由 Label 在此处引用。唯一的限制是,生成的输出必须与包含的其他资源文件位于同一“ res ”目录下。
|
android_instrumentation_test
android_instrumentation_test(name, data, args, compatible_with, deprecation, distribs, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, licenses, local, restricted_to, shard_count, size, support_apks, tags, target_compatible_with, target_device, test_app, testonly, timeout, toolchains, visibility)
android_instrumentation_test
规则用于运行 Android 插桩测试。它将启动模拟器,安装被测试的应用、测试应用和任何其他所需的应用,并运行测试软件包中定义的测试。
test_app 属性指定包含测试的 android_binary
。该 android_binary
进而通过其 instruments 属性指定被测 android_binary
应用。
示例
# java/com/samples/hello_world/BUILD android_library( name = "hello_world_lib", srcs = ["Lib.java"], manifest = "LibraryManifest.xml", resource_files = glob(["res/**"]), ) # The app under test android_binary( name = "hello_world_app", manifest = "AndroidManifest.xml", deps = [":hello_world_lib"], )
# javatests/com/samples/hello_world/BUILD android_library( name = "hello_world_test_lib", srcs = ["Tests.java"], deps = [ "//java/com/samples/hello_world:hello_world_lib", ... # test dependencies such as Espresso and Mockito ], ) # The test app android_binary( name = "hello_world_test_app", instruments = "//java/com/samples/hello_world:hello_world_app", manifest = "AndroidManifest.xml", deps = [":hello_world_test_lib"], ) android_instrumentation_test( name = "hello_world_uiinstrumentation_tests", target_device = ":some_target_device", test_app = ":hello_world_test_app", )
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
support_apks
|
|
target_device
|
应在其中运行测试的 android_device。 如需在已在运行的模拟器上或在实体设备上运行测试,请使用以下参数:
|
test_app
|
android_binary 目标必须通过其 instruments 属性指定要测试的目标。
|
android_local_test
android_local_test(name, deps, srcs, data, args, compatible_with, custom_package, densities, deprecation, enable_data_binding, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, javacopts, jvm_flags, licenses, local, manifest, manifest_values, nocompress_extensions, plugins, resource_configuration_filters, resource_jars, resource_strip_prefix, restricted_to, runtime_deps, shard_count, size, stamp, tags, target_compatible_with, test_class, testonly, timeout, toolchains, use_launcher, visibility)
此规则适用于在本地(而不是在设备上)对 android_library
规则进行单元测试。它与 Android Robolectric 测试框架搭配使用。如需详细了解如何编写 Robolectric 测试,请访问 Android Robolectric 网站。
隐式输出目标
name.jar
:测试的 Java 归档文件。name-src.jar
:包含源代码的归档(“源 jar”)。name_deploy.jar
:适合部署的 Java 部署归档(仅在明确请求时构建)。
示例
如需将 Robolectric 与 android_local_test
搭配使用,请将 Robolectric 的代码库添加到 WORKSPACE
文件中:
http_archive( name = "robolectric", urls = ["https://github.com/robolectric/robolectric/archive/<COMMIT>.tar.gz"], strip_prefix = "robolectric-<COMMIT>", sha256 = "<HASH>", ) load("@robolectric//bazel:robolectric.bzl", "robolectric_repositories") robolectric_repositories()这会拉取 Robolectric 所需的
maven_jar
规则。那么,每条 android_local_test
规则都应依赖于 @robolectric//bazel:robolectric
。请参见下面的示例。
android_local_test( name = "SampleTest", srcs = [ "SampleTest.java", ], manifest = "LibManifest.xml", deps = [ ":sample_test_lib", "@robolectric//bazel:robolectric", ], ) android_library( name = "sample_test_lib", srcs = [ "Lib.java", ], resource_files = glob(["res/**"]), manifest = "AndroidManifest.xml", )
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
deps
|
|
srcs
|
系统会编译 系统会解压缩并编译类型为 只要至少存在一个上述文件类型的文件,所有其他文件都会被忽略。否则会报错。
|
custom_package
|
test_class 。
|
densities
|
|
enable_data_binding
|
|
javacopts
|
这些编译器选项在全局编译器选项之后传递给 javac。 |
jvm_flags
|
Java 二进制文件的封装容器脚本包含 CLASSPATH 定义(以查找所有依赖的 jar),并调用正确的 Java 解释器。封装容器脚本生成的命令行包含主类的名称,后跟 请注意,此属性对 |
manifest
|
AndroidManifest.xml 。如果定义了 resource_files 或 assets,或者被测库中的任何清单包含 minSdkVersion 标记,则必须定义该属性。
|
manifest_values
|
applicationId 、versionCode 、versionName 、minSdkVersion 、targetSdkVersion 和 maxSdkVersion 还会替换清单和 uses-sdk 标记的相应属性。packageName 将被忽略,并且将通过 applicationId (如果已指定)或清单中的软件包进行设置。
无需在规则中包含清单即可使用 manifest_values。
|
nocompress_extensions
|
|
plugins
|
java_plugin 。库还可以从使用 exported_plugins 的依赖项继承插件。插件生成的资源将包含在此规则生成的 jar 中。 |
resource_configuration_filters
|
|
resource_jars
|
|
resource_strip_prefix
|
如果指定,则此路径前缀将从 |
runtime_deps
|
deps 一样,这些变量会显示在运行时类路径上,但与它们不同的是,它们不会出现在编译时类路径上。仅在运行时需要的依赖项应在此处列出。依赖项分析工具应忽略 runtime_deps 和 deps 中都出现的目标。
|
stamp
|
带时间戳的二进制文件不会重新构建,除非其依赖项发生更改。 |
test_class
|
此属性用于指定此测试要运行的 Java 类的名称。很少需要设置该属性。如果省略此参数,将使用名称与此 |
use_launcher
|
如果将此属性设为 false,则对于此目标,系统将忽略 launcher 属性和相关的 |
android_device
android_device(name, cache, compatible_with, default_properties, deprecation, distribs, exec_compatible_with, exec_properties, features, horizontal_resolution, licenses, platform_apks, ram, restricted_to, screen_density, system_image, tags, target_compatible_with, testonly, vertical_resolution, visibility, vm_heap)
此规则会创建一个使用指定规范配置的 Android 模拟器。此模拟器可以通过 bazel 运行命令或直接执行生成的脚本来启动。建议依赖于现有的 android_device 规则,而不是定义自己的规则。
此规则适合用于 --run_under 标志,以便 bazel 测试和 blaze 运行。它会启动模拟器,将要测试/运行的目标复制到模拟器,然后根据需要进行测试或运行。
如果底层 system_image 是基于 X86 并且针对最多 I686 CPU 架构进行了优化,则 android_device
支持创建 KVM 映像。如需使用 KVM,请将 tags = ['requires-kvm']
添加到 android_device
规则中。
隐式输出目标
name_images/userdata.dat
:包含用于启动模拟器的映像文件和快照name_images/emulator-meta-data.pb
:包含传递给模拟器以重启模拟器所需的序列化信息。
示例
以下示例展示了如何使用 android_device。
//java/android/helloandroid/BUILD
包含
android_device( name = "nexus_s", cache = 32, default_properties = "nexus_s.properties", horizontal_resolution = 480, ram = 512, screen_density = 233, system_image = ":emulator_images_android_16_x86", vertical_resolution = 800, vm_heap = 32, ) filegroup( name = "emulator_images_android_16_x86", srcs = glob(["androidsdk/system-images/android-16/**"]), )
//java/android/helloandroid/nexus_s.properties
包含:
ro.product.brand=google ro.product.device=crespo ro.product.manufacturer=samsung ro.product.model=Nexus S ro.product.name=soju
此规则将生成映像和启动脚本。您可以通过执行 bazel run :nexus_s -- --action=start 在本地启动模拟器。该脚本会公开以下标记:
- --adb_port:公开 adb 的端口。如果您希望向模拟器发出 adb 命令,这就是您要发出 adb connect 的端口。
- --emulator_port:用于公开模拟器的 telnet 管理控制台的端口。
- --enable_display:如果为 true,则启动模拟器并显示显示屏(默认为 false)。
- --action:启动或终止。
- --apks_to_install:要在模拟器上安装的 APK 的列表。
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
cache
|
|
default_properties
|
|
horizontal_resolution
|
|
platform_apks
|
|
ram
|
|
screen_density
|
|
system_image
|
|
vertical_resolution
|
|
vm_heap
|
|
android_ndk_repository
android_ndk_repository(name, api_level, path, repo_mapping)
将 Bazel 配置为使用 Android NDK,以支持使用原生代码构建 Android 目标。
请注意,针对 Android 进行构建还需要在 WORKSPACE
文件中添加 android_sdk_repository
规则。
如需了解详情,请参阅 有关将 Android NDK 与 Bazel 搭配使用的完整文档。
示例
android_ndk_repository( name = "androidndk", )
以上示例将从 $ANDROID_NDK_HOME
定位您的 Android NDK,并检测其支持的最高 API 级别。
android_ndk_repository( name = "androidndk", path = "./android-ndk-r20", api_level = 24, )
以上示例将使用位于 ./android-ndk-r20
中的工作区内的 Android NDK。在编译您的 JNI 代码时,它会使用 API 级别 24 的库。
cpufeatures
Android NDK 包含 cpufeatures 库,此库可用于在运行时检测设备的 CPU。以下示例演示了如何将 cpufeatures 与 Bazel 搭配使用。
# jni.cc #include "ndk/sources/android/cpufeatures/cpu-features.h" ...
# BUILD cc_library( name = "jni", srcs = ["jni.cc"], deps = ["@androidndk//:cpufeatures"], )
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
api_level
|
|
path
|
$ANDROID_NDK_HOME 环境变量。
您可以从 Android 开发者网站 下载 Android NDK。 |
repo_mapping
|
例如,条目 |
android_sdk_repository
android_sdk_repository(name, api_level, build_tools_version, path, repo_mapping)
将 Bazel 配置为使用本地 Android SDK 来支持构建 Android 目标。
示例
为 Bazel 设置 Android SDK 的最低要求是在WORKSPACE
文件中添加一条名为“androidsdk”的 android_sdk_repository
规则,并将 $ANDROID_HOME
环境变量设置为 Android SDK 的路径。默认情况下,Bazel 会使用 Android SDK 中安装的最高 Android API 级别和构建工具版本。android_sdk_repository( name = "androidsdk", )
为了确保 build 可重现,您可以将 path
、api_level
和 build_tools_version
属性设为特定值。如果 Android SDK 未安装指定的 API 级别或构建工具版本,构建将会失败。
android_sdk_repository( name = "androidsdk", path = "./sdk", api_level = 19, build_tools_version = "25.0.0", )
以上示例还演示了如何使用指向 Android SDK 的工作区相对路径。如果 Android SDK 是 Bazel 工作区的一部分(例如,如果它签入了版本控制),这会非常有用。
支持库
Android SDK 管理器中以“Android 支持代码库”的形式提供支持库。这是一组带有版本编号的常用 Android 库(例如 Support 库和 AppCompat 库),打包为本地 Maven 制品库。android_sdk_repository
会为每个库生成 Bazel 目标,这些目标可在 android_binary
和 android_library
目标的依赖项中使用。
所生成目标的名称派生自 Android 支持代码库中库的 Maven 坐标,格式为 @androidsdk//${group}:${artifact}-${version}
。以下示例展示了 android_library
如何依赖于 v7 appcompat 库的 25.0.0 版。
android_library( name = "lib", srcs = glob(["*.java"]), manifest = "AndroidManifest.xml", resource_files = glob(["res/**"]), deps = ["@androidsdk//com.android.support:appcompat-v7-25.0.0"], )
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
api_level
|
可用于给定 build 的 API 级别可被 如需查看 |
build_tools_version
|
Bazel 需要使用构建工具 30.0.0 或更高版本。 |
path
|
$ANDROID_HOME 环境变量。
您可以从 Android 开发者网站下载 Android SDK。 |
repo_mapping
|
例如,条目 |