“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:25

This is a very old question, but it still happens a lot and there is no truly comprehensive answer here.

I have had this problem more times than I want to admit. It can be caused by a wide variety of issues that mostly all have to do with your project structure not matching what's expected. Here's a list of all the problems I've had:

Importing from Eclipse to Android Studio does not always work smoothly, especially with older versions of Android Studio. Make sure you're using the correct directory tree. It should look like this for a moderately mature app:

AppName/        // AppName is your app's name, obviously
    .gradle/    // This is a compiler created directory. You should normally leave it alone
    .idea/      // Ditto
    build/      // This too
    gradle/     // And this
    app/
        build/
        libs/
        src/
            androidTest/   // Tests based on the Android instrumentation runner should go here
            main/
                java/
                    fully/           // Typically com
                        qualified/   // the domain name. Longer package 
                                     // names may have deeper folder trees
                            AppName/ // All your java files go here
                res/
                    drawable-*/      // One of these for each resolution you support
                    layout/          // All of your general layouts
                    menu/            // All of your menu layouts
                    values/          // All of your resource xml files
                    xml/             // PreferenceScreen layouts go here
                AndroidManifest.xml
            debug/         
            test/          // Pure jUnit tests should go here
        .gitignore
        app.iml
        build.gradle   // This is the gradle file you should be making most changes to
        proguard-rules.pro
    .gitignore
    build.gradle       // This gradle file should mostly be left alone
    gradle.properties
    gradlew
    local.properties
    AppName.iml
    settings.gradle

It may not look like this in your IDE. There is a drop-down above the file tree that displays your files in different ways, and it defaults on my system to one called Android that breaks your files out into their different types. It's easier to fix this kind of problem from the one called Project, and even easier directly from your OS's file system, because Android Studio collapses folders that only contain another folder onto a single line.

Especially if you ever change the name of your app, you need to make sure that the source tree main/java/com/domain/AppName is updated. Also make sure that the package tag in your AndroidManifest.xml is correct.

If there are errors in either your Gradle files or your AndroidManifest.xml, this will prevent Android Studio from properly building your resource files. Gradle files can be broken by upgrading Android Studio sometimes, especially from the pre-1.0 versions. Sometimes this is because it stops supporting older versions of the Gradle plugin, so you need to update your version numbers. It can sometimes be hard to find what the current versions are. As of today, 7/17/15, my apps are compiling fine with com.android.tools.build:gradle:1.2.3. This is in the dependencies block in the outermost gradle file,

If your AndroidManifest references a non-existent drawable or string resource or activity, it will break and cause this error. Sometimes if anything references a nonexistent drawable or string resource you will get this error.

If you have a file in your resources that is corrupted, or an invalid xml file, you will get this error.

In my experience, sometimes Android Studio just hiccups for no reason, and you need to restart it and/or your PC. I don't know why, but sometimes it works.

If you have two xml resources with the same name, in directories that do not override each other, you can have this problem. For instance, you can have the same name in drawable-mhdpi and drawable-xhdpi because they override each other depending on the target device, but if you have the same name in layout and in menu, it will cause a problem. Rename or delete one of the files.

If only some resources are having this problem, those resources are most likely in the wrong directory.

In one case I had to completely reinstall Android Studio. I don't know what was wrong, but it worked.

In one case I moved my entire project to a different directory and re-imported it as a new project. I don't know what was wrong, but it worked.

Xml files with reserved words for names can cause this problem. Rename or delete them.

There are a few ways your Gradle file can end up referencing a version of the build-tools that you do not have installed. Correct this by changing Gradle or downloading the appropriate build-tools.

Finally, after you've fixed whatever is wrong, you need to clean your Gradle project. You do this by going to the Build menu at the top and selecting Clean Project.

查看更多
梦醉为红颜
3楼-- · 2018-12-31 02:25

Build > Clean Project

This worked for me. Had the same problem a few times, and this seems to set it right. Unless you have changed something or called a variable R. This issue usually happens out of nowhere, when it happens to me, so I imagine its just Android studios freaking out. haha

Have a good one, and good luck with your projects.

查看更多
呛了眼睛熬了心
4楼-- · 2018-12-31 02:25

There are many causes for this error.

  1. Sometimes it occurs for replacing an image file keeping same name.
  2. Suppose you deleted an item from your layout .xml say a <Button/> but it is still declared in any Activity or Fragment .java.
  3. Many more.

Best way to track the error is Rebuild it rather clean or sync doing some intentional error.

If it doesn't solve your problem then there must have to be some flaw or runtime error or error occurred due to improper use of resources in may be both java or xml file in your code or design which is forcing gradle to stop because (R)esource file can't index your resources properly and you have to admit that.

If your project ran before you made the changes then comment out the changes you have made and try to rebuild the project.

It will surely work since there will be no changes.

To track down the exact error, check the changes by breaking the changes into smaller module.

For example - If you are making a list visible with a button click and inserting list values in the adapter, first check if you are able to make it visible or not then check for adapter errors.

查看更多
美炸的是我
5楼-- · 2018-12-31 02:28

I have had this with

  1. An uppercase letter in my drawable resources.
  2. Import Android.R being added by Android Studio (or Eclipse)
  3. Error in xml file
查看更多
不流泪的眼
6楼-- · 2018-12-31 02:28

This was a big headache for me. In my case the error appeared after configuring "Google Play Services" in Android Studio (installing + build.gradle config). This in turn requires minsdk > 7 but the error is very unclear about it (actually a complaint about this only appears as "info"). Anyhow.. upon changing minsdk to 8 both in the manifest file and the build.gradle file and rebuilding the project, the error was gone.

So.. a general advice about this, if I may generalize - you probably have some problem in your manifest file, or some other configuration file, that is preventing a proper build. Try looking really hard at the error report, even the messages titled "info" for any hint about what it might be...

查看更多
ら面具成の殇う
7楼-- · 2018-12-31 02:28

Dudes, I think there are a lot of shots in the dark here. In some cases Clean and Sync Project will help only after you fixed the problem.

Step1: So go and look at the top of each file where the package is specified as follows pakage="YourURL.YourProject"; and insure that the correct package (Your Own Project) is specified. You will find this in java files and AndroidManifest.xml and it is critical for the java files to reference the correct package, as that package contains the resources ("R") you are pointing to. Should they not match up the error cannot resolve Symbol R occurs.

Step2: Clean, Sync, whatever. Done

So why does this occur randomly or what did I do wrong??

If you copy and paste code, you should pay close attention to the "package=" as explained above. More importantly, when you paste code, it immediately runs through a sort of debugger (Excuse my bad tech term) to show you "presumed errors", which immediately takes in consideration the "Wrong Package", and you get all the errors. Therefore, even though you immediately corrected the notation after pasting, the debugger has already refreshed. Which is why "Clean, Sync, Whatever" works SOMETIMES

查看更多
登录 后发表回答