Home » Android » Do Kotlin Android Extensions cache the synthetic properties or each time it calls findViewById()?

Do Kotlin Android Extensions cache the synthetic properties or each time it calls findViewById()?

Posted by: admin June 15, 2020 Leave a comment


If I have a simple custom view:




Accessing a kotlinx syntentic property:

import kotlinx.android.synthetic.main.myitem.view.*

view.toolbar.text = "Some text"

Internally it generates a call to findByViewID(). So my question is:

Is the result cached for custom views like for activities or each each time findByViewID is called? The answer is quite important for performance reasons.

How to&Answers:

Since 1.1.4 views can be cached in any class.
Caching in custom views enabled by default. For ViewHolders you need to implement LayoutContainer interface like this:

class MyViewHolder(override val containerView: View): LayoutContainer

See this doc for details

To be able to use LayoutContainer you should add this to the gradle script:

androidExtensions {
experimental = true


In the current version (1.1.3), views are cached for Activities and Fragments layouts. For other kinds of containers like RecyclerView ViewHolders, there is no cache.

Also, the cache is a HashMap with Integer boxing for keys. A SparseArray would have been better.

Edit: Since version 1.1.4, views can be cached for other classes too, including ViewHolder, if you make them implement the LayoutContainer interface. You can also use the @ContainerOptions annotation to specify another cache implementation, including SparseArray. Both of these features are still experimental and need to be enabled manually in your build.gradle file:

androidExtensions {
    experimental = true

Read more about it.