规则
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
:使用调试密钥签名并 经过 zipalign 签名的 Android 应用软件包文件,该文件可用于开发和调试应用。 使用调试密钥签名时,您无法发布应用。name_unsigned.apk
:上述文件的未签名版本,在公开发布之前可以使用发布密钥进行签名。name_deploy.jar
:包含此目标的传递闭包的 Java 归档。部署 jar 包含从头到尾搜索此目标的运行时类路径的类加载器可以找到的所有类。
name_proguard.jar
:一个 Java 归档,其中包含在name_deploy.jar
上运行 ProGuard 的结果。 仅当指定了 proguard_specs 属性时,才会生成此输出。name_proguard.map
:在name_deploy.jar
上运行 ProGuard 的映射文件结果。 仅在指定了 proguard_specs 属性且设置了 proguard_generate_mapping 或 shrink_resources 时,才会生成此输出。
示例
Android 规则的示例可以在 Bazel 源代码树的 examples/android
目录中找到。
参数
属性 | |
---|---|
name |
名称(必需) 此目标的唯一名称。 |
deps
|
标签列表;默认值为 android_library 、具有 android 约束的 java_library ,以及用于 Android 目标平台的 cc_library 封装或生成 .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
|
无论是否使用增量 dexing,强制构建目标,并替换默认值和 --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
|
标签(必需) Android 清单文件的名称,通常为AndroidManifest.xml 。如果定义了 resource_files 或 assets,则必须定义。
|
manifest_values
|
字典:String -> String;默认值为
清单中的任何
当 |
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)生成的文件。唯一的限制是,生成的输出必须与包含的其他资源文件位于同一“ res ”目录下。
|
shrink_resources
|
整数;默认值为 manifest 和 resource_files 属性)的规则支持这种做法,并且需要 ProGuard。
它的运行方式与 Gradle 资源缩减器 (https://developer.android.com/studio/build/shrink-code.html#shrink-resources) 大致相同。显著差异:
name_files/resource_shrinker.log ,详细说明执行的分析和删除操作。
可能的值包括:
|
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 归档文件和资源。不包含传递性闭包。
示例
Android 规则的示例可以在 Bazel 源代码树的 examples/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 或进行编译。
应仅包含与此库中的 这些文件必须放置在适当的位置,以便 AIDL 编译器找到它们。如需了解这意味着什么,请参阅 idl_import_root 的说明。 |
idl_preprocessed
|
标签列表;默认值为 android_library 目标的导入项(直接或通过其传递闭包提供),但不会转换为 Java 或进行编译。
应仅包含与此库中的 |
idl_srcs
|
标签列表;默认值为 srcs 的内容一起编译。这些文件将作为依赖于此库的任何 这些文件必须放置在适当的位置,以便 AIDL 编译器找到它们。如需了解这意味着什么,请参阅 idl_import_root 的说明。 |
javacopts
|
字符串列表;默认值为 这些编译器选项会在全局编译器选项之后传递到 javac。 |
manifest
|
标签;默认值为 AndroidManifest.xml 。如果定义了 resource_files 或 assets,则必须定义。
|
neverlink
|
布尔值;默认值为 neverlink 的规则的输出不会用于创建 .apk 。如果库将在执行期间由运行时环境提供,这会非常有用。
|
plugins
|
标签列表;默认值为 java_plugin 都会运行。插件生成的资源将包含在目标的结果 jar 中。
|
proguard_specs
|
标签列表;默认值为 android_binary 目标。
此处包含的文件必须仅具有幂等规则,即 -dontnote、-dontwarn、shouldnosideeffects 以及以 -keep 开头的规则。其他选项只能出现在 android_binary 的 proguard_specs 中,以确保非同义合并。
|
resource_files
|
标签列表;默认值为 res 目录下所有文件的 glob 。
此处的标签也可以引用(来自 genrules)生成的文件。唯一的限制是,生成的输出必须与包含的其他资源文件位于同一“ res ”目录下。
|
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-bazel/archive/<COMMIT>.tar.gz"], strip_prefix = "robolectric-bazel-<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:android-all", ], ) 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 二进制文件的封装容器脚本包含类路径定义(用于查找所有相关的 JAR),并调用正确的 Java 解释器。封装容器脚本生成的命令行包含主类的名称,后跟 请注意,此属性对 |
manifest
|
标签;默认值为 AndroidManifest.xml 。如果定义了 resource_files 或 assets,或者被测库中的任何清单包含 minSdkVersion 标记,则必须定义。
|
manifest_values
|
字典:String -> String;默认值为 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_ndk_repository
查看规则来源android_ndk_repository(name, api_level, path, repo_mapping)
将 Bazel 配置为使用 Android NDK 以支持使用原生代码构建 Android 目标。
请注意,此 android_ndk_repository
实现将被 Starlark 中的实现所取代。对未来版本的 NDK(包括版本 25 及更高版本)的支持将在 Starlark 版本的 android_ndk_repository
中实现。如需查看 Starlark 版本,请参阅 rules_android_ndk。
请注意,如需针对 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
|
构建所依据的 Android API 级别。如果未指定,将使用安装的最高 API 级别。 |
path
|
字符串;不可配置;默认值为 $ANDROID_NDK_HOME 环境变量。
您可以从 Android 开发者网站 下载 Android NDK。 |
repo_mapping
|
字典:String -> String;默认值为 例如,条目 |
android_sdk_repository
查看规则来源android_sdk_repository(name, api_level, build_tools_version, path, repo_mapping)
将 Bazel 配置为使用本地 Android SDK 来支持构建 Android 目标。
示例
若要设置 Android SDK for Bazel,您至少要在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
会为每个库生成可在 android_binary
和 android_library
目标的依赖项中使用的 Bazel 目标。
所生成目标的名称从 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
|
构建时所依据的 Android API 级别。如果未指定,将使用安装的最高 API 级别。
用于给定 build 的 API 级别可被 如需查看 |
build_tools_version
|
字符串;不可配置;默认值为 Bazel 需要构建工具版本 30.0.0 或更高版本。 |
path
|
字符串;不可配置;默认值为 $ANDROID_HOME 环境变量。
您可以从 Android 开发者网站下载 Android SDK。 |
repo_mapping
|
字典:String -> String;默认值为 例如,条目 |