我现在是这样做的
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 团队的新规则集,用于传递地获取和解析 Artifact 。
在这种情况下,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_rules
和 maven_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 theWORKSPACE
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 withgmaven_artifact
by specifying the artifact coordinate directly.To use
@gmaven_rules
, add these lines to theWORKSPACE
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_jar
不同,gmaven_artifact
是可传递的,所以你只需要指定artifact的坐标和@gmaven_rules
将自动解决依赖关系。
关于java - 如何在 Bazel 中为 Android 项目添加 Maven 依赖项(例如 Volley、Gson)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50585493/