diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index 927228e14..8ecdd998b 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -197,4 +197,64 @@ tasks:
build_targets:
- "//java/com/basicapp:basic_app"
- "//java/com/basicapp:basic_app_bundle"
-
+ ubuntu2004_databinding:
+ name: "Databinding Basic app ubuntu"
+ platform: ubuntu2004
+ working_directory: examples/databinding
+ build_flags:
+ <<: *rules_flags
+ ? "--@rules_android//rules/flags:databinding_use_androidx"
+ build_targets:
+ - "//java/com/databindingapp:databindingapp"
+ ubuntu2004_databinding_bzlmod:
+ name: "Databinding Basic app ubuntu bzlmod"
+ platform: ubuntu2004
+ working_directory: examples/databinding
+ build_flags:
+ <<: *rules_flags
+ ? "--enable_bzlmod"
+ ? "--enable_workspace=false"
+ ? "--@rules_android//rules/flags:databinding_use_androidx"
+ build_targets:
+ - "//java/com/databindingapp:databindingapp"
+ macos_arm64_databinding:
+ name: "Databinding Basic app mac arm64"
+ platform: macos_arm64
+ working_directory: examples/databinding
+ build_flags:
+ <<: *rules_flags
+ ? "--@rules_android//rules/flags:databinding_use_androidx"
+ build_targets:
+ - "//java/com/databindingapp:databindingapp"
+ macos_arm64_databinding_bzlmod:
+ name: "Databinding Basic app mac arm64 bzlmod"
+ platform: macos_arm64
+ working_directory: examples/databinding
+ build_flags:
+ <<: *rules_flags
+ ? "--enable_bzlmod"
+ ? "--enable_workspace=false"
+ ? "--@rules_android//rules/flags:databinding_use_androidx"
+ build_targets:
+ - "//java/com/databindingapp:databindingapp"
+ # n.b. The below do not build.
+ # windows_databinding_bzlmod:
+ # name: "Databinding Basic app Windows Bzlmod"
+ # platform: windows
+ # working_directory: examples/databinding
+ # build_flags:
+ # <<: *rules_flags
+ # ? "--enable_bzlmod"
+ # ? "--enable_workspace=false"
+ # ? "--@rules_android//rules/flags:databinding_use_androidx"
+ # build_targets:
+ # - "//java/com/databindingapp:databindingapp"
+ # windows_databinding:
+ # name: "Databinding Basic app Windows"
+ # platform: windows
+ # working_directory: examples/databinding
+ # build_flags:
+ # <<: *rules_flags
+ # ? "--@rules_android//rules/flags:databinding_use_androidx"
+ # build_targets:
+ # - "//java/com/databindingapp:databindingapp"
diff --git a/.bazelrc b/.bazelrc
index faa25c257..1c72780d6 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,7 +1,7 @@
common --java_language_version=17
-common --java_runtime_version=17
+common --java_runtime_version=remotejdk_17
common --tool_java_language_version=17
-common --tool_java_runtime_version=17
+common --tool_java_runtime_version=remotejdk_17
# Workaround for a rules_java + bazel < 8.3.0 issue. It should only be relevant
# for bazel@HEAD and rolling releases.
diff --git a/examples/databinding/.bazelrc b/examples/databinding/.bazelrc
new file mode 100644
index 000000000..d381541ae
--- /dev/null
+++ b/examples/databinding/.bazelrc
@@ -0,0 +1,9 @@
+common:core_library_desugaring --desugar_java8_libs
+
+common --java_runtime_version=remotejdk_17
+common --tool_java_runtime_version=remotejdk_17
+
+# Flags to enable mobile-install v3
+mobile-install --mode=skylark --mobile_install_aspect=@rules_android//mobile_install:mi.bzl --mobile_install_supported_rules=android_binary
+# Required to invoke the Studio deployer jar
+mobile-install --tool_java_runtime_version=17
diff --git a/examples/databinding/.gitignore b/examples/databinding/.gitignore
new file mode 100644
index 000000000..63f1fef0e
--- /dev/null
+++ b/examples/databinding/.gitignore
@@ -0,0 +1 @@
+*.lock
diff --git a/examples/databinding/BUILD b/examples/databinding/BUILD
new file mode 100644
index 000000000..a09fce916
--- /dev/null
+++ b/examples/databinding/BUILD
@@ -0,0 +1 @@
+# Empty build file to satisfy gazelle for rules_go.
\ No newline at end of file
diff --git a/examples/databinding/MODULE.bazel b/examples/databinding/MODULE.bazel
new file mode 100644
index 000000000..9ca90efee
--- /dev/null
+++ b/examples/databinding/MODULE.bazel
@@ -0,0 +1,52 @@
+module(
+ name = "databinding",
+)
+
+bazel_dep(name = "rules_java", version = "7.12.2")
+bazel_dep(name = "bazel_skylib", version = "1.3.0")
+bazel_dep(name = "rules_jvm_external", version = "6.6")
+
+bazel_dep(
+ name = "rules_android",
+ version = "0.6.3",
+)
+
+local_path_override(
+ module_name = "rules_android",
+ path = "../../",
+)
+
+remote_android_extensions = use_extension(
+ "@rules_android//bzlmod_extensions:android_extensions.bzl",
+ "remote_android_tools_extensions")
+use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")
+
+android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
+use_repo(android_sdk_repository_extension, "androidsdk")
+
+register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")
+
+
+maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
+maven.install(
+ name = "maven",
+ aar_import_bzl_label = "@rules_android//rules:rules.bzl",
+ artifacts = [
+ "androidx.annotation:annotation-jvm:1.9.1",
+ "androidx.databinding:databinding-adapters:8.7.0",
+ "androidx.databinding:databinding-runtime:8.7.0",
+ ],
+ repositories = [
+ "https://maven.google.com",
+ "https://repo1.maven.org/maven2",
+ "https://repo.gradle.org/gradle/libs-releases",
+ ],
+ # To generate, run:
+ # REPIN=1 bazelisk run --enable_bzlmod @maven//:pin
+ # lock_file = "//:maven_install.json",
+ use_starlark_android_rules = True,
+)
+use_repo(
+ maven,
+ "maven",
+)
diff --git a/examples/databinding/README.md b/examples/databinding/README.md
new file mode 100644
index 000000000..183e122ed
--- /dev/null
+++ b/examples/databinding/README.md
@@ -0,0 +1,8 @@
+To build, ensure that the `ANDROID_HOME` environment variable is set to the path
+to an Android SDK, and run:
+
+```
+bazel build java/com/basicapp:basic_app
+```
+
+See the `.bazelrc` file for flags needed to build the app.
\ No newline at end of file
diff --git a/examples/databinding/WORKSPACE b/examples/databinding/WORKSPACE
new file mode 100644
index 000000000..85827cb9e
--- /dev/null
+++ b/examples/databinding/WORKSPACE
@@ -0,0 +1,60 @@
+local_repository(
+ name = "rules_android",
+ path = "../..", # rules_android's WORKSPACE relative to this inner workspace
+)
+
+# --SNIP--: Everything below this line goes into the example WORKSPACE snippet in the release notes.
+
+# Android rules dependencies
+load("@rules_android//:prereqs.bzl", "rules_android_prereqs")
+rules_android_prereqs()
+
+##### rules_java setup for rules_android #####
+load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
+rules_java_dependencies()
+# note that the following line is what is minimally required from protobuf for the java rules
+# consider using the protobuf_deps() public API from @com_google_protobuf//:protobuf_deps.bzl
+load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility
+proto_bazel_features(name = "proto_bazel_features")
+# register toolchains
+load("@rules_java//java:repositories.bzl", "rules_java_toolchains")
+rules_java_toolchains()
+
+##### rules_jvm_external setup for rules_android #####
+load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
+rules_jvm_external_deps()
+load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
+rules_jvm_external_setup()
+
+load("@rules_jvm_external//:defs.bzl", "maven_install")
+
+maven_install(
+ name = "maven",
+ artifacts = [
+ "androidx.annotation:annotation-jvm:1.9.1",
+ "androidx.databinding:databinding-adapters:8.7.0",
+ "androidx.databinding:databinding-runtime:8.7.0",
+ "com.google.protobuf:protobuf-java:4.27.2",
+ "com.google.protobuf:protobuf-java-util:4.27.2",
+ ],
+ repositories = [
+ "https://maven.google.com",
+ "https://repo1.maven.org/maven2",
+ "https://repo.gradle.org/gradle/libs-releases",
+ ],
+)
+
+##### rules_android setup #####
+load("@rules_android//:defs.bzl", "rules_android_workspace")
+rules_android_workspace()
+
+# Android SDK setup
+load("@rules_android//rules:rules.bzl", "android_sdk_repository")
+android_sdk_repository(
+ name = "androidsdk",
+)
+
+register_toolchains(
+ "@rules_android//toolchains/android:android_default_toolchain",
+ "@rules_android//toolchains/android_sdk:android_sdk_tools",
+)
diff --git a/examples/databinding/WORKSPACE.bzlmod b/examples/databinding/WORKSPACE.bzlmod
new file mode 100644
index 000000000..172da6f27
--- /dev/null
+++ b/examples/databinding/WORKSPACE.bzlmod
@@ -0,0 +1 @@
+workspace(name = "basicapp")
diff --git a/examples/databinding/java/com/databindingapp/AndroidManifest.xml b/examples/databinding/java/com/databindingapp/AndroidManifest.xml
new file mode 100644
index 000000000..19e9358df
--- /dev/null
+++ b/examples/databinding/java/com/databindingapp/AndroidManifest.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/databinding/java/com/databindingapp/BUILD b/examples/databinding/java/com/databindingapp/BUILD
new file mode 100644
index 000000000..9de03a021
--- /dev/null
+++ b/examples/databinding/java/com/databindingapp/BUILD
@@ -0,0 +1,24 @@
+load("@rules_android//android:rules.bzl", "android_binary", "android_library")
+
+android_binary(
+ name = "databindingapp",
+ manifest = "AndroidManifest.xml",
+ enable_data_binding = True,
+ deps = [
+ ":databinding_lib",
+ "@maven//:androidx_databinding_databinding_runtime",
+ ],
+)
+
+android_library(
+ name = "databinding_lib",
+ srcs = ["DataBindingMainActivity.java"],
+ manifest = "AndroidManifest.xml",
+ resource_files = glob(["res/**"]),
+ enable_data_binding = True,
+ deps = [
+ "@maven//:androidx_annotation_annotation_jvm",
+ "@maven//:androidx_databinding_databinding_adapters",
+ "@maven//:androidx_databinding_databinding_runtime",
+ ],
+)
diff --git a/examples/databinding/java/com/databindingapp/DataBindingMainActivity.java b/examples/databinding/java/com/databindingapp/DataBindingMainActivity.java
new file mode 100644
index 000000000..48fe1de0b
--- /dev/null
+++ b/examples/databinding/java/com/databindingapp/DataBindingMainActivity.java
@@ -0,0 +1,51 @@
+// Copyright 2022 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.databindingapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.Menu;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+import com.databindingapp.databinding.ActivityDataBindingBinding;
+
+import java.util.Random;
+
+/**
+ * The main activity of the Basic Sample App.
+ */
+public class DataBindingMainActivity extends Activity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ ActivityDataBindingBinding binding = ActivityDataBindingBinding.inflate(getLayoutInflater());
+
+ Random random = new Random();
+ binding.setShowFizz(random.nextDouble() < 0.5);
+ binding.setShowBuzz(random.nextDouble() < 0.5);
+
+ setContentView(binding.getRoot());
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ // Inflate the menu; this adds items to the action bar if it is present.
+ getMenuInflater().inflate(R.menu.menu, menu);
+ return true;
+ }
+}
diff --git a/examples/databinding/java/com/databindingapp/res/drawable-hdpi/ic_launcher.png b/examples/databinding/java/com/databindingapp/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 000000000..6ab2adde2
Binary files /dev/null and b/examples/databinding/java/com/databindingapp/res/drawable-hdpi/ic_launcher.png differ
diff --git a/examples/databinding/java/com/databindingapp/res/drawable-mdpi/ic_launcher.png b/examples/databinding/java/com/databindingapp/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 000000000..c0a73c33d
Binary files /dev/null and b/examples/databinding/java/com/databindingapp/res/drawable-mdpi/ic_launcher.png differ
diff --git a/examples/databinding/java/com/databindingapp/res/drawable-xhdpi/ic_launcher.png b/examples/databinding/java/com/databindingapp/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 000000000..014b0f106
Binary files /dev/null and b/examples/databinding/java/com/databindingapp/res/drawable-xhdpi/ic_launcher.png differ
diff --git a/examples/databinding/java/com/databindingapp/res/drawable-xxhdpi/ic_launcher.png b/examples/databinding/java/com/databindingapp/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 000000000..20703a15c
Binary files /dev/null and b/examples/databinding/java/com/databindingapp/res/drawable-xxhdpi/ic_launcher.png differ
diff --git a/examples/databinding/java/com/databindingapp/res/layout/activity_data_binding.xml b/examples/databinding/java/com/databindingapp/res/layout/activity_data_binding.xml
new file mode 100644
index 000000000..d4bc445e2
--- /dev/null
+++ b/examples/databinding/java/com/databindingapp/res/layout/activity_data_binding.xml
@@ -0,0 +1,39 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/databinding/java/com/databindingapp/res/menu/menu.xml b/examples/databinding/java/com/databindingapp/res/menu/menu.xml
new file mode 100644
index 000000000..a56bed69f
--- /dev/null
+++ b/examples/databinding/java/com/databindingapp/res/menu/menu.xml
@@ -0,0 +1,8 @@
+
diff --git a/examples/databinding/java/com/databindingapp/res/values/dimens.xml b/examples/databinding/java/com/databindingapp/res/values/dimens.xml
new file mode 100644
index 000000000..47c822467
--- /dev/null
+++ b/examples/databinding/java/com/databindingapp/res/values/dimens.xml
@@ -0,0 +1,5 @@
+
+
+ 16dp
+ 16dp
+
diff --git a/examples/databinding/java/com/databindingapp/res/values/strings.xml b/examples/databinding/java/com/databindingapp/res/values/strings.xml
new file mode 100644
index 000000000..38e8d58d8
--- /dev/null
+++ b/examples/databinding/java/com/databindingapp/res/values/strings.xml
@@ -0,0 +1,8 @@
+
+
+
+ Data Binding App
+ Hello world!
+ Settings
+
+
diff --git a/rules/BUILD b/rules/BUILD
index b0d524b10..918fc1e07 100644
--- a/rules/BUILD
+++ b/rules/BUILD
@@ -3,7 +3,8 @@ load("@bazel_skylib//rules:common_settings.bzl", "int_setting")
load("@rules_java//java:defs.bzl", "java_library")
exports_files([
- "data_binding_annotation_template.txt",
+ "data_binding_annotation_template_androidx.txt",
+ "data_binding_annotation_template_support_lib.txt",
"res_v3_dummy_AndroidManifest.xml",
"res_v3_dummy_R.txt",
"robolectric_properties_template.txt",
diff --git a/rules/aar_import/impl.bzl b/rules/aar_import/impl.bzl
index e3da4dadd..e32dfa362 100644
--- a/rules/aar_import/impl.bzl
+++ b/rules/aar_import/impl.bzl
@@ -96,17 +96,19 @@ def _extract_resources(
ctx,
out_resources_dir,
out_assets_dir,
+ out_databinding_setter_store_dir,
aar,
aar_resources_extractor_tool):
args = ctx.actions.args()
args.add("--input_aar", aar)
args.add("--output_res_dir", out_resources_dir.path)
args.add("--output_assets_dir", out_assets_dir.path)
+ args.add("--output_databinding_setter_store_dir", out_databinding_setter_store_dir.path)
ctx.actions.run(
executable = aar_resources_extractor_tool,
arguments = [args],
inputs = [aar],
- outputs = [out_resources_dir, out_assets_dir],
+ outputs = [out_resources_dir, out_assets_dir, out_databinding_setter_store_dir],
mnemonic = "AarResourcesExtractor",
progress_message = "Extracting resources and assets from %s" % aar.basename,
toolchain = None,
@@ -143,10 +145,12 @@ def _process_resources(
# Extract resources and assets, if they exist.
resources = _create_aar_tree_artifact(ctx, "resources")
assets = _create_aar_tree_artifact(ctx, "assets")
+ data_binding_setter_store = _create_aar_tree_artifact(ctx, "data_binding_setter_store")
_extract_resources(
ctx,
resources,
assets,
+ data_binding_setter_store,
aar,
aar_resources_extractor_tool,
)
@@ -157,6 +161,7 @@ def _process_resources(
assets = [assets],
assets_dir = assets.path,
resource_files = [resources],
+ data_binding_setter_store = data_binding_setter_store,
stamp_manifest = False,
deps = ctx.attr.deps,
exports = ctx.attr.exports,
diff --git a/rules/android_binary/attrs.bzl b/rules/android_binary/attrs.bzl
index aac6bf063..81f521769 100644
--- a/rules/android_binary/attrs.bzl
+++ b/rules/android_binary/attrs.bzl
@@ -250,6 +250,9 @@ ATTRS = _attrs.replace(
Allow for the optimizer to process resources. This is not supported in proguard.
""",
),
+ _databinding_use_androidx = attr.label(
+ default = "//rules/flags:databinding_use_androidx",
+ ),
),
_attrs.compilation_attributes(apply_android_transition = True),
_attrs.DATA_CONTEXT,
diff --git a/rules/android_binary/impl.bzl b/rules/android_binary/impl.bzl
index d567a900e..1db7cdca5 100644
--- a/rules/android_binary/impl.bzl
+++ b/rules/android_binary/impl.bzl
@@ -189,6 +189,13 @@ def _process_proto(_unused_ctx, **_unused_ctxs):
def _process_data_binding(ctx, java_package, packaged_resources_ctx, **_unused_ctxs):
if ctx.attr.enable_data_binding and not acls.in_databinding_allowed(str(ctx.label)):
fail("This target is not allowed to use databinding and enable_data_binding is True.")
+
+ if ctx.attr._databinding_use_androidx[BuildSettingInfo].value:
+ template = get_android_toolchain(ctx).data_binding_annotation_template_androidx
+ else:
+ template = get_android_toolchain(ctx).data_binding_annotation_template_support_lib
+ data_binding_annotation_template = utils.only(template.files.to_list())
+
return ProviderInfo(
name = "db_ctx",
value = data_binding.process(
@@ -202,8 +209,7 @@ def _process_data_binding(ctx, java_package, packaged_resources_ctx, **_unused_c
data_binding_exec = get_android_toolchain(ctx).data_binding_exec.files_to_run,
data_binding_annotation_processor =
get_android_toolchain(ctx).data_binding_annotation_processor[JavaPluginInfo],
- data_binding_annotation_template =
- utils.only(get_android_toolchain(ctx).data_binding_annotation_template.files.to_list()),
+ data_binding_annotation_template = data_binding_annotation_template,
),
)
diff --git a/rules/android_library/attrs.bzl b/rules/android_library/attrs.bzl
index 00e02137f..97363cdd2 100644
--- a/rules/android_library/attrs.bzl
+++ b/rules/android_library/attrs.bzl
@@ -243,6 +243,9 @@ ATTRS = _attrs.add(
_aidl_lib = attr.label(
default = Label("//rules:aidl_lib"),
),
+ _databinding_use_androidx = attr.label(
+ default = "//rules/flags:databinding_use_androidx",
+ ),
),
_attrs.compilation_attributes(),
_attrs.DATA_CONTEXT,
diff --git a/rules/android_library/impl.bzl b/rules/android_library/impl.bzl
index 3d4497a43..dbe330be8 100644
--- a/rules/android_library/impl.bzl
+++ b/rules/android_library/impl.bzl
@@ -33,6 +33,7 @@ load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
load("//rules/flags:flags.bzl", _flags = "flags")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
+load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
load("@rules_java//java/common:java_plugin_info.bzl", "JavaPluginInfo")
load("@rules_java//java/common:proguard_spec_info.bzl", "ProguardSpecInfo")
@@ -237,6 +238,13 @@ def _process_idl(ctx, **unused_sub_ctxs):
def _process_data_binding(ctx, java_package, resources_ctx, **unused_sub_ctxs):
if ctx.attr.enable_data_binding and not acls.in_databinding_allowed(str(ctx.label)):
fail("This target is not allowed to use databinding and enable_data_binding is True.")
+
+ if ctx.attr._databinding_use_androidx[BuildSettingInfo].value:
+ template = get_android_toolchain(ctx).data_binding_annotation_template_androidx
+ else:
+ template = get_android_toolchain(ctx).data_binding_annotation_template_support_lib
+ data_binding_annotation_template = utils.only(template.files.to_list())
+
return ProviderInfo(
name = "db_ctx",
value = _data_binding.process(
@@ -250,8 +258,7 @@ def _process_data_binding(ctx, java_package, resources_ctx, **unused_sub_ctxs):
data_binding_exec = get_android_toolchain(ctx).data_binding_exec.files_to_run,
data_binding_annotation_processor =
get_android_toolchain(ctx).data_binding_annotation_processor,
- data_binding_annotation_template =
- utils.only(get_android_toolchain(ctx).data_binding_annotation_template.files.to_list()),
+ data_binding_annotation_template = data_binding_annotation_template,
),
)
diff --git a/rules/busybox.bzl b/rules/busybox.bzl
index 164df4fdc..b69cde653 100644
--- a/rules/busybox.bzl
+++ b/rules/busybox.bzl
@@ -13,6 +13,7 @@
# limitations under the License.
"""Bazel ResourcesBusyBox Commands."""
+load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
load(":java.bzl", _java = "java")
@@ -984,6 +985,7 @@ def _process_databinding(
args.add_all(res_dirs, before_each = "--resource_root")
args.add("--dataBindingInfoOut", out_databinding_info)
args.add("--appId", java_package)
+ args.add("--useDataBindingAndroidX", ctx.attr._databinding_use_androidx[BuildSettingInfo].value)
_set_warning_level(ctx, args)
diff --git a/rules/data_binding.bzl b/rules/data_binding.bzl
index 9039cba35..187097c41 100644
--- a/rules/data_binding.bzl
+++ b/rules/data_binding.bzl
@@ -13,6 +13,7 @@
# limitations under the License.
"""Bazel Android Data Binding."""
+load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//providers:providers.bzl", "DataBindingV2Info")
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
load(":utils.bzl", "ANDROID_TOOLCHAIN_TYPE", _utils = "utils")
@@ -54,7 +55,7 @@ def _copy_annotation_file(ctx, output_dir, annotation_template):
annotation_out = ctx.actions.declare_file(
output_dir + "/android/databinding/layouts/DataBindingInfo.java",
)
- _utils.copy_file(ctx, annotation_template, annotation_out)
+ ctx.actions.symlink(output = annotation_out, target_file = annotation_template)
return annotation_out
def _gen_sources(ctx, output_dir, java_package, deps, layout_info, data_binding_exec):
@@ -67,7 +68,7 @@ def _gen_sources(ctx, output_dir, java_package, deps, layout_info, data_binding_
args.add("-classInfoOut", class_info)
args.add("-sourceOut", srcjar)
args.add("-zipSourceOutput", "true")
- args.add("-useAndroidX", "false")
+ args.add("-useAndroidX", ctx.attr._databinding_use_androidx[BuildSettingInfo].value)
if deps:
if type(deps[0].class_infos) == "depset":
@@ -117,9 +118,15 @@ def _setup_dependent_lib_artifacts(ctx, output_dir, deps):
path = artifact.short_path
if path.startswith("../"):
path = path[3:]
- dep_lib_artifact = ctx.actions.declare_file(
- output_dir + "dependent-lib-artifacts/" + path,
- )
+ dep_lib_artifact_path = output_dir + "dependent-lib-artifacts/" + path
+ if artifact.is_directory:
+ dep_lib_artifact = ctx.actions.declare_directory(
+ output_dir + "dependent-lib-artifacts/" + path,
+ )
+ else:
+ dep_lib_artifact = ctx.actions.declare_file(
+ output_dir + "dependent-lib-artifacts/" + path,
+ )
# Copy file to a location required by the DataBinding annotation
# processor.
@@ -254,11 +261,11 @@ def _process(
if defines_resources:
# Outputs of the Data Binding annotation processor.
br_out = ctx.actions.declare_file(
- output_dir + "bin-files/%s-br.bin" % java_package,
+ output_dir + "bin-files/%s--br.bin" % java_package,
)
db_info[_JAVA_ANNOTATION_PROCESSOR_ADDITIONAL_OUTPUTS].append(br_out)
setter_store_out = ctx.actions.declare_file(
- output_dir + "bin-files/%s-setter_store.json" % java_package,
+ output_dir + "bin-files/%s--setter_store.json" % java_package,
)
db_info[_JAVA_ANNOTATION_PROCESSOR_ADDITIONAL_OUTPUTS].append(
setter_store_out,
@@ -321,6 +328,19 @@ def _process(
return DataBindingContextInfo(**db_info)
+def _process_aar(
+ ctx,
+ data_binding_setter_store):
+ """Builds a data binding provider for an AAR's extracted setter store
+ """
+ return DataBindingV2Info(
+ setter_stores = depset(direct = [data_binding_setter_store]),
+ class_infos = depset(),
+ transitive_br_files = depset(),
+ java_packages = [],
+ )
+
data_binding = struct(
process = _process,
+ process_aar = _process_aar,
)
diff --git a/rules/data_binding_annotation_template_androidx.txt b/rules/data_binding_annotation_template_androidx.txt
new file mode 100644
index 000000000..6625f5254
--- /dev/null
+++ b/rules/data_binding_annotation_template_androidx.txt
@@ -0,0 +1,13 @@
+package android.databinding.layouts;
+
+import androidx.databinding.BindingBuildInfo;
+
+/**
+ * Template for the file that feeds data binding's annotation processor. The
+ * processor reads the values set here to generate .java files that link XML
+ * data binding declarations (from layoutInfoDir) to app code.
+ */
+@BindingBuildInfo
+public class DataBindingInfo {
+ /* This only exists for annotation processing. */
+}
diff --git a/rules/data_binding_annotation_template.txt b/rules/data_binding_annotation_template_support_lib.txt
similarity index 100%
rename from rules/data_binding_annotation_template.txt
rename to rules/data_binding_annotation_template_support_lib.txt
diff --git a/rules/flags/additional_flags.bzl b/rules/flags/additional_flags.bzl
index 687ff9f00..c7481021d 100644
--- a/rules/flags/additional_flags.bzl
+++ b/rules/flags/additional_flags.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""Additional flag definitions."""
-load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
+load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
def additional_flags():
@@ -28,4 +28,10 @@ def additional_flags():
"dependency",
],
visibility = ["//visibility:public"],
- )
\ No newline at end of file
+ )
+
+ bool_flag(
+ name = "databinding_use_androidx",
+ build_setting_default = False,
+ visibility = ["//visibility:public"],
+ )
diff --git a/rules/resources.bzl b/rules/resources.bzl
index f266a9841..ca8266a6c 100644
--- a/rules/resources.bzl
+++ b/rules/resources.bzl
@@ -16,6 +16,7 @@
load("//providers:providers.bzl", "AndroidLibraryResourceClassJarProvider", "ResourcesNodeInfo", "StarlarkAndroidResourcesInfo")
load("//rules:acls.bzl", "acls")
load("//rules:add_constraints.bzl", "add_constraints")
+load("//rules:data_binding.bzl", _data_binding = "data_binding")
load("//rules:min_sdk_version.bzl", _min_sdk_version = "min_sdk_version")
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
@@ -378,8 +379,7 @@ OUT_DIR=$(mktemp -d)
CUR_PWD=$(pwd)
if zipinfo -t "$1"; then
- # Use awk instead of 'head -n -2' for macOS compatibility (BSD head doesn't support negative counts)
- ORDERED_LIST=`(unzip -l "$1" | sed -e '1,3d' | awk '{lines[NR]=$0} END{for(i=1;i<=NR-2;i++) print lines[i]}' | tr -s " " | cut -d " " -f5)`
+ ORDERED_LIST=`(zipinfo -1 "$1" | sort)`
unzip -q "$1" -d "$IN_DIR"
@@ -389,7 +389,7 @@ if zipinfo -t "$1"; then
cd "$IN_DIR"
if [ -f "$FILE" ]; then
# Use sed with backup extension for macOS compatibility, then remove backup
- sed -i.bak 's/Databinding\\-processed\\-resources/databinding\\-processed\\-resources/g' "$FILE" && rm -f "$FILE.bak"
+ LC_ALL=C sed -i.bak 's/Databinding\\-processed\\-resources/databinding\\-processed\\-resources/g' "$FILE" && rm -f "$FILE.bak"
NEW_NAME=`echo "$FILE" | sed 's/Databinding\\-processed\\-resources/databinding\\-processed\\-resources/g' | sed 's#'"$IN_DIR"'/##g'`
mkdir -p `dirname "$OUT_DIR/$NEW_NAME"` && touch "$OUT_DIR/$NEW_NAME"
cp -p "$FILE" "$OUT_DIR/$NEW_NAME"
@@ -1200,6 +1200,7 @@ def _process_starlark(
resource_files = None,
neverlink = False,
enable_data_binding = False,
+ data_binding_setter_store = None,
fix_resource_transitivity = False,
aapt = None,
android_jar = None,
@@ -1380,6 +1381,12 @@ def _process_starlark(
data_binding_layout_info = None
processed_resources = resource_files
processed_manifest = None
+
+ if data_binding_setter_store != None:
+ resources_ctx[_PROVIDERS].append(
+ _data_binding.process_aar(ctx, data_binding_setter_store)
+ )
+
if not defines_resources:
if aapt:
# Generate an empty manifest with the right package
diff --git a/rules/utils.bzl b/rules/utils.bzl
index d0f5b555d..2e4d3c080 100644
--- a/rules/utils.bzl
+++ b/rules/utils.bzl
@@ -212,7 +212,7 @@ def _copy_file(ctx, src, dest):
if src.is_directory or dest.is_directory:
fail("Cannot use copy_file with directories")
ctx.actions.run_shell(
- command = "cp --reflink=auto $1 $2",
+ command = "cp $1 $2",
arguments = [src.path, dest.path],
inputs = [src],
outputs = [dest],
@@ -224,7 +224,7 @@ def _copy_dir(ctx, src, dest):
if not src.is_directory:
fail("copy_dir src must be a directory")
ctx.actions.run_shell(
- command = "cp -r --reflink=auto $1 $2",
+ command = "cp -r $1 $2",
arguments = [src.path, dest.path],
inputs = [src],
outputs = [dest],
diff --git a/src/tools/java/com/google/devtools/build/android/AndroidResourceProcessor.java b/src/tools/java/com/google/devtools/build/android/AndroidResourceProcessor.java
index b0f0d4b45..830bfa372 100644
--- a/src/tools/java/com/google/devtools/build/android/AndroidResourceProcessor.java
+++ b/src/tools/java/com/google/devtools/build/android/AndroidResourceProcessor.java
@@ -305,7 +305,7 @@ static Path processDataBindings(
ProcessXmlOptionsWrapper options = new ProcessXmlOptionsWrapper();
options.setAppId(packagePath);
- options.setResInput(inputResourcesDir.toFile());
+ options.setResInput(inputResourcesDir.toFile().getAbsoluteFile());
options.setResOutput(processedResourceDir.toFile());
options.setLayoutInfoOutput(dataBindingInfoOut.toFile());
options.setUseAndroidX(useDataBindingAndroidX);
diff --git a/src/tools/java/com/google/devtools/build/android/BUILD b/src/tools/java/com/google/devtools/build/android/BUILD
index fd3282074..8d01f17ca 100644
--- a/src/tools/java/com/google/devtools/build/android/BUILD
+++ b/src/tools/java/com/google/devtools/build/android/BUILD
@@ -41,6 +41,21 @@ run_singlejar(
out = "databinding_exec.jar",
include_prefixes = [
"android/databinding/",
+ "androidx/databinding/",
+ "api-versions.xml",
+ "com/android/tools/build/jetifier",
+ "com/google/auto",
+ "com/google/common/base/",
+ "com/google/common/collect/",
+ "com/google/common/primitives/",
+ "com/google/gson/",
+ "com/squareup/javapoet/",
+ "com/sun/istack/",
+ "com/sun/xml/",
+ "default.generated.config",
+ "javax/activation/",
+ "javax/xml/",
+ "kotlin/",
"org/antlr/",
"org/apache/commons/",
"org/mozilla",
diff --git a/toolchains/android/toolchain.bzl b/toolchains/android/toolchain.bzl
index 98fb7773e..e2b9dc71b 100644
--- a/toolchains/android/toolchain.bzl
+++ b/toolchains/android/toolchain.bzl
@@ -118,8 +118,12 @@ _ATTRS = dict(
cfg = "exec",
default = "//tools/android:compiler_annotation_processor",
),
- data_binding_annotation_template = attr.label(
- default = "//rules:data_binding_annotation_template.txt",
+ data_binding_annotation_template_androidx = attr.label(
+ default = "//rules:data_binding_annotation_template_androidx.txt",
+ allow_files = True,
+ ),
+ data_binding_annotation_template_support_lib = attr.label(
+ default = "//rules:data_binding_annotation_template_support_lib.txt",
allow_files = True,
),
data_binding_exec = attr.label(
diff --git a/tools/android/BUILD b/tools/android/BUILD
index b1b76ecf1..8f053eb76 100644
--- a/tools/android/BUILD
+++ b/tools/android/BUILD
@@ -61,7 +61,9 @@ java_plugin(
processor_class = "android.databinding.annotationprocessor.ProcessDataBinding",
visibility = ["//visibility:public"],
deps = [
+ "//src/tools/java/com/google/devtools/build/android:android_common_30_1_3",
"//src/tools/java/com/google/devtools/build/android:databinding_exec_jar",
+ "@rules_android_maven//:com_beust_jcommander",
],
)
@@ -304,7 +306,9 @@ java_binary(
main_class = "android.databinding.AndroidDataBinding",
visibility = ["//visibility:public"],
runtime_deps = [
+ "//src/tools/java/com/google/devtools/build/android:android_common_30_1_3",
"//src/tools/java/com/google/devtools/build/android:databinding_exec_jar",
+ "@rules_android_maven//:com_beust_jcommander",
],
)