Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android data binding support #2694

Open
gregestren opened this issue Mar 16, 2017 · 28 comments
Open

Add Android data binding support #2694

gregestren opened this issue Mar 16, 2017 · 28 comments
Assignees
Labels
not stale Issues or PRs that are inactive but not considered stale P2 We'll consider working on this in future. (Assignee optional) team-Android Issues for Android team type: feature request

Comments

@gregestren
Copy link
Contributor

Bazel already has internal support for Android data binding (e.g. here). But hooks haven't been exposed to link this to the SDK support libraries or toggle the feature on for android_binary / android_library rules.

This bug tracks the effort to make data binding end-user usable.

@gregestren
Copy link
Contributor Author

@gregestren
Copy link
Contributor Author

Update: we're making internal progress on this change and can start exposing further hooks in Bazel likely over the next week. This still won't be functional though without having the data binding libraries checked into appropriate places in your repo.

Anyone interested in experimenting with this earlier, and willing to play around with checking in the data binding libraries themselves, report here and we're glad to work through the process with you.

bazel-io pushed a commit that referenced this issue Apr 13, 2017
…ary.

Before this change Bazel was adding this implicitly. But that wasn't
working properly with --incremental_dexing. And that implementation is a
lot messier and bug-prone vs. just including it as a standard dependency.

Issue: #2694
PiperOrigin-RevId: 152956638
bazel-io pushed a commit that referenced this issue Apr 18, 2017
The logic is already in Bazel but wasn't available to build rules.
This change makes it available, but still requires data binding's
{build|run}time libraries to be checked into appropriate depot
spots for everything to work.

Followup changes will make those libraries easily available.

Issue: #2694
PiperOrigin-RevId: 153359861
@aj-michael
Copy link
Contributor

Update: After a discussion with @gregestren we've determined that we need to update the aar_import rule to handle the databinding metadata files that are included in the com.android.databinding:adapters:1.3.*:aar libraries.

bazel-io pushed a commit that referenced this issue Apr 24, 2017
#2694

Change-Id: I33e1c1a6c1f1c4b31227eb26b0deed09525a7ec7
bazel-io pushed a commit that referenced this issue Nov 16, 2017
Also, add a fake databinding processor target.

Bazel users will need to do something like this:

# WORKSPACE
local_repository(
    name = "databinding",
    path = "./databinding",
)
bind(
    name = "databinding_annotation_processor",
    actual = "@databinding//:annotation_processor",
)

# databinding/BUILD
java_plugin(
    name = "annotation_processor",
    ...
)
...

#2694

RELNOTES: None
PiperOrigin-RevId: 175975110
@jin jin added the P2 We'll consider working on this in future. (Assignee optional) label Jun 15, 2018
@Paul-McNeice
Copy link

It looks like there has been various commits in relation to this issue since the last comment - just wondering how close we are to having databinding support up and running?

@jin
Copy link
Member

jin commented Jul 22, 2018

@ahumesky

@gregestren gregestren assigned ahumesky and unassigned gregestren Jul 23, 2018
@gregestren
Copy link
Contributor Author

Paul,

As Jin referenced above, @ahumesky is the current point of contact for data binding support. He's working through a number of technical issues about it at the moment and can give you the clearest expectations.

@ahumesky
Copy link
Contributor

We're working to upgrade databinding in bazel to the latest version to support androidx / jetpack, as well as make depending on databinding easier. We've also found some bugs with databinding + android_instrumentation_test. We hope to have these issues sorted out in the next couple of weeks.

@jin jin added team-Android Issues for Android team and removed category: rules > android labels Aug 11, 2018
@Paul-McNeice
Copy link

Hey @ahumesky, any update on the status of this?

@ahumesky
Copy link
Contributor

This was interrupted by a few little urgent issues, but those are done and we're back to work on this. I hope to have v2 working this week, then I plan to make getting the databinding dependencies working with the android rules easier (today you have to manually wire up the databinding dependencies).

@codesuki
Copy link

codesuki commented Nov 8, 2018

Hey, I am trying to convert a project to Bazel using databindings. Is there any progress on this? Even a hacky workaround would help me a lot until it's officially working.
When enabling databinding on android_binary Bazel crashes with NPE.

@ahumesky
Copy link
Contributor

ahumesky commented Nov 8, 2018

There has been significant progress, I have databinding v2 working, and I'm in the process of getting all the code reviewed and submitted. Should be in in the next couple of days.

