https://kotlinlang.org logo
#android
Title
# android
k

Karan Sharma

05/28/2020, 6:14 AM
@here is there any performance difference between DataBinding / ViewBinding and Kotlin Synthetics ?
b

bezrukov

05/28/2020, 7:02 AM
Yes, kotlin synthetics is backed by hashmap (or sparse array), so lookup happens each time you're accessing the view. VB just generates a class with a field per view. On the other hand KS is lazy, so findViewBindId will be called for each view separately at the first access time. VB calls findViewById for all views immediately at the binding creation time.
👍 1
So performance difference heavily depends on your usecase, but VB should be faster in general
k

Karan Sharma

05/28/2020, 7:05 AM
Thanks @bezrukov
g

gildor

05/28/2020, 9:46 AM
Kotlin Synthetic are deprecated, whatever it’s faster or not it’s probably not a good idea to use them
b

Bhullnatik

05/28/2020, 9:54 AM
Since when @gildor?
g

gildor

05/28/2020, 9:57 AM
sorry, you right, only Anko was deprected, before it was under the same project, now it’s just android-extensions gradle plugin But I still think use Kotlin Synthetic is a bad idea
and Google Doesn’t plan to develop it too, it clearly under maintanance, and removed from all Google samples were it was uysed
Android team invest into ViewBindings
and VB has many advantages
And such 2 year old issues make them completely useless for any project with more than 2 modules https://youtrack.jetbrains.com/issue/KT-22430
k

Karan Sharma

05/29/2020, 12:08 AM
Also, what about Data Binding v/s VB ? Should we use Databinding ?
g

gildor

05/29/2020, 1:55 AM
It’s much more complex question. If your goal is to replace findViewById and be more type safe when you work layouts, just use VB, they are faster to compile DataBinding work with views is just a side effect, main feature is binding you data to XML layout directly and generate this glue code for you
k

Karan Sharma

05/29/2020, 4:42 AM
To be more specific, what would be the best approach for scenarios in DataBinding vs ViewBinding 1. Where we use <include> layouts and 2. Where we use some recycler views/ custom View Classes. Thanks in advance ! 🙂
g

gildor

05/29/2020, 4:43 AM
not sure that I got your question
DataBinding makes sense only if you want to use MVVM architecture and use all their features
2 Views