Rules
- android_binary
- aar_import
- android_library
- android_instrumentation_test
- android_local_test
- android_device
- android_ndk_repository
- android_sdk_repository
android_binary
View rule sourceopen_in_newImplicit output targets
-
name.apk: An Android application package file signed with debug keys and zipaligned, it could be used to develop and debug your application. You cannot release your application when signed with the debug keys. -
name_unsigned.apk: An unsigned version of the above file that could be signed with the release keys before release to the public. -
name_deploy.jar: A Java archive containing the transitive closure of this target. The deploy jar contains all the classes that would be found by a classloader that searched the runtime classpath of this target from beginning to end. -
name_proguard.jar: A Java archive containing the result of running ProGuard on thename_deploy.jar. This output is only produced if proguard_specs attribute is specified. -
name_proguard.map: A mapping file result of running ProGuard on thename_deploy.jar. This output is only produced if proguard_specs attribute is specified and proguard_generate_mapping or shrink_resources is set.
Examples
Examples of Android rules can be found in theexamples/android directory of the
Bazel source tree.
Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
deps | List of labels; default is [] The list of other libraries to be linked in to the binary target. Permitted library types are: android_library, java_library with android constraint and cc_library wrapping or producing .so native libraries for the Android target platform. |
srcs | List of labels; default is [] The list of source files that are processed to create the target. srcs files of type .java are compiled. For readability’s sake, it is not good to put the name of a generated .java source file into the srcs. Instead, put the depended-on rule name in the srcs, as described below. srcs files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule or build extension.) |
assets | List of labels; default is [] The list of assets to be packaged. This is typically a glob of all files under the assets directory. You can also reference other rules (any rule that produces files) or exported files in the other packages, as long as all those files are under the assets_dir directory in the corresponding package. |
assets_dir | String; default is "" The string giving the path to the files in assets. The pair assets and assets_dir describe packaged assets and either both attributes should be provided or none of them. |
crunch_png | Boolean; default is True Do PNG crunching (or not). This is independent of nine-patch processing, which is always done. This is a deprecated workaround for an aapt bug which has been fixed in aapt2. |
custom_package | String; default is "" Java package for which java sources will be generated. By default the package is inferred from the directory where the BUILD file containing the rule is. You can specify a different package but this is highly discouraged since it can introduce classpath conflicts with other libraries that will only be detected at runtime. |
debug_key | Label; default is "@bazel_tools//tools/android:debug_keystore" File containing the debug keystore to be used to sign the debug apk. Usually you do not want to use a key other than the default key, so this attribute should be omitted. WARNING: Do not use your production keys, they should be strictly safeguarded and not kept in your source tree. |
debug_signing_keys | List of labels; default is [] List of files, debug keystores to be used to sign the debug apk. Usually you do not want to use keys other than the default key, so this attribute should be omitted. WARNING: Do not use your production keys, they should be strictly safeguarded and not kept in your source tree. |
debug_signing_lineage_file | Label; default is None File containing the signing lineage for the debug_signing_keys. Usually you do not want to use keys other than the default key, so this attribute should be omitted. WARNING: Do not use your production keys, they should be strictly safeguarded and not kept in your source tree. |
densities | List of strings; default is [] Densities to filter for when building the apk. This will strip out raster drawable resources that would not be loaded by a device with the specified screen densities, to reduce APK size. A corresponding compatible-screens section will also be added to the manifest if it does not already contain a superset listing. |
dex_shards | Integer; default is 1 Number of shards dexing should be decomposed into. This is makes dexing much faster at the expense of app installation and startup time. The larger the binary, the more shards should be used. 25 is a good value to start experimenting with. Note that each shard will result in at least one dex in the final app. For this reason, setting this to more than 1 is not recommended for release binaries. |
dexopts | List of strings; default is [] Additional command-line flags for the dx tool when generating classes.dex. Subject to “Make variable” substitution and Bourne shell tokenization. |
enable_data_binding | Boolean; default is False If true, this rule processes data binding expressions in layout resources included through the resource_files attribute. Without this setting, data binding expressions produce build failures. To build an Android app with data binding, you must also do the following: 1. Set this attribute for all Android rules that transitively depend on this one. This is because dependers inherit the rule’s data binding expressions through resource merging. So they also need to build with data binding to parse those expressions.- Add a deps = entry for the data binding runtime library to all targets that set this attribute. The location of this library depends on your depot setup. |
incremental_dexing | Integer; nonconfigurable; default is -1 Force the target to be built with or without incremental dexing, overriding defaults and —incremental_dexing flag. |
instruments | Label; default is None The android_binary target to instrument. If this attribute is set, this android_binary will be treated as a test application for instrumentation tests. An android_instrumentation_test target can then specify this target in its test_app attribute. |
javacopts | List of strings; default is [] Extra compiler options for this target. Subject to “Make variable” substitution and Bourne shell tokenization. These compiler options are passed to javac after the global compiler options. |
key_rotation_min_sdk | String; default is "" Sets the minimum Android platform version (API Level) for which an APK’s rotated signing key should be used to produce the APK’s signature. The original signing key for the APK will be used for all previous platform versions. |
main_dex_list | Label; default is None A text file contains a list of class file names. Classes defined by those class files are put in the primary classes.dex. e.g.: android/support/multidex/MultiDex$V19.class android/support/multidex/MultiDex.class android/support/multidex/MultiDexApplication.class com/google/common/base/Objects.class Must be used with multidex="manual_main_dex". |
main_dex_list_opts | List of strings; default is [] Command line options to pass to the main dex list builder. Use this option to affect the classes included in the main dex list. |
main_dex_proguard_specs | List of labels; default is [] Files to be used as the Proguard specifications to determine classes that must be kept in the main dex. Only allowed if the multidex attribute is set to legacy. |
manifest | Label; required The name of the Android manifest file, normally AndroidManifest.xml. Must be defined if resource_files or assets are defined. |
manifest_values | Dictionary: String -> String; default is {} A dictionary of values to be overridden in the manifest. Any instance of ${name} in the manifest will be replaced with the value corresponding to name in this dictionary. applicationId, versionCode, versionName, minSdkVersion, targetSdkVersion and maxSdkVersion will also override the corresponding attributes in the manifest and uses-sdk tags. packageName will be ignored and will be set from either applicationId if specified or the package in manifest. When manifest_merger is set to legacy, only applicationId, versionCode and versionName will have any effect. |
multidex | String; default is "native" Whether to split code into multiple dex files. Possible values: * native: Split code into multiple dex files when the dex 64K index limit is exceeded. Assumes native platform support for loading multidex classes at runtime. This works with only Android L and newer. * legacy: Split code into multiple dex files when the dex 64K index limit is exceeded. Assumes multidex classes are loaded through application code (i.e. no native platform support). * manual_main_dex: Split code into multiple dex files when the dex 64K index limit is exceeded. The content of the main dex file needs to be specified by providing a list of classes in a text file using the main_dex_list attribute. * off: Compile all code to a single dex file, even if it exceeds the index limit. |
nocompress_extensions | List of strings; default is [] A list of file extension to leave uncompressed in apk. |
package_id | Integer; default is 0 Package ID to be assigned to resources in this binary. See AAPT2’s --package-id argument for more information. This can (and should) typically be left unset, resulting in the default value of 127 (0x7F). |
plugins | List of labels; default is [] Java compiler plugins to run at compile-time. Every java_plugin specified in the plugins attribute will be run whenever this target is built. Resources generated by the plugin will be included in the result jar of the target. |
proguard_apply_dictionary | Label; default is None File to be used as a mapping for proguard. A line separated file of “words” to pull from when renaming classes and members during obfuscation. |
proguard_apply_mapping | Label; default is None File to be used as a mapping for proguard. A mapping file generated by proguard_generate_mapping to be re-used to apply the same mapping to a new build. |
proguard_generate_mapping | Boolean; nonconfigurable; default is False Whether to generate Proguard mapping file. The mapping file will be generated only if proguard_specs is specified. This file will list the mapping between the original and obfuscated class, method, and field names. WARNING: If this attribute is used, the Proguard specification should contain neither -dontobfuscate nor -printmapping. |
proguard_specs | List of labels; default is [] Files to be used as Proguard specification. This file will describe the set of specifications to be used by Proguard. |
resource_configuration_filters | List of strings; default is [] A list of resource configuration filters, such ‘en’ that will limit the resources in the apk to only the ones in the ‘en’ configuration. To enable pseudolocalization, include the en_XA and/or ar_XB pseudo-locales. |
resource_files | List of labels; default is [] The list of resources to be packaged. This is typically a glob of all files under the res directory. Generated files (from genrules) can be referenced by Label here as well. The only restriction is that the generated outputs must be under the same “res” directory as any other resource files that are included. |
shrink_resources | Integer; default is -1 Whether to perform resource shrinking. Resources that are not used by the binary will be removed from the APK. This is only supported for rules using local resources (i.e. the manifest and resource_files attributes) and requires ProGuard. It operates in mostly the same manner as the Gradle resource shrinker (https://developer.android.com/studio/build/shrink-code.html#shrink-resources). Notable differences: * resources in values/ will be removed as well as file based resources * uses strict mode by default * removing unused ID resources is only supported with aapt2 If resource shrinking is enabled, name_files/resource_shrinker.log will also be generated, detailing the analysis and deletions performed. Possible values: * shrink_resources = 1: Turns on Android resource shrinking * shrink_resources = 0: Turns off Android resource shrinking * shrink_resources = -1: Shrinking is controlled by the —android_resource_shrinking flag. |
aar_import
View rule sourceopen_in_new.aar files as libraries for
android_library and
android_binary rules.
Examples
Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
aar | Label; required The .aar file to provide to the Android targets that depend on this target. |
exports | List of labels; default is [] Targets to export to rules that depend on this rule. See java_library.exports. |
srcjar | Label; default is None A JAR file that contains source code for the compiled JAR files in the AAR. |
android_library
View rule sourceopen_in_new.jar file.
The Android runtime library android.jar is implicitly put on
the compilation class path.
Implicit output targets
libname.jar: A Java archive.libname-src.jar: An archive containing the sources (“source jar”).name.aar: An android ‘aar’ bundle containing the java archive and resources of this target. It does not contain the transitive closure.
Examples
Examples of Android rules can be found in theexamples/android directory of the
Bazel source tree.
The following example shows
how to set idl_import_root.
Let //java/bazel/helloandroid/BUILD contain:
Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
deps | List of labels; default is [] The list of other libraries to link against. Permitted library types are: android_library, java_library with android constraint and cc_library wrapping or producing .so native libraries for the Android target platform. |
srcs | List of labels; default is [] The list of .java or .srcjar files that are processed to create the target. srcs files of type .java are compiled. For readability’s sake, it is not good to put the name of a generated .java source file into the srcs. Instead, put the depended-on rule name in the srcs, as described below. srcs files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule or build extension.) If srcs is omitted, then any dependency specified in deps is exported from this rule (see java_library’s exports for more information about exporting dependencies). However, this behavior will be deprecated soon; try not to rely on it. |
assets | List of labels; default is [] The list of assets to be packaged. This is typically a glob of all files under the assets directory. You can also reference other rules (any rule that produces files) or exported files in the other packages, as long as all those files are under the assets_dir directory in the corresponding package. |
assets_dir | String; default is "" The string giving the path to the files in assets. The pair assets and assets_dir describe packaged assets and either both attributes should be provided or none of them. |
custom_package | String; default is "" Java package for which java sources will be generated. By default the package is inferred from the directory where the BUILD file containing the rule is. You can specify a different package but this is highly discouraged since it can introduce classpath conflicts with other libraries that will only be detected at runtime. |
enable_data_binding | Boolean; default is False If true, this rule processes data binding expressions in layout resources included through the resource_files attribute. Without this setting, data binding expressions produce build failures. To build an Android app with data binding, you must also do the following: 1. Set this attribute for all Android rules that transitively depend on this one. This is because dependers inherit the rule’s data binding expressions through resource merging. So they also need to build with data binding to parse those expressions.- Add a deps = entry for the data binding runtime library to all targets that set this attribute. The location of this library depends on your depot setup. |
exported_plugins | List of labels; default is [] The list of java_plugins (e.g. annotation processors) to export to libraries that directly depend on this library. The specified list of java_plugins will be applied to any library which directly depends on this library, just as if that library had explicitly declared these labels in plugins. |
exports | List of labels; default is [] The closure of all rules reached via exports attributes are considered direct dependencies of any rule that directly depends on the target with exports. The exports are not direct deps of the rule they belong to. |
exports_manifest | Integer; default is 1 Whether to export manifest entries to android_binary targets that depend on this target. uses-permissions attributes are never exported. |
idl_import_root | String; default is "" Package-relative path to the root of the java package tree containing idl sources included in this library. This path will be used as the import root when processing idl sources that depend on this library. When idl_import_root is specified, both idl_parcelables and idl_srcs must be at the path specified by the java package of the object they represent under idl_import_root. When idl_import_root is not specified, both idl_parcelables and idl_srcs must be at the path specified by their package under a Java root. See examples. |
idl_parcelables | List of labels; default is [] List of Android IDL definitions to supply as imports. These files will be made available as imports for any android_library target that depends on this library, directly or via its transitive closure, but will not be translated to Java or compiled. Only .aidl files that correspond directly to .java sources in this library should be included (e.g., custom implementations of Parcelable), otherwise idl_srcs should be used. These files must be placed appropriately for the aidl compiler to find them. See the description of idl_import_root for information about what this means. |
idl_preprocessed | List of labels; default is [] List of preprocessed Android IDL definitions to supply as imports. These files will be made available as imports for any android_library target that depends on this library, directly or via its transitive closure, but will not be translated to Java or compiled. Only preprocessed .aidl files that correspond directly to .java sources in this library should be included (e.g., custom implementations of Parcelable), otherwise use idl_srcs for Android IDL definitions that need to be translated to Java interfaces and use idl_parcelable for non-preprocessed AIDL files. |
idl_srcs | List of labels; default is [] List of Android IDL definitions to translate to Java interfaces. After the Java interfaces are generated, they will be compiled together with the contents of srcs. These files will be made available as imports for any android_library target that depends on this library, directly or via its transitive closure. These files must be placed appropriately for the aidl compiler to find them. See the description of idl_import_root for information about what this means. |
javacopts | List of strings; default is [] Extra compiler options for this target. Subject to “Make variable” substitution and Bourne shell tokenization. These compiler options are passed to javac after the global compiler options. |
manifest | Label; default is None The name of the Android manifest file, normally AndroidManifest.xml. Must be defined if resource_files or assets are defined. |
neverlink | Boolean; default is False Only use this library for compilation and not at runtime. The outputs of a rule marked as neverlink will not be used in .apk creation. Useful if the library will be provided by the runtime environment during execution. |
plugins | List of labels; default is [] Java compiler plugins to run at compile-time. Every java_plugin specified in the plugins attribute will be run whenever this target is built. Resources generated by the plugin will be included in the result jar of the target. |
proguard_specs | List of labels; default is [] Files to be used as Proguard specification. These will describe the set of specifications to be used by Proguard. If specified, they will be added to any android_binary target depending on this library. The files included here must only have idempotent rules, namely -dontnote, -dontwarn, assumenosideeffects, and rules that start with -keep. Other options can only appear in android_binary’s proguard_specs, to ensure non-tautological merges. |
resource_files | List of labels; default is [] The list of resources to be packaged. This is typically a glob of all files under the res directory. Generated files (from genrules) can be referenced by Label here as well. The only restriction is that the generated outputs must be under the same “res” directory as any other resource files that are included. |
android_instrumentation_test
View rule sourceopen_in_newandroid_instrumentation_test rule runs Android instrumentation tests. It will
start an emulator, install the application being tested, the test application, and
any other needed applications, and run the tests defined in the test package.
The test_app attribute specifies the
android_binary which contains the test. This android_binary in turn
specifies the android_binary application under test through its
instruments attribute.
Example
Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
support_apks | List of labels; default is [] Other APKs to install on the device before the instrumentation test starts. |
target_device | Label; required The android_device the test should run on. To run the test on an emulator that is already running or on a physical device, use these arguments: --test_output=streamed --test_arg=--device_broker_type=LOCAL_ADB_SERVER --test_arg=--device_serial_number=$device_identifier |
test_app | Label; required The android_binary target containing the test classes. The android_binary target must specify which target it is testing through its instruments attribute. |
android_local_test
View rule sourceopen_in_newandroid_library rules locally
(as opposed to on a device).
It works with the Android Robolectric testing framework.
See the Android Robolectric site for details about
writing Robolectric tests.
Implicit output targets
name.jar: A Java archive of the test.name-src.jar: An archive containing the sources (“source jar”).name_deploy.jar: A Java deploy archive suitable for deployment (only built if explicitly requested).
Examples
To use Robolectric withandroid_local_test, add
Robolectric’s
repository to your WORKSPACE file:
maven_jar rules needed for Robolectric.
Then each android_local_test rule should depend on
@robolectric//bazel:robolectric. See example below.
Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
deps | List of labels; default is [] The list of libraries to be tested as well as additional libraries to be linked in to the target. All resources, assets and manifest files declared in Android rules in the transitive closure of this attribute are made available in the test. The list of allowed rules in deps are android_library, aar_import, java_import, java_library, and java_lite_proto_library. |
srcs | List of labels; default is [] The list of source files that are processed to create the target. Required except in special case described below. srcs files of type .java are compiled. For readability’s sake, it is not good to put the name of a generated .java source file into the srcs. Instead, put the depended-on rule name in the srcs, as described below. srcs files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule or build extension.) All other files are ignored, as long as there is at least one file of a file type described above. Otherwise an error is raised. The srcs attribute is required and cannot be empty, unless runtime_deps is specified. |
custom_package | String; default is "" Java package in which the R class will be generated. By default the package is inferred from the directory where the BUILD file containing the rule is. If you use this attribute, you will likely need to use test_class as well. |
densities | List of strings; default is [] Densities to filter for when building the apk. A corresponding compatible-screens section will also be added to the manifest if it does not already contain a superset StarlarkListing. |
enable_data_binding | Boolean; default is False If true, this rule processes data binding references used in data-binding enabled dependencies used by this test. Without this setting, data-binding dependencies won’t have necessary binary-level code generation, and may produce build failures. |
javacopts | List of strings; default is [] Extra compiler options for this library. Subject to “Make variable” substitution and Bourne shell tokenization. These compiler options are passed to javac after the global compiler options. |
jvm_flags | List of strings; default is [] A list of flags to embed in the wrapper script generated for running this binary. Subject to [(location)](/versions/7.7.1/reference/be/make-variables#location) and ["Make variable"](make-variables) substitution, and [Bourne shell tokenization](common-definitions#sh-tokenization). The wrapper script for a Java binary includes a CLASSPATH definition (to find all the dependent jars) and invokes the right Java interpreter. The command line generated by the wrapper script includes the name of the main class followed by a `"@“so you can pass along other arguments after the classname. However, arguments intended for parsing by the JVM must be specified *before* the classname on the command line. The contents ofjvm_flagsare added to the wrapper script before the classname is listed. Note that this attribute has *no effect* on*_deploy.jar` outputs. |
manifest | Label; default is None The name of the Android manifest file, normally AndroidManifest.xml. Must be defined if resource_files or assets are defined or if any of the manifests from the libraries under test have a minSdkVersion tag in them. |
manifest_values | Dictionary: String -> String; default is {} A dictionary of values to be overridden in the manifest. Any instance of $ in the manifest will be replaced with the value corresponding to name in this dictionary. applicationId, versionCode, versionName, minSdkVersion, targetSdkVersion and maxSdkVersion will also override the corresponding attributes of the manifest and uses-sdk tags. packageName will be ignored and will be set from either applicationId if specified or the package in the manifest. It is not necessary to have a manifest on the rule in order to use manifest_values. |
nocompress_extensions | List of strings; default is [] A list of file extensions to leave uncompressed in the resource apk. |
plugins | List of labels; default is [] Java compiler plugins to run at compile-time. Every java_plugin specified in this attribute will be run whenever this rule is built. A library may also inherit plugins from dependencies that use exported_plugins. Resources generated by the plugin will be included in the resulting jar of this rule. |
resource_configuration_filters | List of strings; default is [] A list of resource configuration filters, such as ‘en’ that will limit the resources in the apk to only the ones in the ‘en’ configuration. |
resource_jars | List of labels; default is [] Deprecated: Use java_import and deps or runtime_deps instead. |
resource_strip_prefix | String; default is "" The path prefix to strip from Java resources. If specified, this path prefix is stripped from every file in the resources attribute. It is an error for a resource file not to be under this directory. If not specified (the default), the path of resource file is determined according to the same logic as the Java package of source files. For example, a source file at stuff/java/foo/bar/a.txt will be located at foo/bar/a.txt. |
runtime_deps | List of labels; default is [] Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. Dependencies needed only at runtime should be listed here. Dependency-analysis tools should ignore targets that appear in both runtime_deps and deps. |
stamp | Integer; default is 0 Whether to encode build information into the binary. Possible values: * stamp = 1: Always stamp the build information into the binary, even in --nostamp builds. This setting should be avoided, since it potentially kills remote caching for the binary and any downstream actions that depend on it. * stamp = 0: Always replace build information by constant values. This gives good build result caching. * stamp = -1: Embedding of build information is controlled by the --[no]stamp flag. Stamped binaries are not rebuilt unless their dependencies change. |
test_class | String; default is "" The Java class to be loaded by the test runner. This attribute specifies the name of a Java class to be run by this test. It is rare to need to set this. If this argument is omitted, the Java class whose name corresponds to the name of this android_local_test rule will be used. The test class needs to be annotated with org.junit.runner.RunWith. |
use_launcher | Boolean; default is True Whether the binary should use a custom launcher. If this attribute is set to false, the launcher attribute and the related --java_launcher flag will be ignored for this target. |
android_device
View rule sourceopen_in_newandroid_device supports creating KVM images if the underlying
system_image is X86 based and is
optimized for at most the I686 CPU architecture. To use KVM add
tags = ['requires-kvm'] to the android_device rule.
Implicit output targets
name_images/userdata.dat: Contains image files and snapshots to start the emulatorname_images/emulator-meta-data.pb: Contains serialized information necessary to pass on to the emulator to restart it.
Examples
The following example shows how to use android_device.//java/android/helloandroid/BUILD contains
//java/android/helloandroid/nexus_s.properties contains:
- —adb_port: The port to expose adb on. If you wish to issue adb commands to the emulator this is the port you will issue adb connect to.
- —emulator_port: The port to expose the emulator’s telnet management console on.
- —enable_display: Starts the emulator with a display if true (defaults to false).
- —action: Either start or kill.
- —apks_to_install: a list of apks to install on the emulator.
Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
cache | Integer; required The size in megabytes of the emulator’s cache partition. The minimum value of this is 16 megabytes. |
default_properties | Label; default is None A single properties file to be placed in /default.prop on the emulator. This allows the rule author to further configure the emulator to appear more like a real device (In particular controlling its UserAgent strings and other behaviour that might cause an application or server to behave differently to a specific device). The properties in this file will override read only properties typically set by the emulator such as ro.product.model. |
horizontal_resolution | Integer; required The horizontal screen resolution in pixels to emulate. The minimum value is 240. |
platform_apks | List of labels; default is [] A list of apks to be installed on the device at boot time. |
ram | Integer; required The amount of ram in megabytes to emulate for the device. This is for the entire device, not just for a particular app installed on the device. The minimum value is 64 megabytes. |
screen_density | Integer; required The density of the emulated screen in pixels per inch. The minimum value of this is 30 ppi. |
system_image | Label; required A filegroup containing the following files: * system.img: The system partition * kernel-qemu: The Linux kernel the emulator will load * ramdisk.img: The initrd image to use at boot time * userdata.img: The initial userdata partition * source.properties: A properties file containing information about the images These files are part of the android sdk or provided by third parties (for example Intel provides x86 images). |
vertical_resolution | Integer; required The vertical screen resolution in pixels to emulate. The minimum value is 240. |
vm_heap | Integer; required The size in megabytes of the virtual machine heap Android will use for each process. The minimum value is 16 megabytes. |
android_ndk_repository
View rule sourceopen_in_newandroid_ndk_repository is being replaced by an
implementation in Starlark. Support for future versions of the NDK including version 25 and up will
be implemented in the Starlark version of android_ndk_repository. See
rules_android_ndk for the Starlark
version.
Note that building for Android also requires an android_sdk_repository rule in your
WORKSPACE file.
For more information, read the full documentation on using Android NDK with Bazel.
Examples
$ANDROID_NDK_HOME and detect
the highest API level that it supports.
./android-ndk-r20. It will use the API level 24 libraries when compiling your JNI
code.
cpufeatures
The Android NDK contains the cpufeatures library which can be used to detect a device’s CPU at runtime. The following example demonstrates how to use cpufeatures with Bazel.Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
api_level | Integer; nonconfigurable; default is 0 The Android API level to build against. If not specified, the highest API level installed will be used. |
path | String; nonconfigurable; default is "" An absolute or relative path to an Android NDK. Either this attribute or the $ANDROID_NDK_HOME environment variable must be set. The Android NDK can be downloaded from the Android developer site. |
repo_mapping | Dictionary: String -> String; default is {} A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository. For example, an entry "@foo": "@bar" declares that, for any time this repository depends on "@foo" (such as a dependency on "@foo//some:target"), it should actually resolve that dependency within globally-declared "@bar" ("@bar//some:target"). |
android_sdk_repository
View rule sourceopen_in_newExamples
The minimum to set up an Android SDK for Bazel is to put anandroid_sdk_repository rule
named “androidsdk” in your WORKSPACE file and set the $ANDROID_HOME
environment variable to the path of your Android SDK. Bazel will use the highest Android API level
and build tools version installed in the Android SDK by default.
path, api_level and
build_tools_version attributes can be set to specific values. The build will fail if
the Android SDK does not have the specified API level or build tools version installed.
Support Libraries
The Support Libraries are available in the Android SDK Manager as “Android Support Repository”. This is a versioned set of common Android libraries, such as the Support and AppCompat libraries, that is packaged as a local Maven repository.android_sdk_repository generates Bazel
targets for each of these libraries that can be used in the dependencies of
android_binary and android_library targets.
The names of the generated targets are derived from the Maven coordinates of the libraries in the
Android Support Repository, formatted as @androidsdk//${group}:${artifact}-${version}.
The following example shows how an android_library can depend on version 25.0.0 of the
v7 appcompat library.
Arguments
| Attributes | |
|---|---|
name | Name; required A unique name for this target. |
api_level | Integer; nonconfigurable; default is 0 The Android API level to build against by default. If not specified, the highest API level installed will be used. The API level used for a given build can be overridden by the android_sdk flag. android_sdk_repository creates an android_sdk target for each API level installed in the SDK with name @androidsdk//:sdk-${level}, whether or not this attribute is specified. For example, to build against a non-default API level: bazel build --android_sdk=@androidsdk//:sdk-19 //java/com/example:app. To view all android_sdk targets generated by android_sdk_repository, you can run bazel query "kind(android_sdk, @androidsdk//...)". |
build_tools_version | String; nonconfigurable; default is "" The version of the Android build tools to use from within the Android SDK. If not specified, the latest build tools version installed will be used. Bazel requires build tools version 30.0.0 or later. |
path | String; nonconfigurable; default is "" An absolute or relative path to an Android SDK. Either this attribute or the $ANDROID_HOME environment variable must be set. The Android SDK can be downloaded from the Android developer site. |
repo_mapping | Dictionary: String -> String; default is {} A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository. For example, an entry "@foo": "@bar" declares that, for any time this repository depends on "@foo" (such as a dependency on "@foo//some:target"), it should actually resolve that dependency within globally-declared "@bar" ("@bar//some:target"). |