From c5f7f7a1be88fe1ff1c2a79a965fa84de2ffa8c4 Mon Sep 17 00:00:00 2001 From: Prasanna Swaminathan Date: Thu, 26 Dec 2024 15:47:27 -0500 Subject: [PATCH 1/6] [databinding] Use symlinks for databinding copy_{file,dir} functions As illustrated in #223, BSD `cp` does not support `--reflink=auto` flag. These functions are only used in the data binding rules, so we can actually just remove them. Instead, we can use symlinks in the data binding actions. n.b. Solely replacing the calls with symlinks and leaving the originals is something I'm not entirely opposed to. I do think it is a bad idea, however, because it is not cross-platform-compatible. It leaves the door open for future usages to call these copy functions and break things downstream for non-Linux users. However, if reverting utils.bzl is the only way to get this merged in, I'll do it. stacked-branch: databinding-symlinks --- rules/data_binding.bzl | 14 ++++++++++---- rules/utils.bzl | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/rules/data_binding.bzl b/rules/data_binding.bzl index 9039cba35..3dbc1dded 100644 --- a/rules/data_binding.bzl +++ b/rules/data_binding.bzl @@ -54,7 +54,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): @@ -117,9 +117,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. 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], From cb8a30a45c250c509c7220b9307d24236252aea1 Mon Sep 17 00:00:00 2001 From: Prasanna Swaminathan Date: Thu, 19 Feb 2026 10:44:24 -0500 Subject: [PATCH 2/6] [databinding] Include other necessary prefixes in databinding_exec.jar Without these prefixes, `StarlarkProcessDatabinding` fails with errors about missing classes. These are all embedded in android_tools.jar, but don't get pulled into the final jar. ``` ERROR: /Users/p/Code/bazel/rules_android/examples/databinding/java/com/basicapp/BUILD:10:16: Processing data binding failed: (Exit 1): java failed: error executing StarlarkProcessDatabinding command (from target //java/com/basicapp:basic_lib) (cd /private/var/tmp/_bazel_p/828537623772ec0b3f803c69d3e68e34/sandbox/darwin-sandbox/211/execroot/_main && \ exec env - \ external/rules_java~~toolchains~remotejdk11_macos_aarch64/bin/java -Xms4G -Xmx4G -XX:+ExitOnOutOfMemoryError -jar bazel-out/darwin_arm64-opt-exec-ST-d57f47055a04/bin/external/rules_android~/src/tools/java/com/google/devtools/build/android/ResourceProcessorBusyBox_deploy.jar --tool PROCESS_DATABINDING -- --output_resource_directory bazel-out/darwin_arm64-fastbuild-android-ST-80dd4ae33d52/bin/java/com/basicapp/Databinding-processed-resources/basic_lib --resource_root java/com/basicapp/res --dataBindingInfoOut bazel-out/darwin_arm64-fastbuild-android-ST-80dd4ae33d52/bin/java/com/basicapp/databinding/basic_lib/layout-info.zip --appId com.basicapp --useDataBindingAndroidX true) Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging Dec 26, 2024 4:38:32 PM com.google.devtools.build.android.ResourceProcessorBusyBox processRequest SEVERE: Error during processing java.lang.RuntimeException: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/ParserRuleContext at com.google.devtools.build.android.AndroidResourceProcessor.processDataBindings(AndroidResourceProcessor.java:318) at com.google.devtools.build.android.AndroidDataBindingProcessingAction.main(AndroidDataBindingProcessingAction.java:113) at com.google.devtools.build.android.ResourceProcessorBusyBox$Tool$13.call(ResourceProcessorBusyBox.java:140) at com.google.devtools.build.android.ResourceProcessorBusyBox.processRequest(ResourceProcessorBusyBox.java:238) at com.google.devtools.build.android.ResourceProcessorBusyBox.main(ResourceProcessorBusyBox.java:175) Caused by: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/ParserRuleContext at android.databinding.tool.LayoutXmlProcessor.(LayoutXmlProcessor.java:52) at android.databinding.AndroidDataBinding.createXmlProcessor(AndroidDataBinding.kt:188) at android.databinding.AndroidDataBinding.processResources(AndroidDataBinding.kt:113) at android.databinding.AndroidDataBinding.doRun(AndroidDataBinding.kt:107) at com.google.devtools.build.android.AndroidDataBindingWrapper.doRun(AndroidDataBindingWrapper.java:33) at com.google.devtools.build.android.AndroidResourceProcessor.processDataBindings(AndroidResourceProcessor.java:316) ... 4 more Caused by: java.lang.ClassNotFoundException: org.antlr.v4.runtime.ParserRuleContext at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527) ... 10 more Exception in thread "main" java.lang.RuntimeException: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/ParserRuleContext at com.google.devtools.build.android.AndroidResourceProcessor.processDataBindings(AndroidResourceProcessor.java:318) at com.google.devtools.build.android.AndroidDataBindingProcessingAction.main(AndroidDataBindingProcessingAction.java:113) at com.google.devtools.build.android.ResourceProcessorBusyBox$Tool$13.call(ResourceProcessorBusyBox.java:140) at com.google.devtools.build.android.ResourceProcessorBusyBox.processRequest(ResourceProcessorBusyBox.java:238) at com.google.devtools.build.android.ResourceProcessorBusyBox.main(ResourceProcessorBusyBox.java:175) Caused by: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/ParserRuleContext at android.databinding.tool.LayoutXmlProcessor.(LayoutXmlProcessor.java:52) at android.databinding.AndroidDataBinding.createXmlProcessor(AndroidDataBinding.kt:188) at android.databinding.AndroidDataBinding.processResources(AndroidDataBinding.kt:113) at android.databinding.AndroidDataBinding.doRun(AndroidDataBinding.kt:107) at com.google.devtools.build.android.AndroidDataBindingWrapper.doRun(AndroidDataBindingWrapper.java:33) at com.google.devtools.build.android.AndroidResourceProcessor.processDataBindings(AndroidResourceProcessor.java:316) ... 4 more Caused by: java.lang.ClassNotFoundException: org.antlr.v4.runtime.ParserRuleContext at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527) ... 10 more options ProcessXmlOptions{appId='com.basicapp', resInput=java/com/basicapp/res, resOutput=bazel-out/darwin_arm64-fastbuild-android-ST-80dd4ae33d52/bin/java/com/basicapp/Databinding-processed-resources/basic_lib/java/com/basicapp/res, layoutInfoOutput=/var/folders/mn/gbs17z997jvgrk53m0g2fgrh0000gn/T/android_data_binding_layout_info_tmp3836744348019465023, zipLayoutInfo=false, useAndroidX=true, enableDebugLogs=false} Target //java/com/basicapp:basic_app failed to build ERROR: /Users/p/Code/bazel/rules_android/examples/databinding/java/com/basicapp/BUILD:10:16 Compiling Android Resources in java/com/basicapp/basic_lib_symbols/symbols.zip failed: (Exit 1): java failed: error executing StarlarkProcessDatabinding command (from target //java/com/basicapp:basic_lib) (cd /private/var/tmp/_bazel_p/828537623772ec0b3f803c69d3e68e34/sandbox/darwin-sandbox/211/execroot/_main && \ exec env - \ external/rules_java~~toolchains~remotejdk11_macos_aarch64/bin/java -Xms4G -Xmx4G -XX:+ExitOnOutOfMemoryError -jar bazel-out/darwin_arm64-opt-exec-ST-d57f47055a04/bin/external/rules_android~/src/tools/java/com/google/devtools/build/android/ResourceProcessorBusyBox_deploy.jar --tool PROCESS_DATABINDING -- --output_resource_directory bazel-out/darwin_arm64-fastbuild-android-ST-80dd4ae33d52/bin/java/com/basicapp/Databinding-processed-resources/basic_lib --resource_root java/com/basicapp/res --dataBindingInfoOut bazel-out/darwin_arm64-fastbuild-android-ST-80dd4ae33d52/bin/java/com/basicapp/databinding/basic_lib/layout-info.zip --appId com.basicapp --useDataBindingAndroidX true) ``` stacked-branch: databinding_exec_jar-fixes --- .../java/com/google/devtools/build/android/BUILD | 15 +++++++++++++++ tools/android/BUILD | 4 ++++ 2 files changed, 19 insertions(+) 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/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", ], ) From 1a810a1e875bb2e2b63e5d0a44e6752ab509059f Mon Sep 17 00:00:00 2001 From: Prasanna Swaminathan Date: Mon, 11 May 2026 11:51:54 -0500 Subject: [PATCH 3/6] [databinding] Fix databinding templates and resources for AndroidX This adds a flag for AndroidX and wires it through to the processing steps. Additionally, the shell command run to fix databinding compiled resources was not cross-platform: * BSD `head` won't take a negative int as its argument (GNU does). The better way to accomplish this goal here (listing all files in the archive) is just to use `zipinfo(1)`. `zipinfo -1` shows in its man page to be intended for exactly this purpose. * BSD `sed` requires passsing an argument to the `-i` flag for a file extension to use when writing a back-up. A file without a back-up must be explicitly signaled by using an empty argument, i.e. `sed -i '' file`. However, a spaced argument to `-i` on GNU sed is interpreted as the next positional argument to `sed`, which is an error. The only cross-platform way to accomplish this appears to be to combine it into one like `-i.bak`. This then needs to be removed. (From [StackOverflow](https://stackoverflow.com/a/22084103/1819790)) * Using `sed` to change a string in an otherwise binary file may require changing the localization variables. At least, it does on standard BSD/macOS: otherwise, `sed` assumes files are encoded as text, and the class files contain invalid bytes. `LC_ALL` overrides all categories not set. --- rules/busybox.bzl | 1 + rules/data_binding.bzl | 6 +++--- rules/data_binding_annotation_template.txt | 6 ++---- rules/resources.bzl | 5 ++--- .../devtools/build/android/AndroidResourceProcessor.java | 2 +- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/rules/busybox.bzl b/rules/busybox.bzl index 64f5c8a96..c67254268 100644 --- a/rules/busybox.bzl +++ b/rules/busybox.bzl @@ -1008,6 +1008,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", "true") _set_warning_level(ctx, args) diff --git a/rules/data_binding.bzl b/rules/data_binding.bzl index 3dbc1dded..80f4d656f 100644 --- a/rules/data_binding.bzl +++ b/rules/data_binding.bzl @@ -67,7 +67,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", "true") if deps: if type(deps[0].class_infos) == "depset": @@ -260,11 +260,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, diff --git a/rules/data_binding_annotation_template.txt b/rules/data_binding_annotation_template.txt index 295397e4b..6625f5254 100644 --- a/rules/data_binding_annotation_template.txt +++ b/rules/data_binding_annotation_template.txt @@ -1,15 +1,13 @@ package android.databinding.layouts; -import android.databinding.BindingBuildInfo; +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( - buildId="not_used_here" // Adds incrementality, which Bazel already supports -) +@BindingBuildInfo public class DataBindingInfo { /* This only exists for annotation processing. */ } diff --git a/rules/resources.bzl b/rules/resources.bzl index 97ad6b657..6ae667442 100644 --- a/rules/resources.bzl +++ b/rules/resources.bzl @@ -378,8 +378,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 +388,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" 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 ce7e0e824..c70f98fc0 100644 --- a/src/tools/java/com/google/devtools/build/android/AndroidResourceProcessor.java +++ b/src/tools/java/com/google/devtools/build/android/AndroidResourceProcessor.java @@ -304,7 +304,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); From 4af2f7c4dbdce239183d6e71304ccc3b321a7e6c Mon Sep 17 00:00:00 2001 From: Prasanna Swaminathan Date: Mon, 11 May 2026 13:31:30 -0500 Subject: [PATCH 4/6] [databinding] Add example that builds with data binding --- examples/databinding/.bazelrc | 37 +++++++++++ examples/databinding/.gitignore | 1 + examples/databinding/BUILD | 1 + examples/databinding/MODULE.bazel | 51 +++++++++++++++ examples/databinding/README.md | 8 +++ examples/databinding/WORKSPACE | 60 ++++++++++++++++++ examples/databinding/WORKSPACE.bzlmod | 1 + .../com/databindingapp/AndroidManifest.xml | 22 +++++++ .../databinding/java/com/databindingapp/BUILD | 23 +++++++ .../DataBindingMainActivity.java | 51 +++++++++++++++ .../res/drawable-hdpi/ic_launcher.png | Bin 0 -> 1678 bytes .../res/drawable-mdpi/ic_launcher.png | Bin 0 -> 1283 bytes .../res/drawable-xhdpi/ic_launcher.png | Bin 0 -> 1817 bytes .../res/drawable-xxhdpi/ic_launcher.png | Bin 0 -> 2137 bytes .../res/layout/activity_data_binding.xml | 39 ++++++++++++ .../java/com/databindingapp/res/menu/menu.xml | 8 +++ .../com/databindingapp/res/values/dimens.xml | 5 ++ .../com/databindingapp/res/values/strings.xml | 8 +++ 18 files changed, 315 insertions(+) create mode 100644 examples/databinding/.bazelrc create mode 100644 examples/databinding/.gitignore create mode 100644 examples/databinding/BUILD create mode 100644 examples/databinding/MODULE.bazel create mode 100644 examples/databinding/README.md create mode 100644 examples/databinding/WORKSPACE create mode 100644 examples/databinding/WORKSPACE.bzlmod create mode 100644 examples/databinding/java/com/databindingapp/AndroidManifest.xml create mode 100644 examples/databinding/java/com/databindingapp/BUILD create mode 100644 examples/databinding/java/com/databindingapp/DataBindingMainActivity.java create mode 100644 examples/databinding/java/com/databindingapp/res/drawable-hdpi/ic_launcher.png create mode 100644 examples/databinding/java/com/databindingapp/res/drawable-mdpi/ic_launcher.png create mode 100644 examples/databinding/java/com/databindingapp/res/drawable-xhdpi/ic_launcher.png create mode 100644 examples/databinding/java/com/databindingapp/res/drawable-xxhdpi/ic_launcher.png create mode 100644 examples/databinding/java/com/databindingapp/res/layout/activity_data_binding.xml create mode 100644 examples/databinding/java/com/databindingapp/res/menu/menu.xml create mode 100644 examples/databinding/java/com/databindingapp/res/values/dimens.xml create mode 100644 examples/databinding/java/com/databindingapp/res/values/strings.xml diff --git a/examples/databinding/.bazelrc b/examples/databinding/.bazelrc new file mode 100644 index 000000000..08999d781 --- /dev/null +++ b/examples/databinding/.bazelrc @@ -0,0 +1,37 @@ +# Config for library desugaring +common:core_library_desugaring --desugar_java8_libs + +# 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 build Android builder tools +common --java_language_version=17 +common --java_runtime_version=17 +common --tool_java_language_version=17 +# Required to invoke the Studio deployer jar +common --tool_java_runtime_version=17 + +# Workaround for a rules_java + bazel < 8.3.0 issue. It should only be relevant +# for bazel@HEAD and rolling releases. +# https://github.com/bazelbuild/bazel/pull/26119 +common --repositories_without_autoloads=bazel_features_version,bazel_features_globals,cc_compatibility_proxy + +# Newer versions of protobuf require downstream transitive projects to set C++ language version flags. +common:linux --cxxopt=-std=c++17 +common:linux --host_cxxopt=-std=c++17 +common:macos --cxxopt=-std=c++17 +common:macos --host_cxxopt=-std=c++17 +common:windows --cxxopt=/std:c++17 +common:windows --host_cxxopt=/std:c++17 +# Suppress warnings from external repos +common:linux --per_file_copt=external/.*@-w +common:linux --host_per_file_copt=external/.*@-w +common:macos --per_file_copt=external/.*@-w +common:macos --host_per_file_copt=external/.*@-w +common:windows --per_file_copt=external/.*@/w +common:windows --host_per_file_copt=external/.*@/w + +# Enable protobuf MSVC support on Windows +build:windows --define=protobuf_allow_msvc=true + +common --enable_platform_specific_config + 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..7bf9eecff --- /dev/null +++ b/examples/databinding/MODULE.bazel @@ -0,0 +1,51 @@ +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_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-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..8f6076b32 --- /dev/null +++ b/examples/databinding/java/com/databindingapp/BUILD @@ -0,0 +1,23 @@ +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_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 0000000000000000000000000000000000000000..6ab2adde210fde3d980a0d3d9c551dcf61a96a86 GIT binary patch literal 1678 zcmah|X;4#F6ux1HVzeYIQliiX5(;8?fM7r+d%|u25fNoI1eXNN0)8-br38IaEd>MkoV-GrPahB^pEDF_|w61z?*C0EH?5v=AyB13*p# z;6(xe^jZLHrS+XF0|3B1hzN<|zyQTDErkNz znl-2t_lU*4>Fh>6vw_Px%wuB0Wi{{^jU3;a6~4Ra#5@YvWCH>KG2R+T?19uCumE5J z0NI5Y1Cce>1-8H%L1V`LYCsU(iuIaBFn1>G>pepz>kgy@smhgicG zmyiE`rhoMcYL43sh~dP1O27f;vYJ5Rdh6Lp>jJ8Ef(@)TD~v;&W%aCOwSkO-oaOZ_ z%dGDSxh{kR8_TFU%j+1}5+w7nCBRj{oj`&1>~b=`18LSkV1(@_*$RZ4VzXn@$>g`8 z$mOVPcGau~F6;v96Y^XL@kGWUF4$rR0%4t*$}oLq)r33>VI2{q+W?*+;A1c3%IMl? zs7hqQut=!?@3Z$eH1xN(YZ;5d;Z}{Fv^z02)`G)96|<_vPV#wNQutY?)qgI~>I(I` zTtHsUgX?*G@qSNnS(&4O#qorg3F{ornHsMwktNl+Om}8-X!4b2Vv<~QZFir# zE<1nV)6?@Or*7mWow0U{>G66L(;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rh1e*FClq&Vt<1W@b;M{O-9E9(=f$P+-j7?Zr+T^Kr8 zWj%l#&H|6fVg?4j!ywFfJby(BP>{XE)7O>#8I!oMF6-ClcY&#i@ua7VV~EG`y_Z6R znGHqS9tP~$v0Fs@9SFoN5b4^o<$%BXO_klA-wf`aGkCJ9c;EM#_jH{8|5UnUY?M2# zFK1I~>T{P{Ix;_}%zl1Ke}el54IjN!mi|jB{SN|X9uc&erIyUn?{MDz1IM=#mj3-; zQds&!TwKLh3CneNH6$1M=&%*4F4g;7Y|)S$S!u)et?QKa<&dW(VKe*6r;3*G2(8L~ zcXi{ocMlidE6G{0(of<4g&Rxyc9idKOb%W=mnrp1m3e_lgUKd23oZwVulH(18tu6D zzSLke^V(ZAg;7#|YMf=zgd2Abxu<8(zVtY`x_z>|_3lX=FW$X;njOFY)^1zo=0DFb zeBfZzC@kseUjC7D>-;%Y-!{}$sdd)fZ~L(CjGxBeJFjo=zQld#^=D6}?{)U9N% z-#d#e;SW&wzUzR4K$L&utzyRpy^QsY#phetW+d3M$*s74#D;a&16wxk1pgzCgjgeV z)^D(T5FguIt#i%X%znm=TWPfk>E-XI%PoBJx7k^(X@2|~dn-RjjV&B?^^q@F4?3*4 zCm_=Du5=-zwyiiz#Os*{4rHzGocv(>*LfY6H-9#$($UjfzwYo0L9e|#eYuzaKmDD# z#O!R#l9)ZWf$^+b;u=wsl30>zm0Xkxq!^40jEr;*jC75RLkumgOiZl|OtlRRtPBjc z-uvi@q9HdwB{QuOw}vClo?n3)BtbR==ckpFCl;kLl$V$5W#(lUCnpx9>g5-u&wghk Q1ysb~>FVdQ&MBb@0DAuQWB>pF literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..014b0f1064d5d3a51451970d0bb39b954e805264 GIT binary patch literal 1817 zcmZ`&3se(l7XBj)nUGe3&>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{#FV}5NMj%;MzEK$lBxQ zv@No%;e;Q^ahX16bZ>k*ri(T@cKfv5miB|b(?qWf!L=XjvJ?njuN^s-utE{ju9ALC zYC1DA$PB?pf~N|OvKfQH=KmOz7!R^03ykYx`@EX8?;O#E-WU6?|Ho^b*p9s5zQcw0 zZ<#d42Y-%xwBv%gW;F=cOx?T@xsUJ<3zSYrscA30!?Eq)@3+=VT4&}=vu?Rfp=F=g z20|}Y{BY!K3{_`S-u6V4FBdA!4m0NMCT3;lB3=4xG8u1hsBzRpePH0e zqKgXH-qo1aJ$=TP#w1#+N$2^6^c4d-=~ZN#q1^?qd1P&^MvfV-1UkIi%b^wFe01KQ ztD|q;3j$E-*aJ-f@}j{f!0VF?XXyUlb`XUyYNZ(6tW-Y5s`@w@bxPs_gv1wq#mFEvEl zI!Yt2m^GDaLRGvj*TVMGHthMohW3@2lfV9p^^}WKpLuNk`G#rjLUz%j^?1lP&(2Mt ztG&csho?`(Q}3TaUcp|w6yuCOeXdWO;rMeKXRI3?fqOAMse1pTX@T>q*h$?+p5yPX zxcH*n)x=dkI8aRYvvA)c_8Z6tG<3#eoMm25-OYGtl^UoAbo97xFg7Bgt!;ECW-A8; z*FWs2Vq%?T$)7-t8go?n=CRHkx%Im;Z2%$BzkF;CwCG3j(k6S#;9u#FiQdApGq44h6N|-WvD$`bW&f4%jw(AZx9tBXeEyzn4kmco rJH)GUF-=BM4$x>cTk{GFinB7*Ia^gl%DU+YFGvD1QMB-%{B*;QT66hG literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..20703a15c6809f4919a823ff4b5f571b02c5e88f GIT binary patch literal 2137 zcmZ`(c~p~E7JmUk5)czKmHi_S6qEvy%?Jr5A(*g85;j}v0z{y|AX`WRC=d_<#Q>us zRSra9O6-gas68%N9Y7GODBD4yDvN9~f)<}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UCP^~oG>RF7?-StL*KI|x?drR>n0Dd~1lYaF=RHI> zvC%y2nkwynnt0$w$a7^Aw` zC+yKDrxPUa5LDt(E>9=>4x6{hE9(eYf3CkFUihI9|LZ%ubLBoCU+S#azH{ro@^;x| zZ`G>=_lwJ~=29Gbr}Dl#F+H+y(P`*-Nl(SOhp#Ur(wpQC{5)Ev9Lv8$6LF}@JfxXS z9N^&xjrCt$Dkj({a&z7^>KAGknBF?faOq_^iSL0lkU`i<%)EnM_%zDffDxXpkRRY* zLlk6u*fo3NSIohjg2o6iIQG^%ovD(x$SwK1YhyTc{=r%nXR9)}d_-UpcBymLL3i-= zY~}!JaG}dD`7ud)ljnpsI&44ZfHFGL_^elRW~6Y!-&;G@S*L#=c3k^@`4sARck`hS z+7VTz#bLLau~m&VH?aF1t}iWmT@QkP!Sy1)Z$$f|MX_GO^jIi>E5+5-iQ?+y>K3+n ztCzcn7sb<&Lh+(d4tdTE{3RecWnV&E*8dMsC53(u1I$(`gr>xaB+==yKq8SiC+ttk b5JsoQI;W(^pP8qbLnq+x7tFrMiYoXov?hQf literal 0 HcmV?d00001 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 @@ + + + + + + + + + + + + + +