简体   繁体   中英

How to use Room Database in Android with Bazel?

I have lots of SQLite tables that has now become hard to manage at the app side because of multiple DAO classes. I am using Bazel as my build system but I can't figure out how to use Room DB with Bazel build system.

If you use a Maven artifact resolver like rules_jvm_external , it'll look something like this.

In your WORKSPACE file, add the dependency on the Room compiler:

load("@rules_jvm_external//:specs.bzl", "maven")

maven_install(
    name = "maven",
    artifacts = [
        "androidx.room:room-runtime:2.1.0-alpha04",
        "androidx.room:room-compiler:2.1.0-alpha04",
        "com.google.guava:guava:28.1-android",
        maven.artifact("com.google.auto", "auto-common", "0.10", neverlink = True),
        # .. other artifacts
    ],
    repositories = [
        "https://maven.google.com",
        "https://jcenter.bintray.com",
    ],
)

In a BUILD file (eg <project root>/BUILD ), create the java_plugin target to expose the annotation processor for Room:

java_plugin(
    name = "androidx_room_room_compiler_plugin",
    processor_class = "androidx.room.RoomProcessor",
    deps = ["@maven//:androidx_room_room_compiler"],
    neverlink = True,
)

java_library(
    name = "androidx_room_room_compiler_library",
    exports = [
        "@maven//:androidx_room_room_compiler",
    ],
    exported_plugins = [
        ":androidx_room_room_compiler_plugin"
    ],
)

Finally, in your app's BUILD file, depend on the Room compiler and runtime:

android_library(
    name = "lib_prod",
    # ...
    deps = [
        "@maven//:androidx_room_room_runtime",
        "//:androidx_room_room_compiler_library",
    ],
)

I have ported an Android sample app that uses the Room and Lifecycle libraries to build with Bazel here: https://github.com/jin/BasicRxJavaSample-Bazel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM