规则
- <ph type="x-smartling-placeholder"></ph> android_binary
 - <ph type="x-smartling-placeholder"></ph> aar_import
 - <ph type="x-smartling-placeholder"></ph> android_library
 - <ph type="x-smartling-placeholder"></ph> android_instrumentation_test
 - <ph type="x-smartling-placeholder"></ph> android_local_test
 - <ph type="x-smartling-placeholder"></ph> android_device
 - <ph type="x-smartling-placeholder"></ph> android_ndk_repository
 - <ph type="x-smartling-placeholder"></ph> android_sdk_repository
 
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 应用 使用调试密钥签名的软件包文件 <ph type="x-smartling-placeholder"></ph> zipaligned,就可用于开发和调试应用。 使用调试密钥签名时,您无法发布应用。name_unsigned.apk: 上面可以使用发布密钥签名的 。name_deploy.jar:包含 传递关闭此目标。这个部署 jar 中包含了 搜索此目标的运行时类路径的类加载器 自始至终。
name_proguard.jar:Java 归档,其中包含 在name_deploy.jar。 只有在以下情况下才会生成此输出: proguard_specs 属性均为 。name_proguard.map:映射文件结果 在name_deploy.jar上运行 ProGuard。 只有在以下情况下才会生成此输出: proguard_specs 属性均为 且 proguard_generate_mapping 或者设置了 shrink_resources 时。
示例
Android 规则的示例可在 examples/android 目录
Bazel 源代码树。
  
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          deps
         | 
        
                     
 android_library、
          具有 android 约束的 java_library 以及
          cc_library 会封装或生成 .so 原生库,
          Android 目标平台。
         | 
      
          srcs
         | 
        
                     
 系统会编译  系统会将   | 
      
          assets
         | 
        
                     
 glob 下所有文件的
          assets 目录中。您也可以引用其他规则(生成
          文件)或导出的文件,前提是所有这些文件都位于
          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
         | 
        
                     
 可能的值: <ph type="x-smartling-placeholder"> 
  | 
      
          nocompress_extensions
         | 
        
                     
  | 
      
          package_id
         | 
        
                     
 如需了解详情,请参阅 AAPT2 的   | 
      
          plugins
         | 
        
                     
 java_plugin:
          插件属性会在
          这个目标就创建好了生成的资源
          该插件将会包含在
          目标。
         | 
      
          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
         | 
        
                     
 glob 下所有文件的
          res 目录中。
          生成的文件(来自 genrule)可通过 还要在此处添加标签。唯一的限制是 生成的输出必须在同一“ 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 归档的 bundle 此目标的资源和资源。不包含传递性闭包。
示例
Android 规则的示例可在 examples/android 目录
Bazel 源代码树。
以下示例展示了
如何设置 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 以及
        cc_library 封装或生成 .so 原生库
        针对 Android 目标平台。
         | 
      
          srcs
         | 
        
                     
 .java 或 .srcjar 文件的列表,
         以创建目标。
        系统会编译  系统会将  如果省略   | 
      
          assets
         | 
        
                     
 glob 下所有文件的
          assets 目录中。您也可以引用其他规则(生成
          文件)或导出的文件,前提是所有这些文件都位于
          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:
          插件属性会在
          这个目标就创建好了生成的资源
          该插件将会包含在
          目标。
         | 
      
          proguard_specs
         | 
        
                     
 android_binary 目标中。
        此处包含的文件只能具有幂等规则,即 -dontnote、-dontwarn、
        以及以 -keep 开头的规则。其他选项只能显示在
        android_binary 的 proguard_specs,旨在确保非同义合并。
         | 
      
          resource_files
         | 
        
                     
 glob 下所有文件的
          res 目录中。
          生成的文件(来自 genrule)可通过 还要在此处添加标签。唯一的限制是 生成的输出必须在同一“ 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
  通过其android_binary
  instruments 属性。
示例
# 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 测试框架配合使用。
请访问 Android Robolectric 网站,详细了解
主要负责编写 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()
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 二进制文件的封装容器脚本包含类路径定义
          (查找所有相关的 jar)并调用正确的 Java 解释器。
          由封装容器脚本生成的命令行包含
          主类,后跟  请注意,此属性对   | 
      
          manifest
         | 
        
                     
 AndroidManifest.xml。
        如果定义了 resource_files 或 assets 或者
        被测库中包含 minSdkVersion 标记。
         | 
      
          manifest_values
         | 
        
                     
 applicationId、versionCode、versionName、
        minSdkVersion、targetSdkVersion和
        maxSdkVersion 还会覆盖相应的属性
        和
        use-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 类的名称
        此测试。很少需要设置此属性。如果省略此参数,则 Java 类
        其名称对应于该 API 的  | 
      
          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 运行。它会启动模拟器,将要测试的目标/运行的目标复制到模拟器; 并根据需要进行测试或运行
android_device 支持创建 KVM 映像,前提是底层
  system_image 基于 X86,
  针对最多 I686 CPU 架构进行了优化。如需使用 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
此规则将生成图片和启动脚本。您可以启动模拟器了 。该脚本会公开 以下标志:
- --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 进行构建还需要在您的android_sdk_repository
WORKSPACE 文件。
有关详情,请参阅 将 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。它会在编译 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 NDK: Android 开发者网站 。  | 
      
          repo_mapping
         | 
        
                     
 例如,条目   | 
      
android_sdk_repository
android_sdk_repository(name, api_level, build_tools_version, path, repo_mapping)
将 Bazel 配置为使用本地 Android SDK 来支持构建 Android 目标。
示例
若要设置 Android SDK for Bazel,至少要设置一个android_sdk_repository 规则
名为“androidsdk”位于 WORKSPACE 文件中,并将 $ANDROID_HOME
环境变量添加到 Android SDK 的路径中。Bazel 将使用最高 Android API 级别
Android SDK 中默认安装的 Build Tools 和 Build Tools 版本。
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 工作区的一部分(例如,如果它已签入到版本)中,则该 SDK 版本会很有用 控制)。
支持库
支持库在 Android SDK 管理器中可作为“Android 支持存储库”提供。
这是一组带版本号的常见 Android 库,例如 Support 库和 AppCompat 库,
打包为本地 Maven 制品库。android_sdk_repository 生成 Bazel
可以用在 API 的依赖项中,
目标为 android_binary 和 android_library。
所生成目标的名称根据
Android 支持代码库,格式为 @androidsdk//${group}:${artifact}-${version}。
以下示例展示了 android_library 如何依赖于
v7 appcompat 库。
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 SDK: Android 开发者网站。  | 
      
          repo_mapping
         | 
        
                     
 例如,条目   |