java - 如何在 Android 中通过 Bazel 使用房间数据库?

标签 java android android-room bazel

我有很多 SQLite 表,由于存在多个 DAO 类,这些表现在变得很难在应用程序端进行管理。我使用 Bazel 作为我的构建系统,但我不知道如何将 Room DB 与 Bazel 构建系统一起使用。

最佳答案

如果您使用像 rules_jvm_external 这样的 Maven Artifact 解析器,它会看起来像这样。

在您的 WORKSPACE 中文件中,添加对 Room 编译器的依赖:

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",
    ],
)

BUILD文件(例如 <project root>/BUILD ),创建 java_plugin目标公开 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"
    ],
)

最后,在您应用的 BUILD 中文件,取决于 Room 编译器和运行时:

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

我已经移植了一个 Android 示例应用程序,该应用程序使用 Room 和 Lifecycle 库通过 Bazel 进行构建:https://github.com/jin/BasicRxJavaSample-Bazel

关于java - 如何在 Android 中通过 Bazel 使用房间数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56218583/

相关文章:

java - 泛型类的概念问题

android - 有没有办法设置android :windowSoftInputMode by Fragment?

java - Android 房间迁移测试失败,但没有任何改变

android - 使用 Room Persistence Storage 在 Sqlite 中更改列的数据类型

java - asynctask 和 java 泛型

java - 自定义按钮在 Mac 上不起作用 (ButtonUI)

java - 在自定义 validator 中访问父属性路径

android - 如何在 Android Studio 中显示丰富的布局编辑器?

android - 为什么单击网页内容中的选择标签会导致崩溃?

android - 无法将表格迁移到Room do,将 bool 值保存在Sqlite中的方式出错