bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.99k stars 4.03k forks source link

Android ViewBinding support #13931

Open Kernald opened 3 years ago

Kernald commented 3 years ago

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 #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?

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).

cpaleop commented 2 years ago

Is there any progress to this?

ahumesky commented 2 years ago

Unfortunately it's unlikely that we'll be able to implement this in the near term while we move the Android rules to Starlark

cpaleop commented 2 years ago

That's too bad. Google has recommended this feature for xml-based layouts, it was the way-to-go for a long time now.

lukaciko commented 2 years ago

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.

magicalmcgrady commented 2 years ago

@lukaciko can you show the implementation source code?

lukaciko commented 2 years ago

@magicalmcgrady the code was written during working hours so I would need to get approval before sharing it.

arunkumar9t2 commented 2 years ago

@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