Skip to content

Commit

Permalink
Add compose support to unified android macros (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkumar9t2 committed Jun 14, 2023
1 parent 9a9722c commit 4a18937
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
9 changes: 8 additions & 1 deletion rules/android/android_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def android_binary(
custom_package = {},
res_values = {},
enable_data_binding = False,
enable_compose = True,
**attrs):
"""
`android_binary` wrapper that adds Kotlin, build config, databinding and res values support. The attrs are passed to native `android_binary`
Expand All @@ -24,6 +25,8 @@ def android_binary(
build config fields
- res_value: `dict` accepting `string` key and their values. The value will be used to generate android resources and then adds as a
resource for `android_binary`.
- enable_data_binding: Enable android databinding support for this target
- enable_compose: Enable Jetpack Compose support for this target
"""

build_config_target = name + "_build_cfg"
Expand All @@ -46,6 +49,10 @@ def android_binary(

# Kotlin compilation with kt_android_library
kotlin_target = "lib_" + name
kotlin_library_deps = attrs.get("deps", default = []) + [build_config_target]
if enable_compose:
kotlin_library_deps.extend(["@grab_bazel_common//rules/android/compose:compose-plugin"])

kt_android_library(
name = kotlin_target,
srcs = attrs.get("srcs", default = []),
Expand All @@ -55,7 +62,7 @@ def android_binary(
manifest = attrs.get("manifest", default = None),
resource_files = resource_files,
visibility = attrs.get("visibility", default = None),
deps = attrs.get("deps", default = []) + [build_config_target],
deps = kotlin_library_deps,
)

# Build deps
Expand Down
10 changes: 9 additions & 1 deletion rules/android/android_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def android_library(
custom_package = {},
res_values = {},
enable_data_binding = False,
enable_compose = False,
**attrs):
"""
`android_library` wrapper that adds Kotlin, build config, databinding and res values support.
Expand All @@ -24,6 +25,8 @@ def android_library(
build config fields
- res_value: `dict` accepting `string` key and their values. The value will be used to generate android resources and then adds as a
resource for `android_library`.
- enable_data_binding: Enable android databinding support for this target
- enable_compose: Enable Jetpack Compose support for this target
"""

build_config_target = name + "_build_cfg"
Expand All @@ -44,6 +47,11 @@ def android_library(
res_values = res_values,
)

# Build deps
android_library_deps = attrs.get("deps", default = []) + [build_config_target]
if enable_compose:
android_library_deps.extend(["@grab_bazel_common//rules/android/compose:compose-plugin"])

# For now we delegate to existing macros to build the modules, as android library implementation matures, we can remove this and just
# have one implementation of android_library that does all esp databinding and Kotlin support.
# Databinding -> kt_db_android_library
Expand All @@ -69,6 +77,6 @@ def android_library(
assets_dir = attrs.get("assets_dir", default = None),
visibility = attrs.get("visibility", default = None),
tags = attrs.get("tags", default = None),
deps = attrs.get("deps", default = []) + [build_config_target],
deps = android_library_deps,
plugins = attrs.get("plugins", default = None),
)
16 changes: 16 additions & 0 deletions rules/android/compose/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@grab_bazel_common//rules:defs.bzl", "kt_compiler_plugin", "kt_jvm_library")

kt_compiler_plugin(
name = "compose-compiler-plugin",
id = "androidx.compose.compiler",
target_embedded_compiler = True,
deps = [
"@maven//:androidx_compose_compiler_compiler",
],
)

kt_jvm_library(
name = "compose-plugin",
exported_compiler_plugins = [":compose-compiler-plugin"],
visibility = ["//visibility:public"],
)
10 changes: 10 additions & 0 deletions rules/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
load("@grab_bazel_common//rules/android:android_binary.bzl", _android_binary = "android_binary")
load("@grab_bazel_common//rules/android:android_library.bzl", _android_library = "android_library")
load(
"@grab_bazel_common//rules/kotlin:kotlin.bzl",
_kt_compiler_plugin = "kt_compiler_plugin",
_kt_jvm_library = "kt_jvm_library",
)

# Android
android_binary = _android_binary
android_library = _android_library

# Kotlin
kt_jvm_library = _kt_jvm_library
kt_compiler_plugin = _kt_compiler_plugin
File renamed without changes.
5 changes: 5 additions & 0 deletions rules/kotlin/kotlin.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", _kt_jvm_library = "kt_jvm_library")
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", _kt_compiler_plugin = "kt_compiler_plugin")

kt_jvm_library = _kt_jvm_library
kt_compiler_plugin = _kt_compiler_plugin
1 change: 1 addition & 0 deletions workspace_defs.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GRAB_BAZEL_COMMON_ARTIFACTS = [
"org.jetbrains.kotlin:kotlin-parcelize-compiler:1.8.10",
"org.jetbrains.kotlin:kotlin-parcelize-runtime:1.8.10",
"androidx.compose.compiler:compiler:1.2.0-beta02",
"androidx.databinding:databinding-adapters:7.2.2",
"androidx.databinding:databinding-common:7.2.2",
"androidx.databinding:databinding-runtime:7.2.2",
Expand Down

0 comments on commit 4a18937

Please sign in to comment.