“cannot resolve symbol R” in Android Studio

2018-12-31 01:26发布

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.

30条回答
倾城一夜雪
2楼-- · 2018-12-31 02:23

Same problem. Started when I added a few images in my drawable folder and tried to access them. Also the images added were having the extension with capital letters. That seems to have crashed the build, since even if I renamed them, the message was the same and R was inaccessible. What I did is, in the .iml file I looked for the excludeFolder and removed them (like bellow):

  <excludeFolder url="file://$MODULE_DIR$/build/apk" />
  <excludeFolder url="file://$MODULE_DIR$/build/assets" />
  <excludeFolder url="file://$MODULE_DIR$/build/bundles" />
  <excludeFolder url="file://$MODULE_DIR$/build/classes" />
  <excludeFolder url="file://$MODULE_DIR$/build/dependency-cache" />
  <excludeFolder url="file://$MODULE_DIR$/build/incremental" />
  <excludeFolder url="file://$MODULE_DIR$/build/libs" />
  <excludeFolder url="file://$MODULE_DIR$/build/manifests" />
  <excludeFolder url="file://$MODULE_DIR$/build/res" />
  <excludeFolder url="file://$MODULE_DIR$/build/symbols" />
  <excludeFolder url="file://$MODULE_DIR$/build/tmp" />

After that I rebuilt the project and R magically re-appeared.

查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 02:24

In the latest versions of Android Studio, at least for me, the following works:

"Tools" -> "Android" -> "Sync Project with Gradle Files"

In latest Android Studio 3.1.3 (July 2018), "Sync Project with Gradle Files" is available in main menu bar.

enter image description here

查看更多
初与友歌
4楼-- · 2018-12-31 02:24

I have a special case for this problem.

In my project, every thing goes well, it can compile and build successfully, but in my Android Studio IDE(and I also tried Intelligent IDEA, they are in the same situation) the R.java file can not be resolved well and always be drop in red line.

Just like this: enter image description here

This almost made me crazy, I can't bear the read color when I am programing.

At last I found this tricky problem is cause by my R.java's file size. My project is very a huge one, it supports many multi-languages and multi screen sizes. There are so many resources that my R.java's file size is about 2.5M.

The max size of a file in Android Studio is 2.5M in default settings, so files bigger than this limit can't not be resolved well. And you can change the default settings in "AndroidStudio-root/bin/idea.properties".

change this line:

idea.max.intellisense.filesize=2500

to :

idea.max.intellisense.filesize=5000

Then restart Android studio, the red color dismissed, I can program happily again.

查看更多
听够珍惜
5楼-- · 2018-12-31 02:25

I have similar problem and here is what I did:

clean project and Sync Project with Gradle ,

enter image description here

check buildTools version in my sdk enter image description here

From build gradle (module ) change minSdkVersion from 8 to 9

defaultConfig {
        applicationId "PackageName"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

But all this didn't help.

Finally I found the answer (this worked in my case)

Change from build.gradle(module:app)

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.1'
......
....
}

Or

select File | Project Structure change Build Tools Version to 21.1.1

enter image description here

查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 02:25

Brother, R.java file contains the link between xml and java page. "R cannot be resolved" arise only if there is a problem with some of your resource files. So the best and effective way is delete the last done xml or drawable in res file. and then again start from there according to android coding norms. This is the only way. The effective and right way.

Also use Git for proper tracking of code. ..

Happy coding..:)

查看更多
看淡一切
7楼-- · 2018-12-31 02:25

I faced this issue when I manually renamed the domain folder of my app. To fix this issue, I had to

  1. Set the proper package folder structure of <manifest> in AndroidManifest.xml.
  2. Set the new package location for android:name of <activity> in AndroidManifest.xml.
  3. Clear cache by

File Menu -> Invalidate Caches / Restart ...

The issue will be gone, once the Android studio restarts and builds the fresh index.

查看更多
登录 后发表回答