ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - pubref/rules_kotlin: Bazel rules for Kotlin
Bazel rules for Kotlin. Contribute to pubref/rules_kotlin development by creating an account on GitHub.
Visit Site

GitHub - pubref/rules_kotlin: Bazel rules for Kotlin

GitHub - pubref/rules_kotlin: Bazel rules for Kotlin

Kotlin Rules for Bazel

Build Status

Build Kotlin source with with Bazel.

Rule Description
kotlin_repositories Load workspace dependencies
kotlin_library Build a java library from kotlin source
kotlin_binary Build a java binary from kotlin source
kotlin_android_library Build an android library from kotlin source
kotlin_test Run a kotlin test

Note: Bazel 0.7.0 or higher is required for rules_kotlin 0.5.0.

Workspace rules

Add the following to your WORKSPACE file:

git_repository(
    name = "org_pubref_rules_kotlin",
    remote = "https://github.com/pubref/rules_kotlin.git",
    tag = "v0.5.0", # update as needed
)

load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_repositories")

kotlin_repositories()

This will fetch a kotlin release (currently 1.1.4-3) and load a number of dependencies related to dagger (used to build the KotlinCompiler bazel worker).

You can override kotlin release via the com_github_jetbrains_kotlin_url, com_github_jetbrains_kotlin_sha256 options and various dependencies loaded in the kotlin_repositories rule via the omit_* options; see the source file for details.

BUILD rules

Add the following to your BUILD file:

load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_library")

kotlin_library

Example:

kotlin_library(
    name = "my_kotlin_lib",
    srcs = ["kotlin_source_file.kt"],
    deps = [":some_other_kotlin_library_rule"],
    java_deps = [":some_other_java_library_rule", "@another_maven_jar//jar"],
)

Use the deps attribute to name other kotlin_library targets as jar providers for this rule. Use the java_deps attribute to name other java_library or java_import targets (to expose traditional java classes in your kotlin source).

To compile a set of kotlin sources files with the kotlinc tool and emit the corresponding jar file, use:

$ bazel build :my_kotlin_lib
Target :my_kotlin_lib up-to-date:
  bazel-bin/.../my_kotlin_lib.jar

To use the output of a kotlin_library as input to a java_library rule (and make your compiled kotlin classes available to your traditional java source files), name it as a dependency using the deps attribute, just as you would any other java_library target. The name of this target must be of the form {kotlin_library_target_name}_kt. For example, to use the :my_kotlin_lib in an android_binary target, the name would be :my_kotlin_lib_kt, such as:

android_binary(
   name = "foo",
   deps = [
       ":my_kotlin_lib_kt`,
   ]
)

kotlin_library attributes

Name Type Description
srcs label_list Kotlin source files *.kt
deps label_list List of kotlin_library targets
java_deps label_list List of java provider targets (java_library, java_import, ...)
android_deps label_list List of android provider targets (android_library)
jars label_list List of jar file targets (*.jar)
x_opts string_list List of additional -X options to kotlinc
plugin_opts string_dict List of additional -P options to kotlinc

kotlin_binary

A kotlin_binary macro takes the same arguments as a kotlin_library, plus a required main_class argument (the name of the compiled kotlin class to run, in java package notation). This class should have a fun main(...) entrypoint. Example:

kotlin_binary(
    name = "main_kt",
    main_class = "my.project.MainKt",
    srcs = ["main.kt"],
    deps = [":my_kotlin_lib"]
    java_deps = [":javalib"]
)

To create a self-contained executable jar, invoke the implicit _deploy.jar target. For example:

$ bazel build :main_kt_deploy.jar
Target :main_kt_deploy.jar up-to-date:
  bazel-bin/.../main_kt_deploy.jar
$ java -jar ./bazel-bin/.../main_kt_deploy.jar

The kotlin-runtime.jar is implicitly included by the kotlin_binary rule.

kotlin_binary attributes

Includes all kotlin_library attributes as well as:

Name Type Description
main_class string Main class to run with the kotlin_binary rule

kotlin_android_library

A kotlin_android_library macro takes mostly the same arguments as a kotlin_library, plus some Android specific arguments like aar_deps, resource_files, custom_package, manifest, etc. An example with combined source and resource:

