Why Do I Get "Unresolved Reference" Error for My View's Name/Id When I Type It in Kotlin

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.

Kotlin Unresolved reference: btn_start but i define it in my layout


Kotlin Android Extensions is deprecated, which means that using Kotlin
synthetics for view binding is no longer supported.

Please refer to the official documentation here

Why does it still say Unresolved reference: button even when I have button id in xml?


val button = findViewById<View>(R.id.button)

“Unresolved reference” in MainActivity.kt calling a TextView?

I think you need to define on MainActivity.kt like this

val ShowMeIt= findViewById<TextView>(R.id.ShowMeIt)

Unresolved reference in kotlin with id and activity_main

In my case removing import android.R solved the issue.

Kotlin Error at runtime - Unresolved reference Object ID

Looks like you're trying to use synthetic view binding. Your project needs to have the kotlin-android-extension plugin enabled in build.gradle like:

apply plugin: 'kotlin-android-extensions'

After that you should be able to import your btnDatePicker.

Note that kotlin-android-extensions and synthetic view binding is deprecated so you are probably better off with Jetpack view binding. See https://developer.android.com/topic/libraries/view-binding/migration

Android Studio 3.1.3 - Unresolved reference: R - Kotlin

The issue can be caused by many factors,

  • as mentioned by martomstom in this Answer the issue is sometimes caused by com.android.tools.build:gradle version, changing it's version to a more stable one would solve the problem: for example: com.android.tools.build:gradle:3.4.0-alpha02 with com.android.tools.build:gradle:3.2.1

  • Also, having libraries from the same group, but with different versions may cause the problem or even more runtime errors. use the exclude group method like the following : implementation('com.squareup.picasso:picasso:2.71828') { exclude(group: 'com.android.support') } in this case, picasso library uses android.support components, the android library version used in picasso is different than the one you're currently using in your app, so in order to solve this issue, we have to exclude it completely from its sub library and class groups.

  • It can also happen by the mismatch of resources and code, including this importation line in your activity may solve the problem too : import com.package.name.R

  • Sometimes it can happen because of the IDE, performances or memory.. Cleaning the project from time to time may save you some time, on Android Studio it would be something like this : Build -> Clean Project / Rebuild Project - Cleaning IDE cash also helps with performance and memory, on Android Studio it would look like this : File-> Invalidate Chases/ Restart -> Invalidate Cashes and Restart

  • I noticed that this problem happens to me the most of the time when importing new resources, Using prohibited characters in their names would fire the error, such as . , , - , UpperCase or special Letters

  • And as a suggestion , if you're using Kotlin, i really recommend using Kotlin extensions in your activity such as : import kotlinx.android.synthetic.main.activity_page.* or if you're using a custom view : kotlinx.android.synthetic.main.view_layout.view.*
    after that, in onCreat() method of an activity , you'll only have to call the id, for example : my_edit_text_ID.text = "Kotlin Dbest!", or from a custom view : mCostumView.my_edit_text_ID.text = "Kotlin Dbest!"

EDIT :

  • I have faced this issue againe and the problem was the '' R '' library was imported from 2 different sources :

    com.android.R

    com.example.package.R

    You must only import the '' R '' library with your application package name,
    in this case com.example.package.R
    Sometimes the library is not imported at all, to import it, click on the
    unresolved reference R and press Alt + Enter

EDIT:

As tobltobs mentioned in the comments section: " Most of the time the problem is caused by another error which prevents the build system from creating generated sources. To find the root cause look at the gradle log (the "toggle view" icon below of the green hammer in the Build output) and look for errors unrelated to R or BuildConfig (also generated). If there is no other error left and the problem with R persists then maybe something of this list might help. "

EDIT:

As Patrick Beagan mentioned, Kotlin extensions are now deprecated - I'd advise using ViewBinding instead



Related Topics



Leave a reply



Submit