Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/grab/grab-bazel-common
Common rules and macros for Grab's Android projects built with Bazel.
https://github.com/grab/grab-bazel-common
android android-bazel bazel bazel-rules kotlin
Last synced: 16 days ago
JSON representation
Common rules and macros for Grab's Android projects built with Bazel.
- Host: GitHub
- URL: https://github.com/grab/grab-bazel-common
- Owner: grab
- License: apache-2.0
- Created: 2021-06-22T05:26:41.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-12-03T08:25:37.000Z (about 1 month ago)
- Last Synced: 2024-12-03T09:29:46.862Z (about 1 month ago)
- Topics: android, android-bazel, bazel, bazel-rules, kotlin
- Language: Kotlin
- Homepage: https://grab.github.io/grazel
- Size: 243 MB
- Stars: 39
- Watchers: 13
- Forks: 15
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# Grab Bazel Common Android
Common rules and macros for Grab's Android projects built with Bazel. This repo provides rules and macros to support some of Android Gradle
Plugin features in Bazel.The repo also hosts a patched Bazel Android Tools jar with fixes for build reproducibility and databinding applied.
See [/patches](https://github.com/grab/grab-bazel-common/tree/master/patches) for details.The rules are used by [Grazel](https://github.com/grab/Grazel) - Gradle plugin to automate migration to Bazel.
# Usage
In `WORKSPACE` file,
```python
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")git_repository(
name = "grab_bazel_common",
commit = "",
remote = "https://github.com/grab/grab-bazel-common.git",
)load("@grab_bazel_common//rules:repositories.bzl", "bazel_common_dependencies")
bazel_common_dependencies()
load("@grab_bazel_common//rules:setup.bzl", "bazel_common_setup")
bazel_common_setup(
patched_android_tools = True, # Optionally use patched android_tools jars
buildifier_version = "6.3.3", # Optionally can specify buildifier version
)load("@grab_bazel_common//rules:maven.bzl", "pin_bazel_common_dependencies")
pin_bazel_common_dependencies()
load("@grab_bazel_common//:workspace_defs.bzl", "GRAB_BAZEL_COMMON_ARTIFACTS")
# Bazel common uses few artifacts under @maven repository
maven_install(
artifacts = GRAB_BAZEL_COMMON_ARTIFACTS + [
# Your project artifacts
]
)
```# Features
### Build Config Fields
`Build Config` support for android projects
```python
load("@grab_bazel_common//tools/build_config:build_config.bzl", "build_config")build_config(
name = "feature-toggle-build-config",
package_name = "com.grab.featuretoggle",
strings = {
"ID": "Hello",
},
ints = {},
longs = {},
strings = {},
)
```### Res values
Gradle's `resValue` strings support for Android Projects
```python
load("@grab_bazel_common//tools/res_value:res_value.bzl", "res_value")
# Usage of defined resValues
android_library(
resource_files = [
...
] + res_value(
name = "app-res-value",
strings = {
"prefix": "app",
"field": "debug"
},
),
)
```### Databinding
Provides a macro which in most cases can be used as a drop-in replacement to `kt_android_library` to enable support for Kotlin code when
using databinding. [Details](https://github.com/grab/grab-bazel-common/blob/documentation/tools/databinding/databinding.bzl). Worker support
for some of the internal actions can be enabled by `build --strategy=DatabindingStubs=worker`.```python
load("@grab_bazel_common//tools/databinding:databinding.bzl", "kt_db_android_library")kt_db_android_library(
name = "module",
srcs = glob([
"src/main/java/com/grab/module/**/*.kt",
]),
assets = glob([
"src/main/assets/empty_file.txt",
]),
assets_dir = "src/main/assets",
custom_package = "com.grab.module",
manifest = "src/main/AndroidManifest.xml",
resource_files = glob([
"src/main/res/**",
]),
visibility = [
"//visibility:public",
],
deps = [
"@maven//:io_reactivex_rxjava2_rxjava",
],
)
```This requires the following flags in `.bazelrc` file.
```python
# Databinding flags
build --experimental_android_databinding_v2
build --android_databinding_use_v3_4_args
build --android_databinding_use_androidx# Flags to enable latest android providers in rules
build --experimental_google_legacy_api
query --experimental_google_legacy_api
```### Custom Resource Sets
Bazel expects certain Android resource folder structure (should start with `res/`) and this can conflict with Android Gradle plugin's custom
resource source set feature which does not have this validation. This macro helps to adapt the folder to Bazel expected structure so both
build systems can function.In Gradle, if you have:
```groovy
sourceSets {
debug {
res.srcDirs += "src/main/res-debug"
}
}
```the Bazel equivalent would be:
```python
load("@grab_bazel_common//tools/custom_res:custom_res.bzl", "custom_res")android_binary(
name = "app",
custom_package = "com.grab.playground",
manifest = "src/main/AndroidManifest.xml",
resource_files = glob([
"src/main/res/**",
]) + custom_res( # Wrap the folder with custom_res macro
dir_name = "res-debug",
resource_files = glob([
"src/main/res-debug/**",
]),
target = "app",
),
)
```### Unit Test Macros
Provides macros to simplify migrating unit tests to Bazel and ports over Android Gradle
Plugin's [return default values](https://developer.android.com/studio/test/index.html#test_options) feature. With default values, Android
Unit tests can be executed without [Robolectric](http://robolectric.org) by relying on mocked `android.jar` as an alternative
to `android_local_test` in Bazel.The below macros makes assumptions that files containing Kotlin tests are named `*Tests.kt` or `Test.kt` and class name matches the file
name.#### Kotlin Unit tests
```python
load("@grab_bazel_common//rules:defs.bzl", "kotlin_test")kotlin_test(
name = "kotlin_test",
srcs = glob([
"src/test/java/**/*.kt",
]),
deps = [
":binding-adapter-bridge",
":binding-adapter-processor",
"@maven//:org_jetbrains_kotlin_kotlin_test",
"@maven//:com_github_tschuchortdev_kotlin_compile_testing",
"@maven//:junit_junit",
],
)
```This will generate a single build target for all Kotlin files and individual `*Test` targets for each `*Test`
class#### Android Unit tests
Similarly for android unit tests, use `android_unit_test` to build and execute tests
```python
load("@grab_bazel_common//rules:defs.bzl", "android_unit_test")android_unit_test(
name = "android_unit_test",
srcs = glob([
"src/test/java/**/*.kt",
]),
associates = [
":grab_android_local_test_lib_kt",
],
deps = [
"@maven//:junit_junit",
],
)
```# License
```
Copyright 2021 Grabtaxi Holdings PTE LTE (GRAB)Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```