All Projects → grab → grab-bazel-common

grab / grab-bazel-common

Licence: Apache-2.0 license
Common rules and macros for Grab's Android projects built with Bazel.

Programming Languages

kotlin
9241 projects
Starlark
911 projects

Projects that are alternatives of or similar to grab-bazel-common

rules helm
rules_helm: Bazel rules for managing helm charts
Stars: ✭ 46 (+130%)
Mutual labels:  bazel, bazel-rules
rules verilator
Bazel build rules for Verilator
Stars: ✭ 14 (-30%)
Mutual labels:  bazel, bazel-rules
rules proto grpc
Bazel rules for building Protobuf and gRPC code and libraries from proto_library targets
Stars: ✭ 201 (+905%)
Mutual labels:  bazel, bazel-rules
rules elm
Bazel rules for building web applications written in Elm
Stars: ✭ 22 (+10%)
Mutual labels:  bazel, bazel-rules
rules gitops
This repository contains rules for continuous, GitOps driven Kubernetes deployments.
Stars: ✭ 112 (+460%)
Mutual labels:  bazel, bazel-rules
rules openapi
🍃 bazel rules for generating code from openapi specifications
Stars: ✭ 49 (+145%)
Mutual labels:  bazel, bazel-rules
bazel-latex
Bazel build system rules for LaTeX
Stars: ✭ 67 (+235%)
Mutual labels:  bazel, bazel-rules
Rules k8s
This repository contains rules for interacting with Kubernetes configurations / clusters.
Stars: ✭ 222 (+1010%)
Mutual labels:  bazel, bazel-rules
rules sass
Sass rules for Bazel
Stars: ✭ 47 (+135%)
Mutual labels:  bazel, bazel-rules
rules dart
Dart rules for Bazel
Stars: ✭ 35 (+75%)
Mutual labels:  bazel, bazel-rules
Rules rust
Rust rules for Bazel
Stars: ✭ 241 (+1105%)
Mutual labels:  bazel, bazel-rules
rules java
Java rules for Bazel
Stars: ✭ 44 (+120%)
Mutual labels:  bazel, bazel-rules
Rules kotlin
Bazel rules for Kotlin
Stars: ✭ 235 (+1075%)
Mutual labels:  bazel, bazel-rules
bazel-maven-proxy
A local (read-only) proxy for Bazel to access Maven resources behind a secure repository or from the local Maven repository
Stars: ✭ 22 (+10%)
Mutual labels:  bazel, bazel-rules
Rules python
Experimental Bazel Python Rules
Stars: ✭ 233 (+1065%)
Mutual labels:  bazel, bazel-rules
rules antlr
ANTLR rules for Bazel
Stars: ✭ 24 (+20%)
Mutual labels:  bazel, bazel-rules
Rules protobuf
Bazel rules for building protocol buffers and gRPC services (java, c++, go, ...)
Stars: ✭ 206 (+930%)
Mutual labels:  bazel, bazel-rules
Rules apple
Bazel rules to build apps for Apple platforms.
Stars: ✭ 217 (+985%)
Mutual labels:  bazel, bazel-rules
rules ocaml
OCaml build rules for Bazel
Stars: ✭ 38 (+90%)
Mutual labels:  bazel, bazel-rules
rules appengine
AppEngine rules for Bazel
Stars: ✭ 28 (+40%)
Mutual labels:  bazel, bazel-rules

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 for details.

The rules are used by Grazel - Gradle plugin to automate migration to Bazel.

Usage

In WORKSPACE file,

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "grab_bazel_common",
    commit = "<commit-hash>",
    remote = "https://github.com/grab/grab-bazel-common.git",
)

# Optional patched Android Tools
load("@grab_bazel_common//:workspace_defs.bzl", "android_tools")

android_tools(
    commit = "<commit-hash>",
    remote = "https://github.com/grab/grab-bazel-common.git",
)

# Maven dependencies
load("@grab_bazel_common//:workspace_defs.bzl", "GRAB_BAZEL_COMMON_ARTIFACTS")

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    artifacts = GRAB_BAZEL_COMMON_ARTIFACTS + [
        # ... 
    ],
    ...
)

Features

Build Config Fields

Build Config support for android projects

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

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.

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.

# 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:

sourceSets {
    debug {
        res.srcDirs += "src/main/res-debug"
    }
}

the Bazel equivalent would be:

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 feature. With default values, Android Unit tests can be executed without Robolectric 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

load("@grab_bazel_common//tools/test:test.bzl", "grab_kt_jvm_test")

grab_kt_jvm_test(
    name = "binding-adapter-processor-test",
    srcs = glob([
        "src/test/java/**/*.kt",
    ]),
    deps = [
        ":binding-adapter-bridge",
        ":binding-adapter-processor",
        "@com_github_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. Reference.

Android Unit tests

Similarly for android unit tests, use grab_android_local_test to build and execute tests. Reference.

load("@grab_bazel_common//tools/test:test.bzl", "grab_android_local_test")

grab_android_local_test(
    name = "grab_android_local_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 at

    http://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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].