Home "cannot resolve symbol R" in Android Studio
Reply: 30

"cannot resolve symbol R" in Android Studio

ez4nick
1#
ez4nick Published in 2013-06-11 21:18:01Z

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.

UPDATE: Solution: At the time android studio was brand new and I was also a brand new developer. I should never had been using android studio, I never realized how unfinished it was. Thanks everyone for the support in trying to help since this has been posted. Ironically the actual answer to this question has been deleted and cannot be undeleted. I feel that my original answer is the solution to this problem.

My original answer that was deleted:

I think I have figured out the issue. The issue is with Android Studio, every time I added the admob library all these errors started. All my other projects without ads don't have similar issues. I'm now using Intelij and staying away from Android Studio

invertigo
2#
invertigo Reply to 2013-06-11 22:59:50Z

Have you updated your SDK tools recently? Launch the android SDK manager and make sure you have the latest SDK tools, which is now separate from the platform tools. I had this same issue when I first updated my SDK manager, the SDK build tools package did not show up for install/update until I closed and reopened the SDK manager.

Yvette Colomb
3#
Yvette Colomb Reply to 2017-10-13 02:50:48Z

I had the same problem, and it happens when I create a new project.

What I do is:

  • check for SDK updates
  • then android studio updates,
  • then reopen the project
  • open the andoridmanifest.xml
  • erase a space between a "_>" in the android:label and save.

That works for me.

Tevin J
4#
Tevin J Reply to 2013-08-12 07:34:37Z

This notation seems to work fine.

android:id="@+id/viewID"

Android Studio's design panel doesn't seem to work well.

tokhi
5#
tokhi Reply to 2015-02-23 14:29:06Z

I had this this issue too. A simple 'gradlew clean' and 'gradlew build' did the trick.


Click on Build->Clean Project and that will perform a gradle clean

Yvette Colomb
6#
Yvette Colomb Reply to 2016-03-16 18:21:58Z

Apparently in my case the problem was resolved by adding an "*" at the end

import android.R.*;
Ryan Heitner
7#
Ryan Heitner Reply to 2013-09-24 14:54:10Z

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
Mike Hays
8#
Mike Hays Reply to 2013-10-22 20:09:09Z

On OS X with Android Studio 0.2.13, I did "Jump to Declaration" on the com.< project >.R import in one of my activities. When I went back to the activity file all the warnings were gone.

Edit: You need to build the current build variant before this will work.

Tom C.
9#
Tom C. Reply to 2013-11-19 09:51:51Z

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

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

Rubin Nellikunnathu
10#
Rubin Nellikunnathu Reply to 2016-04-21 15:32:12Z

There seems to be many causes for this issue. Recently, I added an activity to test with called NewActivity. This created a file called res/menu/new.xml and gradle did not like that name because new is a reserved word.

gradlew clean

or

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

Those are definitely good things to try, but you may have some other issues you need to resolve before you run those commands.

user3063582
11#
user3063582 Reply to 2013-12-03 22:40:08Z

I had my XML files in a LAYOUT folder instead of MENU folder.

This was as a result of moving to Android Studio from Eclipse.

Solution for me was simple ... move my XML files to the MENU folder and recompile.

ayal gelles
12#
ayal gelles Reply to 2014-01-05 13:36:15Z

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...

thefreeman
13#
thefreeman Reply to 2014-03-01 13:09:24Z

None of the above answers helped me. I finally realized that the issue was that the "build" folder was missing from Android Studio. I found a number of lines in the projects "iml" file excluding "build/*". After removing those, the build file appeared and the errors disappeared.

ninjahoahong
14#
ninjahoahong Reply to 2014-03-12 08:03:58Z

I think if you put your Activities files to another folder than the default one. You need to import the com.example.yourproject.R (this is your project R file NOT Android.R file) to ALL activities using R. For example, in MainActivity file insert this line:

import com.example.yourproject.R;

user1137313
15#
user1137313 Reply to 2014-03-21 10:07:13Z

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.

Minto
16#
Minto Reply to 2014-04-03 03:43:43Z

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..:)

xhenryx14
17#
xhenryx14 Reply to 2014-05-21 15:07:19Z

Make sure you have AndroidManifest.xml in the project.

It happened to me that I imported a project that didn't have the manifest yet so Android Studio wouldn't recognize it as an Android project. Then I pulled the project in GIT and it downloaded the manifest but still not working. Then I restarted Android Studio and it said something like "Framework detected android".

So make sure your project is configured as an Android project.

Tomap
18#
Tomap Reply to 2014-07-07 15:29:26Z

I followed everything above and it didn't work. In my case I had to setup the JDK when I had every single Java Function in red for the Android-L preview...

