Skip to main content

kotlin_library

A kotlin_library() rule is used to define a set of Kotlin files that can be compiled together. The main output of a kotlin_library() rule is a single JAR file containing all of the compiled class files, as well as the static files specified in the resources argument.

Function Signature

def kotlin_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] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
_build_only_native_code: bool = select({"prelude//android/constraints: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",
_kotlin_toolchain: str = "gh_facebook_buck2_shims_meta//:kotlin",
abi_generation_mode: None | str = None,
annotation_processing_tool: None | str = None,
annotation_processor_deps: list[str] = [],
annotation_processor_params: list[str] = [],
annotation_processors: list[str] = [],
attrs_validators: None | list[str] = None,
contacts: list[str] = [],
default_host_platform: None | str = None,
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,
javac: None | str = None,
k2: bool = False,
keep_synthetics_in_class_abi: None | bool = None,
kotlin_compiler_plugins: list[(str, dict[str, str])] = [],
labels: list[str] = [],
licenses: list[str] = [],
manifest_file: None | str = None,
maven_coords: None | str = None,
never_mark_as_unused_dependency: None | bool = None,
no_x_jdk_release: bool = False,
non_exec_dep_plugins_deprecated: list[str | (str, list[str])] = [],
on_unused_dependencies: None | str = None,
plugins: list[str | (str, list[str])] = [],
proguard_config: None | str = None,
provided_deps: list[str] = [],
remove_classes: list[str] = [],
required_for_source_only_abi: bool = False,
resources: list[str] = [],
resources_root: None | str = None,
runtime_deps: list[str] = [],
should_kosabi_jvm_abi_gen_use_k2: None | bool = None,
source: None | str = None,
source_abi_verification_mode: None | str = None,
source_only_abi_deps: list[str] = [],
srcs: list[str] = [],
target: None | str = None,
use_jvm_abi_gen: None | bool = None,
validation_deps: list[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

  • annotation_processing_tool: (defaults to: None)

    Specifies the tool to use for annotation processing. Possible values: "kapt" or "javac". "kapt" allows running Java annotation processors against Kotlin sources while backporting it for Java sources too. "javac" works only against Java sources, Kotlin sources won't have access to generated classes at compile time.

  • deps: (defaults to: [])

    Rules (usually other kotlin_library rules) that are used to generate the classpath required to compile this kotlin_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_kotlinc_arguments: (defaults to: [])

    List of additional arguments to pass into the Kotlin compiler.

  • friend_paths: (defaults to: [])

    List of source paths to pass into the Kotlin compiler as friend-paths, that is, modules you can have access to internal methods.

  • incremental: (defaults to: False)

    Enables Kotlin incremental compilation.

  • javac: (defaults to: None)

    Specifies the Java compiler program to use for this rule. The value is a source path or an execution dep (e.g., //foo/bar:bar). Overrides the value in "javac" in the "tools" section of .buckconfig.

  • k2: (defaults to: False)

    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() .

  • no_x_jdk_release: (defaults to: False)

    By default, classic kotlin adds -Xjdk-release=java_version to the kotlinc arguments. Set this to True to not add -Xjdk-release=java_version to the kotlinc arguments. This is helpful if the -Xjdk-release= brings trouble since it is not 100% guaranteed to work as expected. See the doc of kotlinc for more detail.

  • 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.

  • 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.

  • remove_classes: (defaults to: [])

    Specifies a list of Patterns that are used to exclude classes from the JAR. The pattern matching is based on the name of the class. This can be used to exclude a member class or delete a local view of a class that will be replaced during a later stage of the build.

  • resources: (defaults to: [])

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

    Note: If resources_root isn't set, Buck uses the .buckconfig property in .buckconfig to determine where resources should be placed within the generated JAR file.

  • srcs: (defaults to: [])

    The set of .kt, .java or .kts files to compile for this rule. If any of the files in this list end in .src.zip, then the entries in the ZIP file that end in .java or .kt will be included as ordinary inputs to compilation.

Examples



# A rule that compiles a single .kt file.
kotlin_library(
name = 'JsonUtil',
srcs = ['JsonUtil.kt'],
deps = [
'//third_party/guava:guava',
'//third_party/jackson:jackson',
],
)

# A rule that compiles all of the .kt files under the directory in
# which the rule is defined using glob(). It also excludes an
# individual file that may have additional dependencies, so it is
# compiled by a separate rule.
kotlin_library(
name = 'messenger',
srcs = glob(['**/*.kt'], excludes = ['MessengerModule.kt']),
deps = [
'//src/com/facebook/base:base',
'//third_party/guava:guava',
],
)

kotlin_library(
name = 'MessengerModule',
srcs = ['MessengerModule.kt'],
deps = [
'//src/com/facebook/base:base',
'//src/com/google/inject:inject',
'//third_party/guava:guava',
'//third_party/jsr-330:jsr-330',
],
)

# A rule that builds a library with both relative and
# fully-qualified deps.
kotlin_library(
name = 'testutil',
srcs = glob(['tests/**/*.kt'], excludes = 'tests/**/*Test.kt'),
deps = [
':lib-fb4a',
'//java/com/facebook/base:base',
],
)