Skip to content

Commit ba7310c

Browse files
authored
Update rules_jvm_external to use the Starlark version of aar_import after the native version was removed from Bazel (#1149)
* Update rules_jvm_external to use the Starlark version of aar_import automatically if the version of Bazel used does not contain the native version of aar_import. This also updates the version of rules_android to the latest commit, because version 0.1.1 is basically just wrappers around the native versions of the rules. This also disbles `--experimental_sibling_repository_layout` added in b6631f9 because the latest rules_android uses protos in go, and there is a problem compiling protos in go with `--experimental_sibling_repository_layout`: bazel-contrib/rules_go#3947 Fixes #1139. * Restore .bazelrc * Don't overwrite bazelrc * Update rules_android commit to inclue bazelbuild/rules_android@d25741d * Update comments * Update to latest rules_android commit * Load the rules_android rules before rules_kotlin to avoid the version of rules_android that rules_kotlin uses * Update .bazelversion from 7.1.0 to 7.2.1 * Add dependencies for "aar_import_that_consumes_the_downloaded_file_directly" because Starlark aar_import checks that there are no missing class imports in aars * Workarounds for aar_import ImportDepsChecker checks * Remove unnecessary com.google.ar.sceneform:assets:1.10.0 dep and revert regression_testing_coursier_install.json * Use the native Android rules in WORKSPACE via rules_android 0.1.1 for use with bazel 5 and bazel 6 in CI * Update to latest rules_android * Update to latest rules_android * Update to rules_android 0.5.1, address review comments
1 parent 1478dc6 commit ba7310c

File tree

9 files changed

+70
-16
lines changed

9 files changed

+70
-16
lines changed

.bazelrc

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ build --tool_java_runtime_version=remotejdk_11
77
build --experimental_strict_java_deps=strict
88
build --explicit_java_test_deps
99

10-
build --experimental_sibling_repository_layout
10+
# Re-enable once https://github.com/bazelbuild/rules_go/issues/3947 is addressed.
11+
#build --experimental_sibling_repository_layout
12+
13+
# Remove once https://github.com/bazelbuild/rules_android/issues/219 is fixed
14+
build --experimental_google_legacy_api
1115

1216
# Make sure we get something helpful when tests fail
1317
test --verbose_failures

.bazelversion

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.1.0
1+
7.2.1

MODULE.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bazel_dep(
2626
)
2727
bazel_dep(
2828
name = "rules_android",
29-
version = "0.1.1",
29+
version = "0.5.1",
3030
)
3131
bazel_dep(
3232
name = "stardoc",

WORKSPACE

+15-8
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,10 @@ maven_install(
519519

520520
maven_install(
521521
name = "starlark_aar_import_with_sources_test",
522-
# Not actually necessary since this is the default value, but useful for
523-
# testing.
524-
aar_import_bzl_label = "@build_bazel_rules_android//android:rules.bzl",
522+
# The default is "@rules_android//rules:rules.bzl" but use
523+
# "@rules_android//android:rules.bzl" with the older 0.1.1 release
524+
# to use the native rules.
525+
aar_import_bzl_label = "@rules_android//android:rules.bzl",
525526
artifacts = [
526527
"androidx.work:work-runtime:2.6.0",
527528
],
@@ -535,9 +536,10 @@ maven_install(
535536

536537
maven_install(
537538
name = "starlark_aar_import_test",
538-
# Not actually necessary since this is the default value, but useful for
539-
# testing.
540-
aar_import_bzl_label = "@build_bazel_rules_android//android:rules.bzl",
539+
# The default is "@rules_android//rules:rules.bzl" but use
540+
# "@rules_android//android:rules.bzl" with the older 0.1.1 release
541+
# to use the native rules.
542+
aar_import_bzl_label = "@rules_android//android:rules.bzl",
541543
artifacts = [
542544
"com.android.support:appcompat-v7:28.0.0",
543545
],
@@ -550,9 +552,14 @@ maven_install(
550552
)
551553

552554
# for the above "starlark_aar_import_test" maven_install with
553-
# use_starlark_android_rules = True
555+
# use_starlark_android_rules = True.
556+
# Note that this version is different from the version in MODULE.bazel
557+
# because the latest versions of rules_android do not support Bazel 5 or 6,
558+
# which rules_jvm_external supports and uses in CI tests. So use
559+
# rules_android 0.1.1, which are wrappers around the native Android rules,
560+
# since the tests with Bazel 5 and 6 do no use bzlmod.
554561
http_archive(
555-
name = "build_bazel_rules_android",
562+
name = "rules_android",
556563
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
557564
strip_prefix = "rules_android-0.1.1",
558565
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],

private/rules/coursier.bzl

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bzl_library(
5454
)
5555
"""
5656

57-
DEFAULT_AAR_IMPORT_LABEL = "@build_bazel_rules_android//android:rules.bzl"
57+
DEFAULT_AAR_IMPORT_LABEL = "@rules_android//rules:rules.bzl"
5858

5959
_AAR_IMPORT_STATEMENT = """\
6060
load("%s", "aar_import")
@@ -245,7 +245,9 @@ def _relativize_and_symlink_file_in_maven_local(repository_ctx, absolute_path):
245245
return artifact_relative_path
246246

247247
def _get_aar_import_statement_or_empty_str(repository_ctx):
248-
if repository_ctx.attr.use_starlark_android_rules:
248+
# Use the Starlark version of aar_import if requested, or if this version of Bazel
249+
# does not have native aar_import.
250+
if repository_ctx.attr.use_starlark_android_rules or not hasattr(native, "aar_import"):
249251
# parse the label to validate it
250252
_ = Label(repository_ctx.attr.aar_import_bzl_label)
251253
return _AAR_IMPORT_STATEMENT % repository_ctx.attr.aar_import_bzl_label

private/rules/maven_install.bzl

+9-3
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ def maven_install(
6868
use_credentials_from_home_netrc_file: Whether to pass machine login credentials from the ~/.netrc file to coursier.
6969
fail_if_repin_required: Whether to fail the build if the required maven artifacts have been changed but not repinned. Requires the `maven_install_json` to have been set.
7070
use_starlark_android_rules: Whether to use the native or Starlark version
71-
of the Android rules. Default is False.
71+
of the Android rules. Default is False if the running version of Bazel supports native aar_import.
72+
If the running version of Bazel does not support native aar_import, this parameter is ignored and the
73+
Starlark Android rules is used.
7274
aar_import_bzl_label: The label (as a string) to use to import aar_import
7375
from. This is usually needed only if the top-level workspace file does
7476
not use the typical default repository name to import the Android
75-
Starlark rules. Default is
76-
"@build_bazel_rules_android//rules:rules.bzl".
77+
Starlark rules. Default is "@rules_android//rules:rules.bzl".
7778
duplicate_version_warning: What to do if an artifact is specified multiple times. If "error" then
7879
fail the build, if "warn" then print a message and continue, if "none" then do nothing. The default
7980
is "warn".
@@ -106,6 +107,11 @@ def maven_install(
106107
if additional_netrc_lines and maven_install_json == None:
107108
fail("`additional_netrc_lines` is only supported with `maven_install_json` specified", "additional_netrc_lines")
108109

110+
if not hasattr(native, "aar_import"):
111+
# If this version of bazel does not have the native version of
112+
# aar_import, then the Starlark version of aar_import must be used.
113+
use_starlark_android_rules = True
114+
109115
# The first coursier_fetch generates the @unpinned_maven
110116
# repository, which executes Coursier.
111117
#

tests/integration/override_targets/BUILD

+10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ aar_import(
3030
],
3131
visibility = ["@regression_testing_coursier//:__subpackages__"],
3232
deps = [
33+
# This is a hack to get past ImportDepsChecker because
34+
# apparently the Load class is nowhere to be found in any
35+
# sceneform AARs or deps.
36+
":fake_loader",
3337
# Add the missing dependencies
38+
"@regression_testing_coursier//:com_android_support_support_annotations",
3439
"@regression_testing_coursier//:com_google_ar_core",
3540
"@regression_testing_coursier//:com_google_ar_sceneform_sceneform_base",
3641
"@regression_testing_coursier//:com_google_ar_sceneform_filament_android",
@@ -68,3 +73,8 @@ sh_test(
6873
"@bazel_tools//tools/bash/runfiles",
6974
],
7075
)
76+
77+
java_library(
78+
name = "fake_loader",
79+
srcs = ["Loader.java"],
80+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.google.ar.sceneform.assets;
2+
3+
/**
4+
* Some class in com.google.ar.sceneform:rendering:aar:1.10.0 loads
5+
* this class, but there is no class "Loader" in
6+
* com.google.ar.sceneform:assets:1.10.0 or any other aar in sceneform.
7+
* This is only to satisfy Starlark aar_import's ImportDepsChecker
8+
* check. None of this code is actually run.
9+
*/
10+
public class Loader {
11+
public static boolean loadUnifiedJni() {
12+
return false;
13+
}
14+
}

tests/unit/aar_import/BUILD

+11
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,23 @@
1313
# limitations under the License.
1414

1515
load("@bazel_skylib//rules:build_test.bzl", "build_test")
16+
load("@rules_android//android:rules.bzl", "aar_import")
1617
load(":aar_import_test.bzl", "aar_import_test_suite")
1718

1819
aar_import(
1920
name = "aar_import_that_consumes_the_downloaded_file_directly",
2021
# Will produce an error if the downloaded file does not have the `.aar` file extension
2122
aar = "@com_android_support_appcompat_v7_aar_28_0_0//file:file",
23+
deps = [
24+
"@starlark_aar_import_test//:com_android_support_animated_vector_drawable",
25+
"@starlark_aar_import_test//:com_android_support_collections",
26+
"@starlark_aar_import_test//:com_android_support_cursoradapter",
27+
"@starlark_aar_import_test//:com_android_support_support_annotations",
28+
"@starlark_aar_import_test//:com_android_support_support_compat",
29+
"@starlark_aar_import_test//:com_android_support_support_core_utils",
30+
"@starlark_aar_import_test//:com_android_support_support_fragment",
31+
"@starlark_aar_import_test//:com_android_support_support_vector_drawable",
32+
],
2233
)
2334

2435
build_test(

0 commit comments

Comments
 (0)