escargot agile
escargot agile

Reputation: 22379

Android Studio marks R in red with error message "cannot resolve symbol R", but build succeeds

In every project I've tried to create in Android Studio, all usages of R are marked in red with the error message "cannot resolve symbol R", but the compilation succeeds and the application runs. This is really annoying, as it blocks auto-completion and shows huge red waved lines all over my code.

I'm running Android Studio 1.7.0 and creating the project with default settings. A screenshot is attached:

Android Studio Screenshot

This is my build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

This is how the Project Structure looks like:

enter image description here

Any idea how to fix this?

Upvotes: 158

Views: 458793

Answers (30)

pathe.kiran
pathe.kiran

Reputation: 2484

Just Click on Build -> Rebuild Project option in your Android Studio.

But in some cases we may additionally need to, use File -> Invalidate Cache... and restart IDE (beside said rebuild, as something of previous "cannot resolve symbol" state may be cached).

We can auto-complete libraries because they are already built, but own project needs to be built first, and only then Android-Studio can see related resources.

#1 Rebuild is required, because Android-Studio does simply rely solely on Gradle's build-result (without static-analysis support, at least at time of writing, 2022).

#2 Clearing cache may be required, because the previous build-result (before above rebuild) may be indexed, which may make Android Studio ignore latest Gradle build-result
(this is a very good speed optimization, but a very bad Developer-experience as well).

There can sometimes be other reasons too, which is why there are many other answers posted here.

Upvotes: 113

MrWaqasAhmed
MrWaqasAhmed

Reputation: 1489

Check the version of Gradle install in Android Studio and the one mentioned in Build.Grable file

