Skip to content

Commit ce2aaad

Browse files
fishypcj
authored andcommitted
Add kotlin_android_library macro (#32)
* Add kotlin_android_library rule * Add documentation for kotlin_android_library * Fix duplicated deps arguments in _res rule
1 parent d1a29da commit ce2aaad

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,115 @@ Includes all `kotlin_library` attributes as well as:
148148
| `main_class` | `string` | Main class to run with the `kotlin_binary` rule |
149149

150150

151+
### kotlin_android_library
152+
153+
A `kotlin_android_library` macro takes mostly the same arguments as a
154+
`kotlin_library`,
155+
plus some Android specific arguments like
156+
`aar_deps`, `resource_files`, `custom_package`, `manifest`, etc.
157+
An example with combined source and resource:
158+
159+
```python
160+
PACKAGE = "com.company.app"
161+
MANIFEST = "AndroidManifest.xml"
162+
163+
kotlin_android_library(
164+
name = "src",
165+
srcs = glob(["src/**/*.kt"]),
166+
custom_package = PACKAGE,
167+
manifest = MANIFEST,
168+
resource_files = glob(["res/**/*"]),
169+
java_deps = [
170+
"@com_squareup_okhttp3_okhttp//jar",
171+
"@com_squareup_okio_okio//jar",
172+
],
173+
aar_deps = [
174+
"@androidsdk//com.android.support:appcompat-v7-25.3.1",
175+
"@androidsdk//com.android.support:cardview-v7-25.3.1",
176+
"@androidsdk//com.android.support:recyclerview-v7-25.3.1",
177+
],
178+
)
179+
180+
android_binary(
181+
name = "app",
182+
custom_package = PACKAGE,
183+
manifest = MANIFEST,
184+
deps = [
185+
":src",
186+
],
187+
)
188+
```
189+
190+
If you prefer to separate your source files and resource files in
191+
different Bazel rules (for example the resource files are also used
192+
by java `android_library` rules), you can do so as example:
193+
194+
```python
195+
PACKAGE = "com.company.app"
196+
MANIFEST = "AndroidManifest.xml"
197+
198+
android_library(
199+
name = "res",
200+
custom_package = PACKAGE,
201+
manifest = MANIFEST,
202+
resource_files = glob(["res/**/*"]),
203+
aar_deps = [
204+
"@androidsdk//com.android.support:appcompat-v7-25.3.1",
205+
"@androidsdk//com.android.support:cardview-v7-25.3.1",
206+
"@androidsdk//com.android.support:recyclerview-v7-25.3.1",
207+
],
208+
)
209+
210+
android_library(
211+
name = "java",
212+
srcs = glob(["src/**/*.java"]),
213+
deps = [
214+
":res",
215+
# And other depedencies
216+
]
217+
)
218+
219+
kotlin_android_library(
220+
name = "kt",
221+
srcs = glob(["src/**/*.kt"]),
222+
aar_deps = [
223+
"@androidsdk//com.android.support:appcompat-v7-25.3.1",
224+
"@androidsdk//com.android.support:cardview-v7-25.3.1",
225+
"@androidsdk//com.android.support:recyclerview-v7-25.3.1",
226+
],
227+
android_deps = [
228+
":res",
229+
]
230+
)
231+
232+
android_binary(
233+
name = "app",
234+
custom_package = PACKAGE,
235+
manifest = MANIFEST,
236+
deps = [
237+
":java",
238+
":kt",
239+
":res",
240+
],
241+
)
242+
```
243+
244+
Please note that if you want to use `R` class in your Kotlin code,
245+
your `kotlin_android_library` rule need to either have the
246+
`resource_files` and related arguments,
247+
or have the resource rule in `android_deps`.
248+
249+
#### kotlin_android_library attributes
250+
251+
Includes all `kotlin_library` attributes as well as:
252+
253+
| Name | Type | Description |
254+
| --- | --- | --- |
255+
| `aar_deps` | `label_list` | List of AAR library targets |
256+
257+
And also [`android_library` arguments](android_library).
258+
259+
151260
### kotlin_test
152261

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

219328
[bazel]: http://www.bazel.io
220329
[kotlin]: http://www.kotlinlang.org
330+
[android_library]: https://docs.bazel.build/versions/master/be/android.html#android_library_args

kotlin/rules.bzl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,83 @@ kotlin_compile = rule(
176176
)
177177

178178

179+
def kotlin_android_library(
180+
name,
181+
srcs = [],
182+
deps = [],
183+
java_deps = [],
184+
aar_deps = [],
185+
resource_files = [],
186+
custom_package = None,
187+
manifest = None,
188+
proguard_specs = [],
189+
visibility = None,
190+
**kwargs):
191+
192+
res_deps = []
193+
if len(resource_files) > 0:
194+
native.android_library(
195+
name = name + "_res",
196+
custom_package = custom_package,
197+
manifest = manifest,
198+
resource_files = resource_files,
199+
deps = aar_deps + java_deps,
200+
**kwargs
201+
)
202+
res_deps.append(name + "_res")
203+
204+
native.android_library(
205+
name = name + "_aar",
206+
neverlink = 1,
207+
deps = aar_deps,
208+
)
209+
210+
native.java_import(
211+
name = name + "_sdk",
212+
neverlink = 1,
213+
jars = [
214+
"//tools/defaults:android_jar",
215+
],
216+
)
217+
218+
kotlin_compile(
219+
name = name + "_compile",
220+
srcs = srcs,
221+
deps = deps,
222+
java_deps = java_deps + [
223+
name + "_sdk",
224+
name + "_aar",
225+
],
226+
android_deps = res_deps,
227+
**kwargs
228+
)
229+
230+
# Convert kotlin deps into java deps
231+
kt_deps = []
232+
for dep in deps:
233+
kt_deps.append(dep + "_kt")
234+
235+
native.java_import(
236+
name = name + "_kt",
237+
jars = [name + "_compile.jar"],
238+
deps = kt_deps + java_deps,
239+
visibility = visibility,
240+
exports = [
241+
"@com_github_jetbrains_kotlin//:runtime",
242+
],
243+
)
244+
245+
native.android_library(
246+
name = name,
247+
deps = aar_deps + res_deps + [
248+
name + "_kt",
249+
],
250+
proguard_specs = proguard_specs,
251+
visibility = visibility,
252+
**kwargs
253+
)
254+
255+
179256
def kotlin_library(name, jars = [], java_deps = [], visibility = None, **kwargs):
180257

181258
kotlin_compile(

0 commit comments

Comments
 (0)