You're seeing an NPE probably because the databinding dependencies need to be added to the rules with databinding enabled (it shouldn't give an NPE for that though, that's a bug in itself). I'm also working to make getting the databinding dependencies easier. The main problem right now is that our gmaven repo for android dependencies is incomplete (databinding depends on things that are hosted outside gmaven). It would be better to use something like bazel-deps to generate the rules for these dependencies, but bazel-deps does not support AARs.

@codesuki
Copy link

codesuki commented Nov 8, 2018

Thanks for the update. That sounds good!

I tried adding

aar_import(
    name = “library-1.3.1”,
    aar = “@androidsdk//:extras/android/m2repository/com/android/databinding/library/1.3.1/library-1.3.1.aar”,
)

As a dependency but it had no effect. Probably it's wrong anyway.

Do you have a sample on how to add the dependency correctly? I could adapt that to my local installation. Right now I don't even know which library is the correct one. And the only one I found is the one in the SDK.
😅

@codesuki
Copy link

codesuki commented Nov 8, 2018

This is the NPE when setting enable_data_binding = True .
Bazel versions 0.15.2 and 0.18.1.

Internal error thrown during build. Printing stack trace: java.lang.RuntimeException: Unrecoverable error while evaluating node '//:app BuildConfigurationValue.Key[c46469ce7e7c9c01ec016ebdb5b40fd0] false' (requested by nodes )
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:495)
	at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:368)
	at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1386)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.execLocalTasks(ForkJoinPool.java:1040)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1058)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.NullPointerException
	at com.google.devtools.build.lib.rules.java.JavaCompilationHelper.usesAnnotationProcessing(JavaCompilationHelper.java:465)
	at com.google.devtools.build.lib.rules.android.AndroidCommon.initJava(AndroidCommon.java:606)
	at com.google.devtools.build.lib.rules.android.AndroidCommon.init(AndroidCommon.java:540)
	at com.google.devtools.build.lib.rules.android.AndroidBinary.init(AndroidBinary.java:251)
	at com.google.devtools.build.lib.rules.android.AndroidBinary.create(AndroidBinary.java:119)
	at com.google.devtools.build.lib.rules.android.AndroidBinary.create(AndroidBinary.java:95)
	at com.google.devtools.build.lib.analysis.ConfiguredTargetFactory.createRule(ConfiguredTargetFactory.java:319)
	at com.google.devtools.build.lib.analysis.ConfiguredTargetFactory.createConfiguredTarget(ConfiguredTargetFactory.java:205)
	at com.google.devtools.build.lib.skyframe.SkyframeBuildView.createConfiguredTarget(SkyframeBuildView.java:635)
	at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.createConfiguredTarget(ConfiguredTargetFunction.java:777)
	at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.compute(ConfiguredTargetFunction.java:323)
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:418)
	... 7 more

https://github.com/bazelbuild/bazel/blob/0.18.1/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java#L463

I tried setting javacopts to include -processor just to see if the NPE goes away but nothing changed.

@cre8ivejp
Copy link

@ahumesky Hi
Any progress on this issue?

@ahumesky
Copy link
Contributor

The current state of things is:

  1. Bazel supports databinding "v1", however v1 has performance issues, and also requires all the databinding dependencies to be manually wired up.
  2. Bazel has been updated to support databinding "v2" which should solve the performance issues, however we ran into some bugs in bazel / databinding integrations while trying to roll this out internally that will require fixes on the databinding side. I hope to have those integrated in the next week or two. v2 integration also requires the databinding dependencies to be pulled in in a particular way, so in the meantime I'm working to make that smoother.

bazel-io pushed a commit that referenced this issue Feb 21, 2020
Necessary for #2694.

RELNOTES: None.
PiperOrigin-RevId: 296434617
bazel-io pushed a commit that referenced this issue May 1, 2020
bazel-io pushed a commit that referenced this issue Jun 23, 2020
1. Updates databinding to v3.4.0
2. Updates gson to 2.8.0
3. All other libraries are added

Necessary for #2694.

