Kotlin Android View Binding: findViewById vs Butterknife vs Kotlin Android Extension Kotlin Android View Binding: findViewById vs Butterknife vs Kotlin Android Extension android android

Kotlin Android View Binding: findViewById vs Butterknife vs Kotlin Android Extension


kotlin-android-extensions is better for Kotlin. ButterKnife is also good but kotlin-android-extensions is a better and smart choice here.

Reason : Kotlin uses synthetic properties and those are called on demand using caching function(Hence slight fast Activity/Fragment loading) while ButterKnife binds all view at a time on ButterKnife.bind()(that consumes slight more time). With Kotlin you don't even need to use annotation for binding the views.

Yes it also plays good with RecyclerView + ViewHolder pattern, you just need to import kotlinx.android.synthetic.main.layout_main.view.*(if layout_main.xml is Activity/Fragment layout file name).

You do not need to do any extra effort for layout imported using include. Just use id of imported views.

Have a look at following official documentation notes:

Kotlin Android Extensions is a plugin for the Kotlin compiler, and it does two things:

  1. Adds a hidden caching function and a field inside each Kotlin Activity. The method is pretty small so it doesn't increase the size of APK much.
  2. Replaces each synthetic property call with a function call.

    How this works is that when invoking a synthetic property, where the receiver is a Kotlin Activity/Fragment class that is in module sources, the caching function is invoked. For instance, given

class MyActivity : Activity()fun MyActivity.a() {     this.textView.setText(“”)}

a hidden caching function is generated inside MyActivity, so we can use the caching mechanism.

However in the following case:

fun Activity.b() {     this.textView.setText(“”)}

We wouldn't know if this function would be invoked on only Activities from our sources or on plain Java Activities also. As such, we don’t use caching there, even if MyActivity instance from the previous example is the receiver.

Link to above documentation page

I hope it helps.


There are a lot of ways to access views in Android. A quick overview:

enter image description here

My advise would be:

  1. findViewById: old school. Avoid.
  2. ButterKnife: old school, but less boilerplate and some added functionality. Still lacks compile time safety. Avoid if possible.
  3. Kotlin Synthetic: really a elegant cached version of findViewbyId. Better performance and way less boilerplate but still no compile time safety. Best option if compile time safety is not needed.
  4. ViewBinding: Somewhere in between option 1-3 and Databinding. But lackspowerful options of DataBinding (like 2 way data binding and using variables inside XML files).
  5. Data Binding: most powerful option. Integrates really nice with LiveData and ViewModels (JetPack) to create reactive UI's (similar to RxJava). Can slow down build times (uses annotation processor just like ButterKnife) on large projects/UI's. My personal preference.

See also: https://www.youtube.com/watch?v=Qxj2eBmXLHg

Funny to note that Jake Wharton (original author of ButterKnife) has now joined Google and works on ViewBinding.


I can't flag this question as a duplicate, as you're asking multiple things that have been answered / discussed under different questions.

What are the pros and cons of each view binding approach in Kotlin?

This has been discussed here.

How does Kotlin Android Extensions handle view binding for nested views via include? ex: For an Activity using activity_main.xml, how would View custom1 be accessed?

All Kotlin Android Extensions does is call findViewById for you. See here.

Does Kotlin Android Extensions play well with the RecyclerView + ViewHolder pattern?

Yes, it does. However, you have to use save the Views you get from it into properties, as there is no cache for them like in Activities or Fragments. See here.


If you still have unanswered questions, feel free to ask for clarification.