In every instance in all of my classes where I reference R.id.something, the R is in red and it says "cannot resolve symbol R". Also every time there is R.layout.something it is underlined in red and says "cannot resolve method setContentView(?)". The project always builds fine. It is annoying to see this all the time. I have read many other questions on here about something similar but most involved importing projects from Eclipse. I am using what I believe to be the most recent version of Android Studio and the project was created with Android Studio and worked without any "cannot resolve R" problems. I would like to know what causes this if anyone knows.


当前回答

我也有同样的问题: Android Studio 3.2 该项目编译和运行良好,但我在屏幕上得到“无法解析符号“R”的警告

没有帮助:

构建——>清洁项目 构建- >重建项目 文件- >缓存失效/重新启动

帮助:

要么

在build.gradle

dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
...
}

替换为

   dependencies {
            classpath 'com.android.tools.build:gradle:3.2.1'
    ...
    }

or

更新Android Studio到3.3

其他回答

我也遇到过同样的问题,当我创建一个新项目时,这种情况就会发生。

我所做的是:

检查SDK更新 然后是安卓工作室更新, 然后重新打开项目 打开andoridmanifest.xml 在android:标签中删除“_>”之间的空格并保存。

这对我很有用。

我今天升级了gradle版本和gradle插件版本的最新版本,它为我工作。

之后,同步项目与gradle文件。

最近,当我更新一个项目的很多库时,我就遇到了这种情况。没有找到R,但是我可以很好地构建和运行项目。它没有崩溃。IDE只是在所有地方显示红色。

我试过

Check for damaged/improper XMLs and images, none found Check for invalid resource file names (e.g. capitalized), none found Downgrade some libraries because maybe it was a resource conflict, not resolved Lowered compileSdkVersion and targetSdkVersion because, in the past, I encountered resource issues due to this. In this project however, that was not the case. Clean, didn't help Rebuild project, didn't help Invalidate caches/Restart multiple times, didn't help Adjusted idea.max.intellisense.filesize to a very high value, didn't help Did a Lint run, no errors found

最后,我发现降级我的Gradle构建工具(从3.3.1到3.2.1)解决了这个问题。这是我唯一要做的改变。其余内容已更新。现在,我所有的库都更新了,没有问题。

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        //classpath 'com.android.tools.build:gradle:3.3.1'
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.google.gms:google-services:4.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

在我的情况下,原因是有一些图像在res文件夹名称包含一个大写字母..

只要用小写字母重新命名就可以了

这不是错误。 如果你有错误 关闭项目。 然后打开项目 在此之后,您可以得到一个错误

Cannot resolve symbol 'default_web_client_id'

可以通过在sting值中插入Web客户端ID来解决