On Mac:

  1. Click once on what is Red (or underlined) in your code (i.e. Activity, String).
  2. Click on the Red Light Bulb Point that appears on the left.
  3. Select "Setup JDK".
  4. Verify if annotations.jar and android.jar are in the current SDK version you are using (my case SDK 20 Android-L). You should also have the "res" folder, but I somehow still had it in the settings.
  5. If not, select the SDK and press "-".
  6. Then press "+" and locate the SDK folder on your computer (Mac: Applications > Android Studio.app > sdk).
  7. Select the version of the SDK you require (in my case 20 Android-L).
  8. Press Add.
  9. Build and Sync.

Then everything came back to normal for me.

Pervy Sage
19#
Pervy Sage Reply to 2014-07-23 21:11:16Z

I had to import my R package in android studio. For ex: import com.example.<package name>.R

Paris G.
20#
Paris G. Reply to 2014-10-01 16:14:51Z

In my case, after having the same error (Cannot resolve symbol R) with android studio version 0.8.9, what I did was go to File->Project structure and changed the JDK location to oracle JDK which I manually installed. So in my case in linux instead of /usr/lib/jvm/java-7-oracle which was the preset one. I changed it to /home/phvz/App/jdk1.8.0_20 saved the settings and the R symbol issue was fixed immediately

Arunendra
21#
Arunendra Reply to 2015-01-16 13:45:32Z

Just clean your project and Sync Project with Gradle File.

And the problem will be resolved.

Illegal Argument
22#
Illegal Argument Reply to 2015-02-20 05:31:35Z

In my case the code was working properly before I updated android studio. After update R file was not generated. I was working on a custom view and had an attribute

<attr name="showText" format="boolean" />

in my declare stylable. This same attribute was also used by by the System's ui:

<style name="Base.Widget.AppCompat.CompoundButton.Switch" parent="android:Widget.CompoundButton">
    <item name="track">@drawable/abc_switch_track_mtrl_alpha</item>
    <item name="android:thumb">@drawable/abc_switch_thumb_material</item>
    <item name="switchTextAppearance">@style/TextAppearance.AppCompat.Widget.Switch</item>
    <item name="android:background">?attr/selectableItemBackgroundBorderless</item>
    <item name="showText">false</item>// this line
</style>

I renamed the previous showText to showTextFormat then gradle sync which regenerated the R.java file.

Tony Tawk
23#
Tony Tawk Reply to 2015-02-25 13:41:29Z

This is caused of recently deleted xmls In Android Studio Clean your project from these xmls by choosing: build -> clean project

Agafonova Victoria
24#
Agafonova Victoria Reply to 2015-02-26 13:20:02Z

I added some images to drawable resourses, and one of my images was named as "super.jpg", which turn out to be a cause.

Renaming the image and after that syncronization the project with gradle files fixed the error.

LOLapalooza
25#
LOLapalooza Reply to 2015-03-09 19:59:36Z

None of these worked for me on Ubuntu 12.04. I had to do the following:

sudo apt-get install lib32stdc++6
sudo apt-get install lib32z1

After that, I followed some other advice in this thread, did Build -> Clean Project and everything was fine.

Iurii
26#
Iurii Reply to 2015-03-10 22:12:23Z
  1. Check your xml files.
  2. Clean Project.

This is it.

For example I had an entry in my strings.xml:

<string name="A">Some text</string>

And in activity_main.xml I used this string entry

<TextView
android:id="@+id/textViewA"
android:text="@string/A"/>

While'd been working with project, I deleted that

 <string name="A">Some text</string>

And consequently activity_main.xml became not valid. As the result: cannot resolve R.

So, check your *.xml files and clean project.

frederick99
27#
frederick99 Reply to 2017-01-21 23:52:37Z

I have similar problem and here is what I did:

clean project and Sync Project with Gradle ,

check buildTools version in my sdk

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

Paul Moore
28#
Paul Moore Reply to 2015-06-07 16:09:26Z

I had the unresolved R problem because of the same image.png file in two places. One in res>anim and the same file in res>drawable-hdpi. This caused R to become unresolved. I created the problem by making a splash screen image and repeatedly moving it from the Moray graphics directory to the Android project directly. I must have dropped it into res>anim folder by accident. I fixed the problem by removing the duplicate image file from the res>anim folder and Android studio corrected itself.

TBridges42
29#
TBridges42 Reply to 2015-09-29 02:05:07Z

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.

Fakher
30#
Fakher Reply to 2015-08-20 13:40:01Z

i did make project and it works but before that try to check the package name in your manifest, rebuild and clean your project. if the problem didn't resolve, than make your project

timmyt123
31#
timmyt123 Reply to 2015-09-02 22:10:40Z

I had android:textAllCaps = "false" in the androidmanifest which was creating havoc to the R file. I deleted it and all is fine. I think the target sdk was not set high enough.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.3927 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO