{"id":13659223,"url":"https://github.com/pubref/rules_kotlin","last_synced_at":"2025-09-08T12:48:33.405Z","repository":{"id":95014854,"uuid":"67053420","full_name":"pubref/rules_kotlin","owner":"pubref","description":"Bazel rules for Kotlin","archived":false,"fork":false,"pushed_at":"2018-06-03T18:25:58.000Z","size":82,"stargazers_count":159,"open_issues_count":5,"forks_count":20,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-08-02T05:10:31.580Z","etag":null,"topics":["bazel","bazel-rules","java","kotlin"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pubref.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-31T16:09:51.000Z","updated_at":"2024-05-08T19:02:22.000Z","dependencies_parsed_at":"2023-05-19T01:46:14.307Z","dependency_job_id":null,"html_url":"https://github.com/pubref/rules_kotlin","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubref%2Frules_kotlin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubref%2Frules_kotlin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubref%2Frules_kotlin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubref%2Frules_kotlin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pubref","download_url":"https://codeload.github.com/pubref/rules_kotlin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223952710,"owners_count":17230936,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bazel","bazel-rules","java","kotlin"],"created_at":"2024-08-02T05:01:06.481Z","updated_at":"2024-11-10T12:31:28.906Z","avatar_url":"https://github.com/pubref.png","language":"Python","readme":"\u003ctable\u003e\u003ctr\u003e\n\u003ctd\u003e\u003cimg src=\"https://bazel.build/images/bazel-icon.svg\" width=\"120\"/\u003e\u003c/td\u003e\n\u003ctd\u003e\u003cimg src=\"https://kotlinlang.org/assets/images/open-graph/kotlin_250x250.png\" width=\"120\"/\u003e\u003c/td\u003e\n\u003c/tr\u003e\u003ctr\u003e\n\u003ctd\u003eBazel\u003c/td\u003e\n\u003ctd\u003eKotlin\u003c/td\u003e\n\u003c/tr\u003e\u003c/table\u003e\n\n# Kotlin Rules for Bazel\n[![Build Status](https://travis-ci.org/pubref/rules_kotlin.svg?branch=master)](https://travis-ci.org/pubref/rules_kotlin)\n\nBuild [Kotlin][kotlin] source with with [Bazel][bazel].\n\n| Rule | Description |\n| ---: | :--- |\n| `kotlin_repositories` | Load workspace dependencies |\n| [`kotlin_library`](#kotlin_library) | Build a java library from kotlin source |\n| [`kotlin_binary`](#kotlin_binary) | Build a java binary from kotlin source |\n| [`kotlin_android_library`](#kotlin_android_library) | Build an android library from kotlin source |\n| [`kotlin_test`](#kotlin_test) | Run a kotlin test |\n\n\u003e Note: **Bazel 0.7.0 or higher is required for rules_kotlin 0.5.0**.\n\n## Workspace rules\n\nAdd the following to your `WORKSPACE` file:\n\n```python\ngit_repository(\n    name = \"org_pubref_rules_kotlin\",\n    remote = \"https://github.com/pubref/rules_kotlin.git\",\n    tag = \"v0.5.0\", # update as needed\n)\n\nload(\"@org_pubref_rules_kotlin//kotlin:rules.bzl\", \"kotlin_repositories\")\n\nkotlin_repositories()\n```\n\nThis will fetch a\n[kotlin release](https://github.com/JetBrains/kotlin/releases)\n(currently 1.1.4-3) and load a number of dependencies related to\ndagger (used to build the `KotlinCompiler` bazel worker).\n\n\u003e You can override kotlin release via the `com_github_jetbrains_kotlin_url`, `com_github_jetbrains_kotlin_sha256` options\n\u003e and various dependencies loaded in the `kotlin_repositories` rule via the `omit_*` options;\n\u003e see the source file for details.\n\n## BUILD rules\n\nAdd the following to your BUILD file:\n\n```python\nload(\"@org_pubref_rules_kotlin//kotlin:rules.bzl\", \"kotlin_library\")\n```\n\n### kotlin_library\n\nExample:\n\n```python\nkotlin_library(\n    name = \"my_kotlin_lib\",\n    srcs = [\"kotlin_source_file.kt\"],\n    deps = [\":some_other_kotlin_library_rule\"],\n    java_deps = [\":some_other_java_library_rule\", \"@another_maven_jar//jar\"],\n)\n```\n\nUse the `deps` attribute to name other `kotlin_library` targets as jar\nproviders for this rule.  Use the `java_deps` attribute to name other\n`java_library` or `java_import` targets (to expose traditional java\nclasses in your kotlin source).\n\nTo compile a set of kotlin sources files with the `kotlinc` tool and\nemit the corresponding jar file, use:\n\n```sh\n$ bazel build :my_kotlin_lib\nTarget :my_kotlin_lib up-to-date:\n  bazel-bin/.../my_kotlin_lib.jar\n```\n\nTo use the output of a `kotlin_library` as input to a `java_library`\nrule (and make your compiled kotlin classes available to your\ntraditional java source files), name it as a dependency using the\n`deps` attribute, just as you would any other `java_library` target.\nThe name of this target must be of the form\n`{kotlin_library_target_name}_kt`.  For example, to use the\n`:my_kotlin_lib` in an `android_binary` target, the name would be\n`:my_kotlin_lib_kt`, such as:\n\n```python\nandroid_binary(\n   name = \"foo\",\n   deps = [\n       \":my_kotlin_lib_kt`,\n   ]\n)\n```\n\n### kotlin_library attributes\n\n| Name | Type | Description |\n| --- | --- | --- |\n| `srcs` | `label_list` | Kotlin source files `*.kt` |\n| `deps` | `label_list` | List of `kotlin_library` targets |\n| `java_deps` | `label_list` | List of java provider targets (`java_library`, `java_import`, `...`) |\n| `android_deps` | `label_list` | List of android provider targets (`android_library`) |\n| `jars` | `label_list` | List of jar file targets (`*.jar`) |\n| `x_opts` | `string_list` | List of additional `-X` options to `kotlinc` |\n| `plugin_opts` | `string_dict` | List of additional `-P` options to `kotlinc` |\n\n\n### kotlin_binary\n\nA `kotlin_binary` macro takes the same arguments as a\n`kotlin_library`, plus a required `main_class` argument (the name of\nthe compiled kotlin class to run, in java package notation).  This\nclass should have a `fun main(...)` entrypoint.  Example:\n\n```python\nkotlin_binary(\n    name = \"main_kt\",\n    main_class = \"my.project.MainKt\",\n    srcs = [\"main.kt\"],\n    deps = [\":my_kotlin_lib\"]\n    java_deps = [\":javalib\"]\n)\n```\n\nTo create a self-contained executable jar, invoke the implicit\n`_deploy.jar` target. For example:\n\n```sh\n$ bazel build :main_kt_deploy.jar\nTarget :main_kt_deploy.jar up-to-date:\n  bazel-bin/.../main_kt_deploy.jar\n$ java -jar ./bazel-bin/.../main_kt_deploy.jar\n```\n\n\u003e The `kotlin-runtime.jar` is implicitly included by the `kotlin_binary` rule.\n\n#### kotlin_binary attributes\n\nIncludes all `kotlin_library` attributes as well as:\n\n| Name | Type | Description |\n| --- | --- | --- |\n| `main_class` | `string` | Main class to run with the `kotlin_binary` rule |\n\n\n### kotlin_android_library\n\nA `kotlin_android_library` macro takes mostly the same arguments as a\n`kotlin_library`,\nplus some Android specific arguments like\n`aar_deps`, `resource_files`, `custom_package`, `manifest`, etc.\nAn example with combined source and resource:\n\n```python\nPACKAGE = \"com.company.app\"\nMANIFEST = \"AndroidManifest.xml\"\n\nkotlin_android_library(\n    name = \"src\",\n    srcs = glob([\"src/**/*.kt\"]),\n    custom_package = PACKAGE,\n    manifest = MANIFEST,\n    resource_files = glob([\"res/**/*\"]),\n    java_deps = [\n        \"@com_squareup_okhttp3_okhttp//jar\",\n        \"@com_squareup_okio_okio//jar\",\n    ],\n    aar_deps = [\n        \"@androidsdk//com.android.support:appcompat-v7-25.3.1\",\n        \"@androidsdk//com.android.support:cardview-v7-25.3.1\",\n        \"@androidsdk//com.android.support:recyclerview-v7-25.3.1\",\n    ],\n)\n\nandroid_binary(\n    name = \"app\",\n    custom_package = PACKAGE,\n    manifest = MANIFEST,\n    deps = [\n        \":src\",\n    ],\n)\n```\n\nIf you prefer to separate your source files and resource files in\ndifferent Bazel rules (for example the resource files are also used\nby java `android_library` rules), you can do so as example:\n\n```python\nPACKAGE = \"com.company.app\"\nMANIFEST = \"AndroidManifest.xml\"\n\nandroid_library(\n    name = \"res\",\n    custom_package = PACKAGE,\n    manifest = MANIFEST,\n    resource_files = glob([\"res/**/*\"]),\n    aar_deps = [\n        \"@androidsdk//com.android.support:appcompat-v7-25.3.1\",\n        \"@androidsdk//com.android.support:cardview-v7-25.3.1\",\n        \"@androidsdk//com.android.support:recyclerview-v7-25.3.1\",\n    ],\n)\n\nandroid_library(\n    name = \"java\",\n    srcs = glob([\"src/**/*.java\"]),\n    deps = [\n        \":res\",\n        # And other depedencies\n    ]\n)\n\nkotlin_android_library(\n    name = \"kt\",\n    srcs = glob([\"src/**/*.kt\"]),\n    aar_deps = [\n        \"@androidsdk//com.android.support:appcompat-v7-25.3.1\",\n        \"@androidsdk//com.android.support:cardview-v7-25.3.1\",\n        \"@androidsdk//com.android.support:recyclerview-v7-25.3.1\",\n    ],\n    android_deps = [\n        \":res\",\n    ]\n)\n\nandroid_binary(\n    name = \"app\",\n    custom_package = PACKAGE,\n    manifest = MANIFEST,\n    deps = [\n        \":java\",\n        \":kt\",\n        \":res\",\n    ],\n)\n```\n\nPlease note that if you want to use `R` class in your Kotlin code,\nyour `kotlin_android_library` rule need to either have the\n`resource_files` and related arguments,\nor have the resource rule in `android_deps`.\n\n#### kotlin_android_library attributes\n\nIncludes all `kotlin_library` attributes as well as:\n\n| Name | Type | Description |\n| --- | --- | --- |\n| `aar_deps` | `label_list` | List of AAR library targets |\n\nAnd also [`android_library`](android_library) arguments.\n\n\n### kotlin_test\n\nThe `kotlin_test` rule is nearly identical the `kotlin_binary` rule\n(other than calling `java_test` internally rather than `java_binary`).\n\n\n```python\nkotlin_test(\n    name = \"main_kt_test\",\n    test_class = \"examples.helloworld.MainKtTest\",\n    srcs = [\"MainKtTest.kt\"],\n    size = \"small\",\n    deps = [\n        \":rules\",\n    ],\n    java_deps = [\n        \"@junit4//jar\",\n    ],\n)\n```\n\n```sh\n$ bazel test :main_kt_test.jar\n```\n\n\u003e The `kotlin-test.jar` is implicitly included by the `kotlin_test` rule.\n\n### kotlin_compile\n\n\u003e TL;DR; You most likely do not need to interact with the\n\u003e `kotlin_compile` rule directly.\n\nThe `kotlin_compile` rule runs the kotlin compiler to generate a\n`.jar` file from a list of kotlin source files.  The `kotlin_library`\nrule calls this internally and then makes the jarfile available to\nother java rules via a `java_import` rule.\n\n# Summary\n\nThat's it!  Hopefully these rules with make it easy to mix kotlin and\ntraditional java code in your projects and take advantage of bazel's\napproach to fast, repeatable, and reliable builds.\n\n\u003e Note: Consider [rules_maven](https://github.com/pubref/rules_maven)\n\u003e for handling transitive maven dependencies with your java/kotlin\n\u003e projects.\n\n## Examples\n\nTo run the examples in this repository, clone the repo:\n\n```sh\n$ git clone https://github.com/pubref/rules_kotlin\n$ cd rules_kotlin\n$ bazel query //... --output label_kind\n$ bazel run examples/helloworld:main_kt\n$ bazel run examples/helloworld:main_java\n$ bazel test examples/helloworld:main_test\n$ bazel test examples/helloworld:main_kt_test\n```\n\n## TODO\n\n1. Proper `data` and runfiles support.\n2. kapt support.\n3. Incremental compilation.\n\n[bazel]: http://www.bazel.build\n[kotlin]: http://www.kotlinlang.org\n[android_library]: https://docs.bazel.build/versions/master/be/android.html#android_library_args\n","funding_links":[],"categories":["Python (144)","Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubref%2Frules_kotlin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpubref%2Frules_kotlin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubref%2Frules_kotlin/lists"}