Partial commit for third_party/*, see #11629.

Signed-off-by: Philipp Wollermann <philwo@google.com>
@ahumesky
Copy link
Contributor

ahumesky commented Jun 26, 2020

Hi all,
If you're interested in trying databinding integration, it's a little easier now with the third-party dependencies checked in.

  1. Get the bazel source git clone git@github.com:bazelbuild/bazel.git bazel
  2. cd bazel
  3. bazel build //src:bazel //tools/android/runtime_deps:android_tools.tar.gz
  4. Extract the android tools somewhere:
mkdir /tmp/android_tools
tar -xf bazel-bin/tools/android/runtime_deps/android_tools.tar.gz -C /tmp/android_tools
  1. Create tools/android/BUILD with this in it:
java_plugin(
    name = "compiler_annotation_processor",
    processor_class = "android.databinding.annotationprocessor.ProcessDataBinding",
    generates_api = True,
    visibility = ["//visibility:public"],
    deps = [
      "@bazel_tools//src/tools/android/java/com/google/devtools/build/android:all_android_tools",
    ],
)
  1. Add the databinding app dependencies, and a bind rule to find the annotation processor plugin added above:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "2.4"
RULES_JVM_EXTERNAL_SHA = "2393f002b0a274055a4e803801cd078df90d1a8ac9f15748d1f803b97cfcdc9c"

http_archive(
    name = "rules_jvm_external",
    strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
    sha256 = RULES_JVM_EXTERNAL_SHA,
    url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    artifacts = [
      "com.android.databinding:adapters:3.4.2",
      "com.android.databinding:library:3.4.2",
      "com.android.databinding:baseLibrary:3.4.2",
      "com.android.support:support-annotations:28.0.0",
    ],
    repositories = [
        "https://jcenter.bintray.com/",
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

bind(
  name = "databinding_annotation_processor",
  actual = "//tools/android:compiler_annotation_processor",
)
  1. Add the databinding app dependencies to the app, e.g.:
load("@rules_jvm_external//:defs.bzl", "artifact")

package(default_visibility = ["//visibility:public"])

android_binary(
    name = "app",
    custom_package = "com.google.android.app",
    enable_data_binding = 1,
    srcs = glob(["src/main/java/**/*.java"]),
    resource_files = glob(["src/main/res/**/*"]),
    manifest = "AndroidManifest.xml",
    deps = [
      artifact("com.android.databinding:adapters:3.4.2"),
      artifact("com.android.databinding:library:3.4.2"),
      artifact("com.android.databinding:baseLibrary:3.4.2"),
      artifact("com.android.support:support-annotations:28.0.0"),
    ],
)
  1. Build using the bazel and android tools built above, and with some other flags:
~/bazel/bazel-bin/src/bazel build app --strategy=Desugar=sandboxed --android_databinding_use_v3_4_args --experimental_android_databinding_v2 --override_repository=android_tools=/tmp/android_tools

The --strategy is to work around #11618. Once the fix for that is in, we can deploy a new version of the android tools, and then neither that nor the --override_repository flags will be needed. --android_databinding_use_v3_4_args and --experimental_android_databinding_v2 can also be defaulted to true now, so in a future version those won't be necessary either. (v1 never worked with bazel, and the pre-v3.4 args no longer work either). The java_plugin() at tools/android/BUILD can probably be moved into the remote android tools BUILD file, and there's probably a way to get rid of the bind.

@ahumesky
Copy link
Contributor

ahumesky commented Jul 7, 2020

One correction: the java_plugin in the build file at tools/android/BUILD needs generates_api = True. I've updated the comment above.

@arunkumar9t2
Copy link
Contributor

arunkumar9t2 commented Jul 27, 2020

Thanks @ahumesky, the suggestions work so far for us. Do you have any plans to add support for androidx?

EDIT: Looks like it is being worked on here: f4430e4

EDIT2: Can confirm the above commit works with androidx

@tsawada
Copy link
Contributor

tsawada commented May 29, 2021

For those who are trying the workaround above, instead of building android_tools.tar.gz yourself && passing it with --override_repository, you can simply use recently-released android_tools by adding following snippet in the WORKSPACE:

http_archive(
    name = "android_tools",
    sha256 = "ed5290594244c2eeab41f0104519bcef51e27c699ff4b379fcbd25215270513e",
    url = "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.23.0.tar.gz",
)

@github-actions
Copy link

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.

@github-actions github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label May 10, 2023
@github-actions
Copy link

This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please reach out to the triage team (@bazelbuild/triage). Thanks!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2023
@keertk keertk reopened this May 25, 2023
@keertk keertk added not stale Issues or PRs that are inactive but not considered stale and removed stale Issues or PRs that are stale (no activity for 30 days) labels May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not stale Issues or PRs that are inactive but not considered stale P2 We'll consider working on this in future. (Assignee optional) team-Android Issues for Android team type: feature request
Projects
None yet
Development

No branches or pull requests