Skip to content

Commit 9659224

Browse files
committed
[databinding] Fix databinding templates and resources for AndroidX
AFAICT, the AndroidX flag isn't respected anywhere; instead, it is just hard-coded as false here. This flips it to true. Additionally, this requires a change in the template. If this actually does need to be customizable, I can wire up some of the flags accordingly. But, given that data binding just didn't work on any platform (without the previous commit), I can't imagine it is too controversial to set androidx=true. 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 27150b9 commit 9659224

File tree

6 files changed

+12
-11
lines changed

6 files changed

+12
-11
lines changed

.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/busybox.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,7 @@ def _process_databinding(
978978
args.add_all(res_dirs, before_each = "--resource_root")
979979
args.add("--dataBindingInfoOut", out_databinding_info)
980980
args.add("--appId", java_package)
981+
args.add("--useDataBindingAndroidX", "true")
981982

982983
_set_warning_level(ctx, args)
983984

rules/data_binding.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _gen_sources(ctx, output_dir, java_package, deps, layout_info, data_binding_
6767
args.add("-classInfoOut", class_info)
6868
args.add("-sourceOut", srcjar)
6969
args.add("-zipSourceOutput", "true")
70-
args.add("-useAndroidX", "false")
70+
args.add("-useAndroidX", "true")
7171

7272
if deps:
7373
if type(deps[0].class_infos) == "depset":
@@ -250,11 +250,11 @@ def _process(
250250
if defines_resources:
251251
# Outputs of the Data Binding annotation processor.
252252
br_out = ctx.actions.declare_file(
253-
output_dir + "bin-files/%s-br.bin" % java_package,
253+
output_dir + "bin-files/%s--br.bin" % java_package,
254254
)
255255
db_info[_JAVA_ANNOTATION_PROCESSOR_ADDITIONAL_OUTPUTS].append(br_out)
256256
setter_store_out = ctx.actions.declare_file(
257-
output_dir + "bin-files/%s-setter_store.json" % java_package,
257+
output_dir + "bin-files/%s--setter_store.json" % java_package,
258258
)
259259
db_info[_JAVA_ANNOTATION_PROCESSOR_ADDITIONAL_OUTPUTS].append(
260260
setter_store_out,
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package android.databinding.layouts;
22

3-
import android.databinding.BindingBuildInfo;
3+
import androidx.databinding.BindingBuildInfo;
44

55
/**
66
* Template for the file that feeds data binding's annotation processor. The
77
* processor reads the values set here to generate .java files that link XML
88
* data binding declarations (from layoutInfoDir) to app code.
99
*/
10-
@BindingBuildInfo(
11-
buildId="not_used_here" // Adds incrementality, which Bazel already supports
12-
)
10+
@BindingBuildInfo
1311
public class DataBindingInfo {
1412
/* This only exists for annotation processing. */
1513
}

rules/resources.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ OUT_DIR=$(mktemp -d)
381381
CUR_PWD=$(pwd)
382382
383383
if zipinfo -t "$1"; then
384-
ORDERED_LIST=`(unzip -l "$1" | sed -e '1,3d' | head -n -2 | tr -s " " | cut -d " " -f5)`
384+
ORDERED_LIST=`(zipinfo -1 "$1" | sort)`
385385
386386
unzip -q "$1" -d "$IN_DIR"
387387
@@ -390,7 +390,8 @@ if zipinfo -t "$1"; then
390390
for FILE in $ORDERED_LIST; do
391391
cd "$IN_DIR"
392392
if [ -f "$FILE" ]; then
393-
sed -i 's/Databinding\\-processed\\-resources/databinding\\-processed\\-resources/g' "$FILE"
393+
LC_ALL=C sed -i.bak 's/Databinding\\-processed\\-resources/databinding\\-processed\\-resources/g' "$FILE"
394+
rm "${FILE}.bak"
394395
NEW_NAME=`echo "$FILE" | sed 's/Databinding\\-processed\\-resources/databinding\\-processed\\-resources/g' | sed 's#'"$IN_DIR"'/##g'`
395396
mkdir -p `dirname "$OUT_DIR/$NEW_NAME"` && touch "$OUT_DIR/$NEW_NAME"
396397
cp -p "$FILE" "$OUT_DIR/$NEW_NAME"

src/tools/java/com/google/devtools/build/android/AndroidResourceProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static Path processDataBindings(
305305

306306
ProcessXmlOptionsWrapper options = new ProcessXmlOptionsWrapper();
307307
options.setAppId(packagePath);
308-
options.setResInput(inputResourcesDir.toFile());
308+
options.setResInput(inputResourcesDir.toFile().getAbsoluteFile());
309309
options.setResOutput(processedResourceDir.toFile());
310310
options.setLayoutInfoOutput(dataBindingInfoOut.toFile());
311311
options.setUseAndroidX(useDataBindingAndroidX);

0 commit comments

Comments
 (0)