Skip to content

Commit f74aeba

Browse files
committed
[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.
1 parent b591c07 commit f74aeba

14 files changed

Lines changed: 64 additions & 16 deletions

File tree

.bazelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
common --java_runtime_version=17
1+
common --java_runtime_version=remotejdk_17
2+
common --tool_java_runtime_version=remotejdk_17

rules/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ load("@bazel_skylib//rules:common_settings.bzl", "int_setting")
33
load("@rules_java//java:defs.bzl", "java_library")
44

55
exports_files([
6-
"data_binding_annotation_template.txt",
6+
"data_binding_annotation_template_androidx.txt",
7+
"data_binding_annotation_template_support_lib.txt",
78
"res_v3_dummy_AndroidManifest.xml",
89
"res_v3_dummy_R.txt",
910
"robolectric_properties_template.txt",

rules/android_binary/attrs.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ ATTRS = _attrs.replace(
247247
Allow for the optimizer to process resources. This is not supported in proguard.
248248
""",
249249
),
250+
_databinding_use_androidx = attr.label(
251+
default = "//rules/flags:databinding_use_androidx",
252+
),
250253
),
251254
_attrs.compilation_attributes(apply_android_transition = True),
252255
_attrs.DATA_CONTEXT,

rules/android_binary/impl.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ def _process_proto(_unused_ctx, **_unused_ctxs):
188188
def _process_data_binding(ctx, java_package, packaged_resources_ctx, **_unused_ctxs):
189189
if ctx.attr.enable_data_binding and not acls.in_databinding_allowed(str(ctx.label)):
190190
fail("This target is not allowed to use databinding and enable_data_binding is True.")
191+
192+
if ctx.attr._databinding_use_androidx[BuildSettingInfo].value:
193+
template = get_android_toolchain(ctx).data_binding_annotation_template_androidx
194+
else:
195+
template = get_android_toolchain(ctx).data_binding_annotation_template_support_lib
196+
data_binding_annotation_template = utils.only(template.files.to_list())
197+
191198
return ProviderInfo(
192199
name = "db_ctx",
193200
value = data_binding.process(
@@ -201,8 +208,7 @@ def _process_data_binding(ctx, java_package, packaged_resources_ctx, **_unused_c
201208
data_binding_exec = get_android_toolchain(ctx).data_binding_exec.files_to_run,
202209
data_binding_annotation_processor =
203210
get_android_toolchain(ctx).data_binding_annotation_processor[JavaPluginInfo],
204-
data_binding_annotation_template =
205-
utils.only(get_android_toolchain(ctx).data_binding_annotation_template.files.to_list()),
211+
data_binding_annotation_template = data_binding_annotation_template,
206212
),
207213
)
208214

rules/android_library/attrs.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ ATTRS = _attrs.add(
243243
_aidl_lib = attr.label(
244244
default = Label("//rules:aidl_lib"),
245245
),
246+
_databinding_use_androidx = attr.label(
247+
default = "//rules/flags:databinding_use_androidx",
248+
),
246249
),
247250
_attrs.compilation_attributes(),
248251
_attrs.DATA_CONTEXT,

rules/android_library/impl.bzl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ load("//rules:resources.bzl", _resources = "resources")
3131
load("//rules:utils.bzl", "get_android_sdk", "get_android_toolchain", "log", "utils")
3232
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
3333
load("//rules/flags:flags.bzl", _flags = "flags")
34+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
3435
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
3536
load("@rules_java//java/common:java_plugin_info.bzl", "JavaPluginInfo")
3637
load("@rules_java//java/common:proguard_spec_info.bzl", "ProguardSpecInfo")
@@ -234,6 +235,13 @@ def _process_idl(ctx, **unused_sub_ctxs):
234235
def _process_data_binding(ctx, java_package, resources_ctx, **unused_sub_ctxs):
235236
if ctx.attr.enable_data_binding and not acls.in_databinding_allowed(str(ctx.label)):
236237
fail("This target is not allowed to use databinding and enable_data_binding is True.")
238+
239+
if ctx.attr._databinding_use_androidx[BuildSettingInfo].value:
240+
template = get_android_toolchain(ctx).data_binding_annotation_template_androidx
241+
else:
242+
template = get_android_toolchain(ctx).data_binding_annotation_template_support_lib
243+
data_binding_annotation_template = utils.only(template.files.to_list())
244+
237245
return ProviderInfo(
238246
name = "db_ctx",
239247
value = _data_binding.process(
@@ -247,8 +255,7 @@ def _process_data_binding(ctx, java_package, resources_ctx, **unused_sub_ctxs):
247255
data_binding_exec = get_android_toolchain(ctx).data_binding_exec.files_to_run,
248256
data_binding_annotation_processor =
249257
get_android_toolchain(ctx).data_binding_annotation_processor,
250-
data_binding_annotation_template =
251-
utils.only(get_android_toolchain(ctx).data_binding_annotation_template.files.to_list()),
258+
data_binding_annotation_template = data_binding_annotation_template,
252259
),
253260
)
254261

rules/busybox.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Bazel ResourcesBusyBox Commands."""
1515

16+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1617
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
1718
load(":java.bzl", _java = "java")
1819

@@ -978,6 +979,7 @@ def _process_databinding(
978979
args.add_all(res_dirs, before_each = "--resource_root")
979980
args.add("--dataBindingInfoOut", out_databinding_info)
980981
args.add("--appId", java_package)
982+
args.add("--useDataBindingAndroidX", ctx.attr._databinding_use_androidx[BuildSettingInfo].value)
981983

982984
_set_warning_level(ctx, args)
983985

rules/data_binding.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Bazel Android Data Binding."""
1515

16+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1617
load("//providers:providers.bzl", "DataBindingV2Info")
1718
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
1819
load(":utils.bzl", "ANDROID_TOOLCHAIN_TYPE", _utils = "utils")
@@ -67,7 +68,7 @@ def _gen_sources(ctx, output_dir, java_package, deps, layout_info, data_binding_
6768
args.add("-classInfoOut", class_info)
6869
args.add("-sourceOut", srcjar)
6970
args.add("-zipSourceOutput", "true")
70-
args.add("-useAndroidX", "false")
71+
args.add("-useAndroidX", ctx.attr._databinding_use_androidx[BuildSettingInfo].value)
7172

7273
if deps:
7374
if type(deps[0].class_infos) == "depset":
@@ -260,11 +261,11 @@ def _process(
260261
if defines_resources:
261262
# Outputs of the Data Binding annotation processor.
262263
br_out = ctx.actions.declare_file(
263-
output_dir + "bin-files/%s-br.bin" % java_package,
264+
output_dir + "bin-files/%s--br.bin" % java_package,
264265
)
265266
db_info[_JAVA_ANNOTATION_PROCESSOR_ADDITIONAL_OUTPUTS].append(br_out)
266267
setter_store_out = ctx.actions.declare_file(
267-
output_dir + "bin-files/%s-setter_store.json" % java_package,
268+
output_dir + "bin-files/%s--setter_store.json" % java_package,
268269
)
269270
db_info[_JAVA_ANNOTATION_PROCESSOR_ADDITIONAL_OUTPUTS].append(
270271
setter_store_out,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package android.databinding.layouts;
2+
3+
import androidx.databinding.BindingBuildInfo;
4+
5+
/**
6+
* Template for the file that feeds data binding's annotation processor. The
7+
* processor reads the values set here to generate .java files that link XML
8+
* data binding declarations (from layoutInfoDir) to app code.
9+
*/
10+
@BindingBuildInfo
11+
public class DataBindingInfo {
12+
/* This only exists for annotation processing. */
13+
}
File renamed without changes.

0 commit comments

Comments
 (0)