-
Notifications
You must be signed in to change notification settings - Fork 213
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 support for Android data-binding when building with Kotlin #346
Add support for Android data-binding when building with Kotlin #346
Conversation
NB: this actually fixes building the "_base" target, but "_kt" still fails with an error:
I'll keep this PR open for early feedback if that's fine with maintainers, but I'm going to continue chipping away at the new issue. |
So it turns out the issue above occurs when specifically compiling headers for the base Further, this leads to runtime errors when trying to open the app:
Which seems like another issue of a dependency not being included somewhere. |
@ahumesky has been helping me navigate these latest errors. It seems the error is caused when you reference a data-binding-enabled library from a non-data-binding-enabled binary. However, after enabling data-binding for both I'm now running into a cannot find symbol error for the generated DataBindingMapperImpl for the library. Hoping to make more progress on that tomorrow. Note that the findings for this message correspond to a Java-only Bazel project, but I suspect the same issues will translate over to Kotlin for this specific part of the build process. |
So per investigations today & feedback from @ahumesky it seems that the top-level Another problem I'm noticing is that custom binding adapters are causing some issues in the Kotlin world where we split the |
Sadly, because we don't have access to the native android providers, the kotlin android integration is pretty bad. And really quite inefficient. Might that change soon? |
@restingbull I'm definitely not in a position to answer that question. :) I'm actually wondering: given that the current integration isn't ideal, how much can |
So there's We could enable the databinding provider in starlark with that flag too: Of course, having to rely on an experimental flag isn't great, but we don't have any plans to remove that flag until the starlark versions are available |
I have a deep, abiding dislike for using experimental flags in production code. Extra actions, I'm looking at you. |
I think this is the best effort we can make short of introducing a dedicated kt_android_library rule that properly integrates with data-binding. While this approach seems to work fairly transparently for simple data-binding cases, the more complex case of custom binding adapters results in some issues that don't seem to have a similarly easy workaround:
Please let me know if this solution is acceptable to you @restingbull and others. While having binding adapters in Java isn't ideal, being able to actually use data-binding with Bazel in a project that heavily makes use of both data-binding and Kotlin is still a huge win for us. |
kotlin/internal/jvm/android.bzl
Outdated
@@ -30,6 +30,7 @@ def _kt_android_artifact(name, srcs = [], deps = [], plugins = [], **kwargs): | |||
name = base_name, | |||
visibility = ["//visibility:private"], | |||
exports = base_deps, | |||
deps = deps if kwargs.get('enable_data_binding', default = False) else [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't pull from kwargs -- add the attribute and propagate as necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
argument rather than pulling it from kwargs.
Thanks @restingbull--I addressed your comment. PTAL. |
Thanks @restingbull! |
There's a lot of context behind this PR, but to summarize: data-binding v2 is currently in the process of being introduced in upstream Bazel (see bazelbuild/bazel#10795). While this solution works correctly with Java-based Android apps, the Kotlin macros seem to break for similar Kotlin + data-binding apps. With trivial data-binding projects, the generated databinding file fails to build due to missing dependencies. This seems to be happening because the Kotlin macro splits up the library into a native android_library target, and a separate kt_jvm_library to actually build the sources. When data-binding is enabled, android_library may also run the Javac compiler which means the dependencies passed into the top-level kt_android_library also need to be passed into the native android_library to satisfy dependencies.
There's one issue with the above: it's not valid for android_library to have deps but no sources (hence why the warning suggests using exports instead). However, it's valid to do this in exactly the data-binding case per https://docs.bazel.build/versions/master/be/android.html (see
enable_data_binding
).This solution is a bit hacky, so please advise on: