Android ViewBinding support
Description of the problem / feature request:
Support Android ViewBinding for Android apps.
Feature requests: what underlying problem are you trying to solve with this feature?
ViewBinding allows generating at build-time a Java class with accessors for different views in a layout, without having to rely on findViewById etc. It's similar to the DataBinding talked about in bazelbuild/bazel#2694, but more recent, simple and less feature-heavy (it's only giving bindings to the view, no data updates or anything).
Have you found anything relevant by searching the web?
bazelbuild/bazel#2694, which is about a similar Jetpack feature, but not the same, and https://github.com/bazelbuild/rules_kotlin/issues/401, which is asking about ViewBinding support in Bazel (arguably in the wrong repo though).
Is there any progress to this?
Unfortunately it's unlikely that we'll be able to implement this in the near term while we move the Android rules to Starlark
That's too bad. Google has recommended this feature for xml-based layouts, it was the way-to-go for a long time now.
We've implemented view binding support at Spotify with not too much code. In short, we made a Kotlin class that invokes androidx.databinding:databinding-compiler-common to generate the *Binding.java classes. We then created a macro which figures out what classes should be generated based on input layout files and registers a genrule which invokes the Kotlin class. There's a couple other things that need to be done, like making sure androidx.databinding:viewbinding dependency is added to the target that is using view binding.
If it makes sense I could take a look at implementing the same in this repository.
@lukaciko can you show the implementation source code?
@magicalmcgrady the code was written during working hours so I would need to get approval before sharing it.
@magicalmcgrady We made something similar for databinding with @BindingAdapter support etc but that can be used for ViewBinding as well.
https://github.com/grab/grab-bazel-common#databinding
Sample usage: https://github.com/grab/Grazel/blob/d1c13ef28514fb9c21732802b18a9f77fa3402f9/sample-android-flavor/BUILD.bazel#L10 https://github.com/grab/Grazel/blob/d1c13ef28514fb9c21732802b18a9f77fa3402f9/sample-android-flavor/src/main/java/com/grab/grazel/android/flavor/FlavorActivity.kt#L28