PACKAGE = "com.company.app"
MANIFEST = "AndroidManifest.xml"

kotlin_android_library(
    name = "src",
    srcs = glob(["src/**/*.kt"]),
    custom_package = PACKAGE,
    manifest = MANIFEST,
    resource_files = glob(["res/**/*"]),
    java_deps = [
        "@com_squareup_okhttp3_okhttp//jar",
        "@com_squareup_okio_okio//jar",
    ],
    aar_deps = [
        "@androidsdk//com.android.support:appcompat-v7-25.3.1",
        "@androidsdk//com.android.support:cardview-v7-25.3.1",
        "@androidsdk//com.android.support:recyclerview-v7-25.3.1",
    ],
)

android_binary(
    name = "app",
    custom_package = PACKAGE,
    manifest = MANIFEST,
    deps = [
        ":src",
    ],
)

If you prefer to separate your source files and resource files in different Bazel rules (for example the resource files are also used by java android_library rules), you can do so as example:

PACKAGE = "com.company.app"
MANIFEST = "AndroidManifest.xml"

android_library(
    name = "res",
    custom_package = PACKAGE,
    manifest = MANIFEST,
    resource_files = glob(["res/**/*"]),
    aar_deps = [
        "@androidsdk//com.android.support:appcompat-v7-25.3.1",
        "@androidsdk//com.android.support:cardview-v7-25.3.1",
        "@androidsdk//com.android.support:recyclerview-v7-25.3.1",
    ],
)

android_library(
    name = "java",
    srcs = glob(["src/**/*.java"]),
    deps = [
        ":res",
        # And other depedencies
    ]
)

kotlin_android_library(
    name = "kt",
    srcs = glob(["src/**/*.kt"]),
    aar_deps = [
        "@androidsdk//com.android.support:appcompat-v7-25.3.1",
        "@androidsdk//com.android.support:cardview-v7-25.3.1",
        "@androidsdk//com.android.support:recyclerview-v7-25.3.1",
    ],
    android_deps = [
        ":res",
    ]
)

android_binary(
    name = "app",
    custom_package = PACKAGE,
    manifest = MANIFEST,
    deps = [
        ":java",
        ":kt",
        ":res",
    ],
)

Please note that if you want to use R class in your Kotlin code, your kotlin_android_library rule need to either have the resource_files and related arguments, or have the resource rule in android_deps.

kotlin_android_library attributes

Includes all kotlin_library attributes as well as:

Name Type Description
aar_deps label_list List of AAR library targets

And also android_library arguments.

kotlin_test

The kotlin_test rule is nearly identical the kotlin_binary rule (other than calling java_test internally rather than java_binary).

kotlin_test(
    name = "main_kt_test",
    test_class = "examples.helloworld.MainKtTest",
    srcs = ["MainKtTest.kt"],
    size = "small",
    deps = [
        ":rules",
    ],
    java_deps = [
        "@junit4//jar",
    ],
)
$ bazel test :main_kt_test.jar

The kotlin-test.jar is implicitly included by the kotlin_test rule.

kotlin_compile

TL;DR; You most likely do not need to interact with the kotlin_compile rule directly.

The kotlin_compile rule runs the kotlin compiler to generate a .jar file from a list of kotlin source files. The kotlin_library rule calls this internally and then makes the jarfile available to other java rules via a java_import rule.

Summary

That's it! Hopefully these rules with make it easy to mix kotlin and traditional java code in your projects and take advantage of bazel's approach to fast, repeatable, and reliable builds.

Note: Consider rules_maven for handling transitive maven dependencies with your java/kotlin projects.

Examples

To run the examples in this repository, clone the repo:

$ git clone https://github.com/pubref/rules_kotlin
$ cd rules_kotlin
$ bazel query //... --output label_kind
$ bazel run examples/helloworld:main_kt
$ bazel run examples/helloworld:main_java
$ bazel test examples/helloworld:main_test
$ bazel test examples/helloworld:main_kt_test

TODO

  1. Proper data and runfiles support.
  2. kapt support.
  3. Incremental compilation.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory