0

This question has been asked many times before, but i have tried everything and nothing has worked. Cleaning, Rebuilding, Syncing Gradle, Editing XML.

I get the error "Cannot resolve symbol 'R'" on every file the has 'R.blah.blah' in it.

This is a line of code where the error shows up.

int gray = getResources().getColor(R.color.card_gray);

Does anyone have any idea why this is happening? I've tried restarting which hasn't worked, so I'm thinking it's a bug in the current Android Studio version.

EDIT: Android Manifest and beginning of Java file (one of the affected ones)

I have 3 manifests, one for each module (CardsUI, Silk, App), I'm making an icon pack. The error is happening in all 3 modules.

App:

Too long to post, so here is a Gist: App Manifest

CardsUI:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.afollestad.cardsui"
      android:versionCode="1"
      android:versionName="1.0">

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="18"/>

</manifest>

Silk:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.afollestad.silk"
      android:versionCode="1"
      android:versionName="0.1">

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19"/>

<application/>
<!-- required to build aar. -->

</manifest>

Example start of affected CardsUI file:

package com.afollestad.cardsui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

EDIT 2: Tried creating new project and importing files, and I get the same thing. This is getting really annoying now.... Please help someone!

Thanks

Samurai Kitty
  • 39
  • 2
  • 7
  • Update your SDK maybe? – Bidhan Jun 13 '15 at 11:05
  • @BidhanA Please read the question carefully. I said that I have tried those many many times and it hasn't worked. – Samurai Kitty Jun 13 '15 at 11:07
  • is it cannot resolved card_gray or R? – Ahmad Sanie Jun 13 '15 at 11:09
  • cannot resolve R will effect any syntax that contain R, so is there any were else that u uses R and the error popups – Ahmad Sanie Jun 13 '15 at 11:11
  • No, it's not a bug with Android Studio. Delete the import that says something like android.R and make sure the package name in the corresponding java file is the correct one. – Stephan Branczyk Jun 13 '15 at 11:15
  • 1
    Note that even if there is only one problem with the generated R file, it will prevent from generating the rest of the resources in that R file. So don't worry if it seems like there are many errors, there is probably just one or two errors, but no more. If my initial advice didn't work, isolate the piece of code that's blocking the generation of the R file by placing portions of the code in comments. You'll have to work backwards from the end of the java file to the beginning of the java file, until it eventually starts compiling. But again that advice won't work if the import is messed up. – Stephan Branczyk Jun 13 '15 at 11:27
  • @StephanBranczyk Thanks for the reply, but android.R isn't generated. There's a com.your.app.R but no android.R. The com.your.app.R says unused import statement. But every single file with R in it says it cannot resolve it, no matter what imports. – Samurai Kitty Jun 13 '15 at 11:36
  • 1
    You have error in xml files and android can't generate R file – Vigen Jun 13 '15 at 11:37
  • Delete import com.your.app.R; that's wrong, and replace with import R; – Stephan Branczyk Jun 13 '15 at 11:44
  • @Vigen There are no errors in any xml files. – Samurai Kitty Jun 13 '15 at 11:46
  • @StephanBranczyk I just tried that (import R;) and it said 'Cannot resolve symbol R' – Samurai Kitty Jun 13 '15 at 11:47
  • Post your manifest and post the beginning of your java file. Do this by editing your original question. Do not post it as a comment. Worst case scenario, just create a new project from scratch using one of the templates, and you'll see that it compiles. Then start cutting and pasting code from your old project to the new one recompiling each time (until you can find what's causing the problem). I'm too tired now. I'm going to sleep. – Stephan Branczyk Jun 13 '15 at 11:47
  • And post your color file while you're at it. – Stephan Branczyk Jun 13 '15 at 11:53
  • @StephanBranczyk Edited the post. I'll have to try and make a new project. This one is all over the place... – Samurai Kitty Jun 13 '15 at 11:56
  • That's it. The package name com.afollestad.cardsui doesn't match com.designrifts.ultimatethemeui from the manifest. Those two need to match, otherwise R won't be found. Make that change, but then don't forget to write import R; – Stephan Branczyk Jun 13 '15 at 12:02
  • @StephanBranczyk Thanks for the reply but I think you missed something. You're looking at the cardsUI file start and the App modules manifest. I imported the App module into a new project, without any libraries, and the same thing happened. So my only thought is that it's a bug in my version of android studio. – Samurai Kitty Jun 13 '15 at 12:07
  • You should build each module one at a time starting with the required dependencies first. Worst case scenario like I said, cut and paste little by little (but do not import). – Stephan Branczyk Jun 13 '15 at 12:15
  • Change your building tools version. I had a similar problem and it fixed it for me – Ilan Kutsman Feb 08 '16 at 05:18
  • Possible duplicate of ["cannot resolve symbol R" in Android Studio](https://stackoverflow.com/questions/17054000/cannot-resolve-symbol-r-in-android-studio) – Zoe Jun 22 '19 at 10:01

2 Answers2

3

You can try what is described in the images below:

enter image description here enter image description here

double-beep
  • 5,031
  • 17
  • 33
  • 41
fuuuh cghh
  • 31
  • 2
0

It could be if you have deleted

<string name="action_settings">Settings</string>

in the strings.xml, but not deleted main.xml file in the menu folder.

Andrew
  • 36,676
  • 11
  • 141
  • 113
  • Sorry, I don't quite get your answer. `Settings` isn't in my strings.xml, and I don't have a main.xml in my menu folder. – Samurai Kitty Jun 13 '15 at 12:09
  • I'd couple times cleaned up strings.xml from redundant strings and had similar problems. The reason was that related link to that string was still there (in the other xml file) – Andrew Jun 13 '15 at 12:15