javaandroidandroid-studiogradlekotlin

Android Studio 3.1.3 - Unresolved reference: R - Kotlin


I am new to Kotlin. I have converted some code from Java, but it seems like there's something wrong. The R in findViewById(R.id.my_id) is highlighted in red and it shows this message:

Unresolved reference: R

I've been looking for a solution, but I seem not to figure it out. So what should I do?

Here's a screenshot:

Enter image description here


Solution

  • The issue can be caused by many factors,

    EDIT :

    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 for finding views in layouts are now deprecated - I'd advise using ViewBinding instead