'Error "Cannot resolve symbol R" only in one file

When enabling View binding, I cannot reference the resource files in custom RecyclerView Adapter anymore. I get an error "cannot resolve symbol R" and it attempts to automatically import R, which is wrong. This doesn't happen in the Activity, anyway. (I've already tried checking all xml files, cleaning and rebuilding the project.)

Why is this happening?

Screenshot of Activity (everything is fine):

Screenshot of Activity (everything is fine)

The custom adapter - can't resolve symbol R: The custom adapter - can't resolve symbol R

Edit:

What I don't understand is why there is no such error when I create a project without View binding? Below is an example of a custom adapter in another test app, where no problems with R are seen:

no problems with R

The question is, in which cases is it required to import a package.R? Why isn't it always needed and how do I know that everything is correct when suddenly R doesn't get resolved in a particular file and Android Studio attempts to import the package.R?



Solution 1:[1]

I've found out that the error with R resolving begins when the Java file is located in a subfolder of the package (for example, the ui folder). Then Android Studio starts to import package.R.

For example, in this case:

package com.example.test3;

there is no R error. But in this one:

package com.example.test3.ui;

R is not resolved and gets autoimported.

Maybe this is the expected behaviour.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1