我有很多 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/