如何在 Bazel 中为 Android 项目添加 Maven 依赖项(例如 Volley、Gson)?

How to add Maven dependencies (e.g. Volley, Gson) in Bazel for an Android project?

我现在就是这样做的

deps = [
   ":tensorflow_native_libs",
   "//tensorflow/contrib/lite/java:tensorflowlite",
   "@androidsdk//com.android.support:appcompat-v7-25.0.0",
   "@androidsdk//com.android.volley:volley:1.1.0",
   "@androidsdk//com.google.code.gson:gson:2.6.2"
],

里面 android_binary。但我收到此错误:

ERROR: /home/abhi/Desktop/Git/SENA-28/tensorflow/SenDetect/BUILD:24:1: //SenDetect:sendetect: invalid label '@androidsdk//com.android.volley:volley:1.1.0' in element 3 of attribute 'deps' in 'android_binary' rule: invalid target name 'volley:1.1.0': target names may not contain ':'

截至 2019 年 4 月:

更新:rules_jvm_external 是 Bazel 团队的新规则集,用于传递地获取和解析工件。

在这种情况下,WORKSPACE 文件将包含如下内容:

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

maven_install(
    artifacts = [
        "com.android.volley:volley:1.1.0",
        "com.google.code.gson:gson:2.6.2",
        "com.android.support:design:27.0.2",
        "com.android.support:support_annotations:jar:27.0.2",
    ],
    repositories = [
        "https://jcenter.bintray.com",
        "https://maven.google.com",
    ]
)

然后在BUILD文件中,可以像这样直接依赖Volley和Gson:

android_library(
    name = "my_lib",
    srcs = # ...
    deps = [
        "@maven//:com_android_volley_volley",
        "@maven//:com_google_code_gson_gson",
        "@maven//:com_android_support_design",
        "@maven//:com_android_support_support_annotations",
    ],
)

截至 2018 年 5 月:

gmaven_rulesmaven_jar 均已弃用。请不要再使用它们。

来自 docs.bazel.build 上的文档,使用其他相关说明进行了编辑:

托管在 Maven 中央存储库上的 Maven 依赖项

Use the maven_jar repository rule for Maven dependencies not hosted on Google Maven. For example, to use Volley 1.1.0 and Gson 2.6.2, add the following lines to the WORKSPACE file at the top level of the project directory:

maven_jar(
    name = "com_android_volley_volley",
    artifact = "com.android.volley:volley:1.1.0",
)

maven_jar(
    name = "com_google_code_gson",
    artifact = "com.google.code.gson:gson:2.6.2",
)

Then, you can depend on them in your BUILD files:

android_library(
    name = "my_app_lib",
    srcs = [..],
    deps = [
        "@com_android_volley_volley//jar",
        "@com_google_code_gson//jar",
    ],
)

注意maven_jar可传递的,所以它不会下载指定jar的依赖。您将需要为传递依赖项手动指定额外的 maven_jar 规则,或使用 bazel-deps 之类的工具自动生成它们。

托管在 Google Maven 存储库 (https://maven.google.com)

上的 Maven 依赖项

For dependencies hosted on Google's Maven repository, @gmaven_rules provides a simple way to fetch dependencies hosted with gmaven_artifact by specifying the artifact coordinate directly.

To use @gmaven_rules, add these lines to the WORKSPACE file:

# Google Maven Repository
GMAVEN_TAG = "20180513-1"    
http_archive(
    name = "gmaven_rules",
    strip_prefix = "gmaven_rules-%s" % GMAVEN_TAG,
    url = "https://github.com/bazelbuild/gmaven_rules/archive/%s.tar.gz" % GMAVEN_TAG,
)
load("@gmaven_rules//:gmaven.bzl", "gmaven_rules")
gmaven_rules()

Then, load the gmaven_artifact macro at the beginning of your BUILD file to use it:

load("@gmaven_rules//:defs.bzl", "gmaven_artifact")

android_library(
    name = "my_app_lib",
    srcs = glob(["java/**/*.java"]),
    deps = [
        gmaven_artifact("com.android.support:design:aar:27.0.2"),
        gmaven_artifact("com.android.support:support_annotations:jar:27.0.2"),
    ]
    # ...
)

不像maven_jargmaven_artifact 传递的,所以你只需要指定神器的坐标,@gmaven_rules就会解析自动依赖。