Skip to main content

android_library

An android_library() rule is used to define a set of Java files that can be compiled together against the Android SDK. The main output of an android_library() rule is a single JAR file containing all of the compiled class files and resources.

Function Signature​

def android_library(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str | target_name_glob] = [],
within_view: list[str | target_name_glob] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_android_toolchain: str = "gh_facebook_buck2_shims_meta//:android",
_apple_platforms: dict[str, str] = {},
_build_only_native_code: bool = select({"prelude//android/constraints:maybe_build_only_native_code[build_only_native_code]": True, "DEFAULT": False}),
_dex_min_sdk_version: None | int = select({"prelude//android/constraints:min_sdk_version_19": 19, "prelude//android/constraints:min_sdk_version_20": 20, "prelude//android/constraints:min_sdk_version_21": 21, "prelude//android/constraints:min_sdk_version_22": 22, "prelude//android/constraints:min_sdk_version_23": 23, "prelude//android/constraints:min_sdk_version_24": 24, "prelude//android/constraints:min_sdk_version_25": 25, "prelude//android/constraints:min_sdk_version_26": 26, "prelude//android/constraints:min_sdk_version_27": 27, "prelude//android/constraints:min_sdk_version_28": 28, "prelude//android/constraints:min_sdk_version_29": 29, "prelude//android/constraints:min_sdk_version_30": 30, "prelude//android/constraints:min_sdk_version_31": 31, "prelude//android/constraints:min_sdk_version_32": 32, "prelude//android/constraints:min_sdk_version_33": 33, "prelude//android/constraints:min_sdk_version_34": 34, "prelude//android/constraints:min_sdk_version_35": 35, "DEFAULT": None}),
_dex_toolchain: str = "gh_facebook_buck2_shims_meta//:dex",
_exec_os_type: str = "prelude//os_lookup/targets:os_lookup",
_is_building_android_binary: bool = select({"prelude//os:building_android_binary": True, "DEFAULT": False}),
_java_toolchain: str = "gh_facebook_buck2_shims_meta//:java_for_android",
_kotlin_toolchain: str = "gh_facebook_buck2_shims_meta//:kotlin_for_android",
abi_generation_mode: None | str = None,
android_optional_jars: None | list[str] = None,
annotation_processor_deps: list[str] = [],
annotation_processor_params: list[str] = [],
annotation_processors: list[str] = [],
attrs_validators: None | list[str] = None,
capabilities_registrations: None | list = None,
contacts: list[str] = [],
deps: list[str] = [],
enable_used_classes: bool = True,
exported_deps: list[str] = [],
exported_provided_deps: list[str] = [],
extra_arguments: list[str] = [],
extra_kotlinc_arguments: list[str] = [],
friend_paths: list[str] = [],
incremental: bool = False,
jar_postprocessor: None | str = None,
java_version: None | str = None,
k2: bool = True,
kotlin_compiler_plugins: list[(str, dict[str, str])] = [],
labels: list[str] = [],
language: None | str = None,
licenses: list[str] = [],
manifest: None | str = None,
manifest_file: None | str = None,
maven_coords: None | str = None,
non_exec_dep_plugins_deprecated: list[str | (str, list[str])] = [],
patches_system_module: bool = False,
plugins: list[str | (str, list[str])] = [],
proguard_config: None | str = None,
provided_deps: list[str] = [],
provided_deps_query: None | str = None,
remove_classes: list[str] = [],
required_for_source_only_abi: bool = False,
resources: list[str] = [],
resources_root: None | str = None,
runtime_deps: list[str] = [],
source: None | str = None,
source_only_abi_deps: list[str] = [],
srcs: list[str] = [],
target: None | str = None,
uses_content_based_paths_for_classic_java: bool = select({"DEFAULT": False}),
uses_content_based_paths_for_kotlincd: bool = True,
validation_deps: list[str] = [],
validation_specs: dict[str, str] = {},
) -> None

