androidxmlkotlinkotlin-android-extensions

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

  • 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, which is no longer supported or included in new projects. (See below for explanation.)

    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.


    How to fix the issue

    One thing to consider is that Jetpack Compose is becoming the default and recommended way to do UI on Android. You might consider switching to a tutorial that teaches that instead of traditional XML view layouts. But if you still want to learn traditional views:

    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.


    What if I want to use Kotlin Android Extensions anyway?

    If your project uses a Kotlin version less than 1.8, you can enable it by adding it to the plugins block of your app module's build.gradle file:

    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.

    However, many of the critical Jetpack libraries for Android development use newer versions of Kotlin, making this impossible.


    Why is it no longer supported?

    Google and JetBrains decided to deprecate Kotlin Android Extensions, meaning they no longer support it, and discourage you from using it.

    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