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

I am following a tutorial exactly. I gave the view a name in the layout XML file under android:id. When I type that name in Kotlin, it is highlighted in red and there is an "unresolved reference" error.

For example, in XML activity_main.xml:

<TextView
    android:id="@+id/nameTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

In Kotlin MainActivity.kt:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    nameTextView // <-- This is highlighted red with error so I can't use it!
}

I created this question because I've seen variations of it several times since the deprecation of Kotlin Android Extensions, but they are all phrased in various ways and are not very searchable. I think the common factor is a new Android programmer following a tutorial that was written before the deprecation. These tutorials generally don't specify that the feature being used is called synthetic properties or Kotlin Android Extensions, synthetic view properties, or kotlin-android-extensions, and that the feature is deprecated.


Solution 1:

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

Solution 2:

You can't directly access the view ids.

But you can use:

  1. Jetpack as Kotlin Android Extensions are deprecated. Source Kotlin Android Extensions Deprection

  2. You can use view binding. View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. Source: View Binding