Why do I get “unresolved reference” error for my view’s name/ID when I type it in Kotlin?

The ability to refer to a view directly by it’s ID/name in Kotlin is called “synthetic properties” and it is a feature of a project plugin called Kotlin Android Extensions.

Google and JetBrains decided to deprecate Kotlin Android Extensions, meaning they no longer support it, and discourage you from using it. Ever since it was deprecated, when you create a new project in Android Studio, the plugin is no longer included in the new project. Therefore, synthetic properties are not supported in new projects and you will get an “unresolved reference” error if you try to use it.

Tutorials written between 2017 and 2020 often make use of this feature, and if they haven’t been updated, they probably don’t even mention it by name, because it was taken for granted to be an included plugin in new projects.

Google explained the reasons for deprecating it in this blog post, with these key reasons:

  • They pollute the global namespace
  • They don’t expose nullability information
  • They only work in Kotlin code

The quick and easy way to get your view reference is to use findViewById. The type of View should go inside the brackets <>. In an Activity, it looks like this:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    val nameTextView = findViewById<TextView>(R.id.nameTextView)

    // Now you can refer to the view using the variable
    nameTextView.setText(R.string.hello_world)
}

In a Fragment, you would probably be working with the view in the onViewCreated function, so you must call findViewById on the parent view. (If you need to access it elsewhere in the Fragment, use requireView() instead of view.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val nameTextView = view.findViewById<TextView>(R.id.nameTextView)
    //...
}

findViewById is probably the best option for now if you just want to complete your tutorial that was written before Kotlin Android Extensions was deprecated.

However, using findViewById can be tedious, and it is also error prone, because it won’t warn you if you are searching for a view that isn’t in the current layout. If you do, it will crash at runtime. For this reason, Google recommends using View Binding. There are a few steps to get started with view binding, but once you set it up, it is a cleaner option than findViewById. The official instructions are here.

Finally, if you really don’t care that Kotlin Android Extensions is deprecated and want to use it anyway, it currently still works OK, but you have to add the plugin to your new project to enable it. (Beware this will no longer work in Kotlin 1.8 and up.) To do that, open the build.gradle file for your app module. At the top in the plugins block, you can add a line for kotlin-android-extensions, like this:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

Then press the “Sync project with Gradle files” button in the toolbar to enable it.

Leave a Comment