`dependencies { classpath 'com.android.tools.build:gradle:3.1.1'

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

I replace 3.1.1 to 3.0.0 and it worked for me, check your own version of Gradle. Or try to create new project and check its build.gradle file to know the installed version.

Upvotes: 0

olleh
olleh

Reputation: 2083

Many a times this usually happens when you have changed any of your layout files - where you might have included a new library component (happens generally during copy paste of code). Due to this copy-paste, you might tend to forget adding the dependency in the build.gradle file.

Hence, when you build/compile your project, Android Studio isn't able to understand that newly added component of the library (because the library hasn't become a part of the project yet).

Therefore, make sure you've added the new dependency in the build.gradle. Then do a gradle-sync.

Upvotes: 0

VAIBHAV NERLE
VAIBHAV NERLE

Reputation: 322

Just sync with gradle file.It works for me

Upvotes: 0

Shashank Bhong
Shashank Bhong

Reputation: 238

I had similar issue with 'com.android.tools.build:gradle:3.3.1', I tried all above solutions nothing worked. It was actually build:gradle 3.3.1 issue. I changed it to 3.2.1 then it worked.

So it can be issue with build:gradle version.

Upvotes: 12

TedVN
TedVN

Reputation: 556

DO NOTHING. Just Update Android Studio. Resolve my problem! Work perfectly.

Upvotes: 19

Akanshi Srivastava
Akanshi Srivastava

Reputation: 1500

Already this has a lot of useful answers but here is something which might work, it did in my case:

Remove unnecessary imports from your class then Clean project or restart Studio.

Upvotes: 0

viral 9966
viral 9966

Reputation: 525

If other solutions does not working. Install new Android Studio version.

Upvotes: 0

Lakshay Sharma
Lakshay Sharma

Reputation: 1

Recheck the naming convention of the images and the validity of the XML in your resource files- then rebuild the project after cleaning.

Upvotes: 0

Sharanjeet Kaur
Sharanjeet Kaur

Reputation: 826

  • In my case, it was gradle issue. I solved it by refreshing gradle :

  • Firstly, Click on gradle icon which right side on Android Studio

  • Then click on the refresh button

Upvotes: 0

user2912087
user2912087

Reputation: 93

You have to change the path:

for example

import it.example.project.myproject.R;

change in

import it.example.project.R;

Upvotes: 0

Yu Zhang
Yu Zhang

Reputation: 1222

Recently I import my project into Android studio, a part of R are marked in red with the error meesage "cannot resolve symbol R",but the compilation succeeds and the application runs.

Develop evirment:

  • IDE: Android studio 3.1.2
  • System: Mac Os

Solution:

step1:

Find the configuration file for Android studio
My path:

/Applications/Android Studio.app/Contents/bin/idea.properties

step2:

Change 2500:

idea.max.intellisense.filesize=2500  

to 5000 or more:

idea.max.intellisense.filesize=5000  

Upvotes: 1

Hoque MD Zahidul
Hoque MD Zahidul

Reputation: 11949

Try to first clean and rebuild the project using these command

1) Sync Project with Gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project

before doing that check if any error in the XML file. If XML file contains any error its create problem to rebuild the project and affecting the

R

Upvotes: 0

Kingsley Ijike
Kingsley Ijike

Reputation: 1399

The problem, like most people have submitted, is most likely from the .xml files. But it can be quite a chore locating it.

In my case, there was an attribute in the manifest.xml file that caused the problem. I had sleepless nights until I discovered it and simply deleted the line. The attribute was

android:fullBackupContent=""

Upvotes: 0

Snappy Cracker
Snappy Cracker

Reputation: 1481

Sometimes these build errors will continue out of nowhere. If this happens to you, try this:

Recheck the validity of the XML in your resource files: If your R.java file was not generated for the last build, you will see errors in your project wherever you reference a resource. This can be caused by a typo in one of your XML files. Layout XML is not always validated, so typos in these files may not be pointedly brought to your attention. Finding the typo and saving the file should cause R.java to regenerate.

Clean your project! Select Build → Clean Project This will rebuild the project from scratch, which may result in an error-free build.

Sync your project with Gradle! Select Tools → Android → Sync Project with Gradle Files Android Studio will rebuild the project from scratch with the correct project settings, which can help to resolve issues after changing your Gradle configuration.

Run Android Lint! Lint may give you good information regarding a problem. If the above does not work, this is where you will most likely find your problem. To run Lint, select Analyze → Inspect Code → select Whole project → OK Carefully analyze Lint suggestions; start with errors and then warnings.

Good luck.

Upvotes: 4

user5196900
user5196900

Reputation: 74

I had a similar issue for weeks but i had the design lib version with + in my build.gradle which made it download the latest version. So i set it to the version of the package and it worked.

Upvotes: 0

Bapusaheb Shinde
Bapusaheb Shinde

Reputation: 849

I resolved it by :

1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches

//imp step
5) Check your xml files properly.

Upvotes: 5

Amal lal T L
Amal lal T L

Reputation: 430

I had the same problem when I added some image files in drawable folder, where the files name was in 'Uppercase'.I renamed them to normal format name and pressed rebuild project. It worked for me.

Upvotes: 0

aex
aex

Reputation: 85

This happened to me, i found it was caused by some cache files which were invalid. so i disabled cache by adding

android.enableBuildCache=false

to gradle properties.

Upvotes: 0

Danger
Danger

Reputation: 453

One thing which works for me every time is:

go to --> build.gradle (Module: app) file of your project --> do a little change (for example: put a space somewhere and remove it back) --> then android studio will ask you to sync your gradle file--> at top right corner of the file --> select "sync now".

After syncing completes, it'll resolve the issue in most of the cases.

Upvotes: 0

craned
craned

Reputation: 3051

Make sure in your AndroidManifest.xml the package name is correct. That fixed the problem for me when my R.whatever was marked red!

I would also recommend checking all your gradle files to make sure those package names are correct. That shouldn't make everything red, but it will stop your gradle files from syncing properly.

Upvotes: 38

TheBrick
TheBrick

Reputation: 41

I had a copy/paste reuse error in the package declaration for the manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wrong.package.name">

the Main activity name had the correct path so the manifest didn't have any checked errors.

After resolving this, the source file still showed errors although build succeeded. I just ran app and then the source error indicators cleared up.

Upvotes: 2

parvez rafi
parvez rafi

Reputation: 482

this causes mainly because of errors in xml file, check your latest xml changes and rebuild it, same issue with me.

Upvotes: 2

Rihcodo
Rihcodo

Reputation: 27

It's Very important to know that android studio has two type of import

  1. java import type
  2. android studio innere import type

and our problem with android import type as import android.R

it's work good? but you did't import android.R?

android studio add this automatic but with different syntax

to solve our problem

  • firstly you need to make a new project "Blank"
  • then copy all files from your first project to the new project except java files
  • then you need to make new java class with the same name

and Easily copy the all contents of you Activity java class

"Don't copy modules and package only copy the content of class"

and don't forget to add extends .... to your class

you will see message ask you to import some of modules

you must deselect android.R and press OK

it's will work correctly

" If you find android.R only at the dialog box you must click cancel"

//See Next Image

Select Class To Import if you don't know exactly where the java class file which cased this problem do this solution with all java file in your project

//Good Luck @ ABT

Upvotes: -2

Just add this sentence in your build.gradle:

compile 'com.android.support:design:23.1.1'

Upvotes: 0

Vikash Kumar
Vikash Kumar

Reputation: 9

When I was beginner at Android studio ,this was general issue I face through . Generally the problem arises when the R.java files during conversion of hex code properly donot recognize the appropriate magic number . So we should kept in mind that all the XML files are properly wriiten.This is oftenly arises due to problem in XML files . Go in each XML files n be sure all the files are true means valid . Then go to File and synchronize the project .My error remove many times by such steps .

Upvotes: 0

Gundu Bandgar
Gundu Bandgar

Reputation: 2603

Just go to Android Top menu list. click on Build Menu, in under Build click on Rebuild Project.

enter image description here

Upvotes: 4

Shogun Android
Shogun Android

Reputation: 1

Issue : Android Studio marks R in red with error message “cannot resolve symbol R”

Solution : I resolved the 'R' error by adding the following to the import & then rebuilding the project and the error went away.

import com.mkyong.android.R;

Explanation: Sometimes the Android Studio does not add the import even when you do build multiple times or run the 'optimize import' command. Sometimes the error is resolved when you 'restart Android Studio' but at times even after restarting Android Studio multiple times the error remains. Thanks.

Upvotes: -1

When creating a project from a sample,while not importing the existing project is good (prevents clashes with gradle, .idea, .iml, build, jars and apks, i.e. all unnecessary build-generated files), after creating a new project, copying and pasting all relevant resources, I'd recommend :

checking packages and imports from packages within the project {AndroidManifest declaration and all imports in java classes}, all resources (drawable, mip-map, layouts and menus & your build.gradle (a sample of build.gradle to use with the latest sdk can be provided on request)) to see if they are there & if declared strings and ids actually exist and have been used, after which your only error should be the question asked:

Bulid->Clean Project

Tools->Android->Sync Project with Gradle Files

File->Invalidate Caches and Restart

In worst cases restarting your machine helps.

The above should work. Feel free to ask questions if necessary, i.e. post comments.

Upvotes: 2

Jay
Jay

Reputation: 131

  • Android studio-> Build -> Make Project Or Make Module 'xxx' fix the problem.
  • Yes, run app can not fix the problem...
  • Yes, clean can not fix the problem...
  • Yes, File -> Invalidate cache can not fix the problem...
  • by the way, should I use yes? or may be no is right in English?

Upvotes: 0

Related Questions