Parameters​

  • name: (required)

    name of the target

  • default_target_platform: (defaults to: None)

    specifies the default target platform, used when no platforms are specified on the command line

  • target_compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • exec_compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with an execution platform

  • visibility: (defaults to: [])

    a list of visibility patterns restricting what targets can depend on this one

  • within_view: (defaults to: ["PUBLIC"])

    a list of visibility patterns restricting what this target can depend on

  • metadata: (defaults to: {})

    a key-value map of metadata associated with this target

  • tests: (defaults to: [])

    a list of targets that provide tests for this one

  • modifiers: (defaults to: [])

    an array of modifiers associated with this target

  • abi_generation_mode: (defaults to: None)

    Overrides .buckconfig for this rule.

  • contacts: (defaults to: [])

    A list of organizational contacts for this rule. These could be individuals who you would contact in the event of a failure or other issue with the rule.

    contacts = [ 'Joe Sixpack', 'Erika Mustermann' ]
  • deps: (defaults to: [])

    Rules (usually other android_library rules) that are used to generate the classpath required to compile this android_library.

  • enable_used_classes: (defaults to: True)

    Deprecated: for an experiment only, will be removed

  • exported_deps: (defaults to: [])

    Other rules that depend on this rule will also include its exported_deps in their classpaths. This is useful when the public API of a rule has return types or checked exceptions that are defined in another rule, which would otherwise require callers to add an extra dependency. It's also useful for exposing e.g. a collection of prebuilt_jar rules as a single target for callers to depend on. Targets in exported_deps are implicitly included in the deps of this rule, so they don't need to be repeated there.

  • exported_provided_deps: (defaults to: [])

    This is a combination of provided_deps and exported_deps. Rules listed in this parameter will be added to classpath of rules that depend on this rule, but they will not be included in a binary if binary depends on a such target.

  • extra_arguments: (defaults to: [])

    List of additional arguments to pass into the Java compiler. These arguments follow the ones specified in .buckconfig.

  • extra_kotlinc_arguments: (defaults to: [])

    List of additional arguments to pass into the Kotlin compiler.

  • friend_paths: (defaults to: [])

    Modules whose internal declarations this rule may access, passed to the Kotlin compiler as friend-paths.

    An entry is either a target (//foo/bar:baz) or a build target pattern covering more than one target (//foo/bar/... recursively, or //foo/bar: for a single package).

  • incremental: (defaults to: False)

    Enables Kotlin incremental compilation.

  • k2: (defaults to: True)

    Enables the Kotlin K2 compiler.

  • kotlin_compiler_plugins: (defaults to: [])

    Use this to specify Kotlin compiler plugins to use when compiling this library. This takes a map, with each entry specify one plugin. Entry's key is plugin source path, and value is a map of plugin option key value pair. Unlike extra_kotlinc_arguments, these can be source paths, not just strings.

    A special option value is __codegen_dir__, in which case Buck will provide a default codegen folder's path as option value instead. E.g.

    fbcode/buck2/prelude/decls/jvm_common.bzl
    kotlin_compiler_plugins = {
    "somePluginSourcePath": {
    "plugin:somePluginId:somePluginOptionKey": "somePluginOptionValue",
    "plugin:somePluginId:someDirectoryRelatedOptionKey": "__codegen_dir__",
    },
    },

    Each plugin source path will be prefixed with -Xplugin= and passed as extra arguments to the compiler. Plugin options will be appended after its plugin with -P.

    A specific example is, if you want to use kotlinx.serialization with kotlin_library(), you need to specify kotlinx-serialization-compiler-plugin.jar under kotlin_compiler_plugins and kotlinx-serialization-runtime.jar (which you may have to fetch from Maven) in your deps:

    kotlin_library(
    name = "example",
    srcs = glob(["*.kt"]),
    deps = [
    ":kotlinx-serialization-runtime",
    ],
    kotlin_compiler_plugins = {
    # Likely copied from your $KOTLIN_HOME directory.
    "kotlinx-serialization-compiler-plugin.jar": {},
    },
    )

    prebuilt_jar(
    name = "kotlinx-serialization-runtime",
    binary_jar = ":kotlinx-serialization-runtime-0.10.0",
    )

    # Note you probably want to set
    # maven_repo=http://jcenter.bintray.com/ in your .buckconfig until
    # https://github.com/Kotlin/kotlinx.serialization/issues/64
    # is closed.
    remote_file(
    name = "kotlinx-serialization-runtime-0.10.0",
    out = "kotlinx-serialization-runtime-0.10.0.jar",
    url = "mvn:org.jetbrains.kotlinx:kotlinx-serialization-runtime:jar:0.10.0",
    sha1 = "23d777a5282c1957c7ce35946374fff0adab114c"
    )
  • labels: (defaults to: [])

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query().

  • licenses: (defaults to: [])

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query

  • manifest: (defaults to: None)

    An optional Android Manifest for the to declare any permissions or intents it may need or want to handle. May either be a file or an android_manifest() target.

  • non_exec_dep_plugins_deprecated: (defaults to: [])

    Plugins that do not use the execution platform. This exists for historical reasons, and should not be used. Use plugins instead - plugins should be configured for the execution platform since that is where they are used.

  • patches_system_module: (defaults to: False)

    Allow this target's sources to (re)declare packages that already exist in the --system module image (java.base). Needed under the JDK 9+ module system when compiling stub sources for such packages (e.g. Android's dalvik.system), which otherwise fail with "package exists in another module: java.base". When set, the compiler is invoked with --patch-module java.base=., folding this target's sources into that module. All sources in the compilation are patched in, so a target using this should only contain sources whose packages belong in the system module.

  • plugins: (defaults to: [])

    List of plugins that should be run during compilation of the target. A list of strings may additionally be provided in order to pass additional arguments to the plugin.

  • provided_deps: (defaults to: [])

    These represent dependencies that are known to be provided at run time, but are required in order for the code to compile. Examples of provided_deps include the JEE servlet APIs. When this rule is included in a , the provided_deps will not be packaged into the output.

  • provided_deps_query: (defaults to: None)

    Status: experimental/unstable. The provided deps query functions in the same way as the deps query, but the results of the query are appended to the declared provided deps.

  • remove_classes: (defaults to: [])

    List of classes to remove from the output jar. It only removes classes from the target's own sources, not from any of its dependencies.

  • required_for_source_only_abi: (defaults to: False)

    Indicates that this rule must be present on the classpath during source-only ABI generation of any rule that depends on it. Typically this is done when a rule contains annotations, enums, constants, or interfaces.

    Having rules present on the classpath during source-only ABI generation prevents Buck from completely flattening the build graph, thus reducing the performance win from source-only ABI generation. These rules should be kept small (ideally just containing annotations, constants, enums, and interfaces) and with minimal dependencies of their own.

  • resources: (defaults to: [])

    Static files to include among the compiled .class files. These files can be loaded via Class.getResource().

    Note: Buck uses the src_roots property in .buckconfig to help determine where resources should be placed within the generated JAR file.

  • source: (defaults to: None)

    Specifies the version of Java (as a string) to interpret source files as. Overrides the value in "source_level" in the "java" section of .buckconfig.

  • source_only_abi_deps: (defaults to: [])

    These are dependencies that must be present during source-only ABI generation. Typically such dependencies are added when some property of the code in this rule prevents source-only ABI generation from being correct without these dependencies being present.

    Having source_only_abi_deps prevents Buck from completely flattening the build graph, thus reducing the performance win from source-only ABI generation. They should be avoided when possible. Often only a small code change is needed to avoid them. For more information on such code changes, read about source-only ABI generation.

  • srcs: (defaults to: [])

    The set of .java files to compile for this rule.

  • target: (defaults to: None)

    Specifies the version of Java (as a string) for which to generate code. Overrides the value in "target_level" in the "java" section of .buckconfig.

Examples​

An android_library rule used in concert with an android_resource() rule. This would be a common arrangement for a standard Android Library project as defined by http://developer.android.com/tools/projects/index.html


android_resource(
name = 'res',
res = 'res',
package = 'com.example',
)

android_library(
name = 'my_library',
srcs = glob(['src/**/*.java']),
deps = [
':res',
],
)