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

1

5

Right now I am doing like this

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

inside android_binary. But I am getting this error:

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

Multimillionaire answered 29/5, 2018 at 13:21 Comment(0)
C
11

As of April 2019:

An update: rules_jvm_external is a new ruleset by the Bazel team to fetch and resolve artifacts transitively.

In this case, the WORKSPACE file will contain something like this:

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

Then in the BUILD file, you can directly depend on Volley and Gson like this:

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

As of May 2018:

Both gmaven_rules and maven_jar are deprecated. Please do not use them anymore.

From the documentation on docs.bazel.build, edited with additional relevant instructions:

Maven dependencies hosted on Maven Central Repository

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

Note that maven_jar is not transitive, so it does not download the dependencies of the specified jar. You will need to manually specify additional maven_jar rules for the transitive dependencies, or use a tool like bazel-deps to generate them automatically.

Maven dependencies hosted on Google Maven Repository (https://maven.google.com)

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

Unlike maven_jar, gmaven_artifact is transitive, so you only need to specify the coordinate of the artifact and @gmaven_rules will resolve the dependencies automatically.

Cicada answered 29/5, 2018 at 16:18 Comment(4)
Thank you very much. You are a lifesaver :)Multimillionaire
@Cicada I get Failed to load Starlark extension '@rules_jvm_external//:defs.bzl'. error with bazel 0.25. What am I missing ?Everyday
@Everyday Please open an issue on the rules_jvm_external GitHub repository with more details.Cicada
How do I use the library in my .java file? it says not foundMistrot

© 2022 - 2024 — McMap. All rights reserved.