Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 307
Abhijeet Kumar Avatar asked May 29 '18 13:05

Abhijeet Kumar


People also ask

Does Bazel use Maven?

You can run Maven and Bazel in the same repository. Note: While Bazel supports downloading and publishing Maven artifacts with rules_jvm_external, it does not directly support Maven-based plugins. Maven plugins can't be directly run by Bazel since there's no Maven compatibility layer.

How do I add a Google Maven repository?

Google's Maven repository can be accessed from https://maven.google.com (an alternative URL is https://dl.google.com/dl/android/maven2/). If you are using Gradle 4.1 or higher, you can use it by adding google() to your repositories configuration in your build. gradle file.

What is Android Maven?

The Android Maven Plugin is used to build applications for the Android operating system as well as build libraries to be used in these efforts in AAR and the legacy APKLIB format using Apache Maven. The plugin includes numerous features with some minimal setup for usage.


1 Answers

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.

like image 150
Jin Avatar answered Nov 14 '22 22:11

Jin