Skip to content

Commit

Permalink
Add kotlin_android_library macro (#32)
Browse files Browse the repository at this point in the history
* Add kotlin_android_library rule

* Add documentation for kotlin_android_library

* Fix duplicated deps arguments in _res rule
  • Loading branch information
fishy authored and pcj committed Sep 8, 2017
1 parent d1a29da commit ce2aaad
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,115 @@ Includes all `kotlin_library` attributes as well as:
| `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:

```python
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:

```python
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](android_library).


### kotlin_test

The `kotlin_test` rule is nearly identical the `kotlin_binary` rule
Expand Down Expand Up @@ -218,3 +327,4 @@ $ bazel test examples/helloworld:main_kt_test

[bazel]: http://www.bazel.io
[kotlin]: http://www.kotlinlang.org
[android_library]: https://docs.bazel.build/versions/master/be/android.html#android_library_args
77 changes: 77 additions & 0 deletions kotlin/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,83 @@ kotlin_compile = rule(
)


def kotlin_android_library(
name,
srcs = [],
deps = [],
java_deps = [],
aar_deps = [],
resource_files = [],
custom_package = None,
manifest = None,
proguard_specs = [],
visibility = None,
**kwargs):

res_deps = []
if len(resource_files) > 0:
native.android_library(
name = name + "_res",
custom_package = custom_package,
manifest = manifest,
resource_files = resource_files,
deps = aar_deps + java_deps,
**kwargs
)
res_deps.append(name + "_res")

native.android_library(
name = name + "_aar",
neverlink = 1,
deps = aar_deps,
)

native.java_import(
name = name + "_sdk",
neverlink = 1,
jars = [
"//tools/defaults:android_jar",
],
)

kotlin_compile(
name = name + "_compile",
srcs = srcs,
deps = deps,
java_deps = java_deps + [
name + "_sdk",
name + "_aar",
],
android_deps = res_deps,
**kwargs
)

# Convert kotlin deps into java deps
kt_deps = []
for dep in deps:
kt_deps.append(dep + "_kt")

native.java_import(
name = name + "_kt",
jars = [name + "_compile.jar"],
deps = kt_deps + java_deps,
visibility = visibility,
exports = [
"@com_github_jetbrains_kotlin//:runtime",
],
)

native.android_library(
name = name,
deps = aar_deps + res_deps + [
name + "_kt",
],
proguard_specs = proguard_specs,
visibility = visibility,
**kwargs
)


def kotlin_library(name, jars = [], java_deps = [], visibility = None, **kwargs):

kotlin_compile(
Expand Down

0 comments on commit ce2aaad

Please sign in to comment.