Меню

Cannot resolve symbol android studio ошибка

Android Studio 0.4.2 was working fine and today I opened it and almost everything was red and the auto-completion had stopped working. I look at the imports and AS seems to be telling me it can’t find android.support.v4 all of a sudden (offering me the option to remove the unused imports). (android.support.v7 seems to be fine though).

Things I have tried:

  • Rebuilding the project
  • Cleaning the project
  • Syncing with Gradle Files
  • Closing the Project, closing AS and relaunching / reopening
  • File > Invalidate Caches / Restart
  • Examining Lint, didn’t see anything obvious
  • Double checking all support libraries are up to date in the SDK manager
  • Examining my Build.gradle, although no changes and it’s the same as usual, the way it was working all the time.

Here it is in case it’s relevant:

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

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.0'

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
    compile 'com.android.support:gridlayout-v7:19.0.0'
    compile 'com.android.support:appcompat-v7:19.0.0'
    compile 'com.google.android.gms:play-services:4.0.30'
    compile project(':libraries:facebook')
    compile files('libs/core.jar')
}

When I hit «Sync with Gradle» and open «Project Settings» I get a red error telling me I have duplicate library references and to remove the unused ones..
enter image description here

My project compiles and runs fine but I really need the autocomplete working!! Does anyone have any suggestions?

enter image description here

AndroidDev's user avatar

AndroidDev

8132 gold badges9 silver badges10 bronze badges

asked Jan 13, 2014 at 20:24

Daniel Wilson's user avatar

Daniel WilsonDaniel Wilson

18.6k11 gold badges84 silver badges127 bronze badges

8

None of the things mentioned earlier here did actually work for me. But then I found this menu entry in the file menu Invalidate Caches/Restart which appears to have fixed the problem.

I don’t really know what happened in the background but when Android Studio started up again the status bar said Indexing... for a minute or so which apparently did wonders.

For reference I’m using Android Studio 0.5.4.

Siddharth's user avatar

Siddharth

9,28215 gold badges86 silver badges144 bronze badges

answered Apr 7, 2014 at 0:01

Victor Häggqvist's user avatar

Victor HäggqvistVictor Häggqvist

4,4993 gold badges27 silver badges35 bronze badges

10

You’ve already gone down the list of most things that would be helpful, but you could try:

  • Exit Android Studio
  • Back up your project
  • Delete all the .iml files and the .idea folder
  • Relaunch Android Studio and reimport your project

By the way, the error messages you see in the Project Structure dialog are bogus for the most part.

UPDATE:

Android Studio 0.4.3 is available in the canary update channel, and should hopefully solve most of these issues. There may be some lingering problems; if you see them in 0.4.3, let us know, and try to give us a reliable set of steps to reproduce so we can ensure we’ve taken care of all code paths.

answered Jan 13, 2014 at 20:39

Scott Barta's user avatar

Scott BartaScott Barta

78.7k24 gold badges179 silver badges161 bronze badges

22

Go through the link and it worked for me.

1.) Choose File option from menu on left top side of android studio.

2.) Select the option : «Invalidate Cache/ Restart..» It will open an dialog.

3.) Click on the first button with option : «Invalidate and Restart»

4.) It will close the studio and restart it. Start indexing of the project.

It resolved my problem:

File -> Invalidate Cache/ Restart -> Invalidate and Restart

Source:
https://www.youtube.com/watch?v=FX_gCTpqhwM

Satan Pandeya's user avatar

answered Aug 16, 2016 at 12:30

Manmohan Soni's user avatar

Manmohan SoniManmohan Soni

6,3122 gold badges22 silver badges29 bronze badges

4

Android Studio 1.3

  1. Open Module Settings
  2. Click on your module under Modules menu
  3. In the properties tab, set the Source Compatibility and Target Compatibility to your java version.

I did nothing else and it worked for me.

answered Aug 5, 2015 at 12:52

Mathieu de Brito's user avatar

Mathieu de BritoMathieu de Brito

2,4362 gold badges24 silver badges30 bronze badges

4

For me it was a «progaurd» build entry in my build.gradle. I removed the entire build section, then did a re-sync and problem solved.

answered Nov 4, 2014 at 23:02

Post Impatica's user avatar

Post ImpaticaPost Impatica

14.2k8 gold badges64 silver badges75 bronze badges

2

I’m using Android Studio 3.1.4 and I was experiencing such issue when moving from my develop branch with a lower api target to another branch with target api Oreo. I tried the first solution which worked but it is quite tricky, while the second solution did not solve the problem.

My Solution
When the problem popped back again I have tried to modify slightly my app gradle file enough to AS to ask me to sync files, and that did the trick. Then I deleted the change.

I guess that «Sync Project with Gradle Files» might work as well but I haven’t tried it myself

Hope it helps

answered Aug 29, 2018 at 9:21

Aenon's user avatar

There is a far easier solution built into Android Studio, and it usually works for me without needing any brute force solution mentioned in other answers — so you should try this first:
enter image description here

click on the «gradle» symbol on the right side of Android Studio, and then on the «Refresh all Gradle projects» tool.

answered Jul 3, 2018 at 14:50

Amir Uval's user avatar

Amir UvalAmir Uval

14.1k4 gold badges49 silver badges74 bronze badges

Thought i’d throw this out there too:

the thing that worked for me was changing my build variant back to a variant that was working previously. For some reason i had changed this before (and i forgot why).

either way, the best thing to do is to try to remember what you changed that day (it could be something as minor as cleaning, or going back to a previous git commit)…etc.

it also helps to try to resync gradle and force a rebuild.

answered Aug 6, 2014 at 7:25

David T.'s user avatar

David T.David T.

21.4k22 gold badges69 silver badges122 bronze badges

I tried cleaning the project and then invalidating the cache, neither of which worked. What worked for me was to comment out all my dependencies in build.gradle (app), then sync, then uncomment the dependencies again, then sync again. Bob’s your uncle.

answered Mar 28, 2019 at 14:57

CKP78's user avatar

CKP78CKP78

6307 silver badges19 bronze badges

Struggled with the same problem for a couple hours this morning. Building my project from command line seems to have done the trick for me.

Exact steps —

  1. Cloned fresh repository (no Android studio files are in repo)
  2. Built debug project from command line ( ./gradlew clean assembleDebug )
  3. Open Android Studio, import project

To check if it worked, look in your projects exploded-bundles folder, inspect a library and find the classes.jar. If it is expandable, then everything is going to be ok.

edit — I found after doing a clean within Android studio, it broke again. So if you have to clean, you will need to do this process again.

answered Jan 14, 2014 at 17:31

Kevin Grant's user avatar

Kevin GrantKevin Grant

2,30123 silver badges17 bronze badges

1

None of these methods helped me in Android Studio 0.5.8.

My solution was to delete ~/.AndroidStudioPreview directory (in Ubuntu). Sorry, I have no idea where is it in other OS. This directory stores temporary files and Android Studio settings, so I missed all my settings. But it works!

answered May 27, 2014 at 14:12

DmitryDzz's user avatar

DmitryDzzDmitryDzz

4164 silver badges7 bronze badges

1

Another way is to download JDK 1.7 and change the path from Android Studio in the error message..and choose Home folder who is contained in Jdk 1.7 folder

answered Aug 9, 2014 at 10:36

Panos's user avatar

PanosPanos

111 bronze badge

Got the same problem today. Fixed it by changing the jdk location in the project structure from javajdk1.7.0_05 to javajdk1.7.0_25 (which I didn´t know existed until now).

I´m using Android Studio 0.8.6.

Manu's user avatar

Manu

4,3385 gold badges41 silver badges76 bronze badges

answered Sep 3, 2014 at 8:23

opened_eyes_media's user avatar

I had the same problem, none of the solutions listed here worked. The problem was my source files where not inside the right folder.

The directory structure MUST be :

[project][module]srcmainjava[yourpackage][yourclass.java]

answered Oct 7, 2014 at 12:29

Rémy DAVID's user avatar

Rémy DAVIDRémy DAVID

4,3136 gold badges25 silver badges27 bronze badges

I tried everything listed here.
Then I checked my androidmanifest.xml
I’d had some stoopid mismatched due to folder renames & package renames.

answered Dec 10, 2015 at 1:29

JDOaktown's user avatar

JDOaktownJDOaktown

4,0847 gold badges38 silver badges50 bronze badges

I fixed this by removing the settings.gradle from my module subproject. It’s a Java Gradle project with it’s own settings.gradle file that somehow it screws it up.

Thanks to this guy: https://stackoverflow.com/a/33978721/425238

Community's user avatar

answered Oct 8, 2016 at 15:34

whitebrow's user avatar

whitebrowwhitebrow

1,95520 silver badges22 bronze badges

I had a much stranger solution. In case anyone runs into this, it’s worth double checking your gradle file. It turns out that as I was cloning this git and gradle was runnning, it deleted one line from my build.gradle (app) file.

dependencies {
     provided files(providedFiles)

Obviously the problem here was to just add it back and re-sync with gradle.

answered Feb 27, 2018 at 18:01

Blaze Gawlik's user avatar

I faced similar problem but I followed following steps in my case :-

1).inside project under .idea folder open modules.xml.
2).check if there are two entries for same iml file.
3).delete one of duplicate entry and close android studio or build gradle file again.

In my case it worked.
Hope it helps

answered Sep 3, 2014 at 8:08

Gagandeep Singh's user avatar

Gagandeep SinghGagandeep Singh

17.2k1 gold badge16 silver badges18 bronze badges

I got it solved by setting JDK. I got a pop up saying that Setup JDK when I placed mouse over the error.

answered Oct 1, 2014 at 4:41

varghesekutty's user avatar

varghesekuttyvarghesekutty

9972 gold badges11 silver badges23 bronze badges

I have finally figured out what causes this issue.

Actually, you should avoid pushing .idea/libraries folder to your repository. It creates weird stuff in Android Studio which tends to remove all downloaded libraries.

If you have commit history, just recreate all missing library files and avoid them to be committed again. Otherwise, just remove whole .idea folder and reimport it into AS.

answered Dec 17, 2014 at 2:47

Adrien Cadet's user avatar

Adrien CadetAdrien Cadet

1,2812 gold badges14 silver badges21 bronze badges

try to change your build.gradle with these value:

android {
compileSdkVersion 18
buildToolsVersion ‘21.0.1’

defaultConfig {
    minSdkVersion 18
    targetSdkVersion 18
}

answered Jun 19, 2015 at 8:12

Fakher's user avatar

FakherFakher

2,0893 gold badges28 silver badges45 bronze badges

In my multi-module project, the problem was that version of «com.android.support:appcompat-v7» in module A was «22.0.0», but in B — «22.2.0».

Solution: make sure
1. version of common libraries is same among modules.
2. each of modules compiles without any errors (try to build each of them from CLI).

answered Jan 13, 2016 at 19:28

surlac's user avatar

surlacsurlac

2,9632 gold badges22 silver badges31 bronze badges

Be aware that the name of files and specially folders can result in this error. For instance, if you have a folder «Helpers» and the package for files in this folder is «com.whatever.helpers» it won’t result in any compilation error but will cause the Android studio fail to load the symbols in that folder.
One typical reason for that is when you name a folder with first capital letter, commit it on Git and later change the folder name to all lower case. The Git system won’t recognize the change resulting in a discrepancy between the local and remote repositories that only the new repository clones will be affect by.

answered May 3, 2016 at 17:22

Eduardo Cobuci's user avatar

Eduardo CobuciEduardo Cobuci

5,6114 gold badges24 silver badges27 bronze badges

I had a similar problem when I rebuilt an aar file and replaced the older one in my project with the new one. I went through all the solutions here and none solved my issue. I later realised that minifyEnabled had been set to true in the library project which effectively removed a lot of dead code that was not being used in the library project.

My solution was to set minifyEnabled to false in the library project, assemble the aar, copied it into my project, invalidated caches and synced the grade project and everything worked fine.

answered Oct 1, 2019 at 12:03

Clement Osei Tano's user avatar

None of these solutions worked for me.
I had to uninstall Android Studio all together, then remove all Android Studio related files (user files), then reinstall it again.

answered Aug 29, 2020 at 20:04

hiddeneyes02's user avatar

hiddeneyes02hiddeneyes02

2,3721 gold badge30 silver badges56 bronze badges

I use shared preferences, but Android Studio complained about Editor symbol. Then, I added

import android.content.SharedPreferences.Editor;

and symbol is cool now.

answered Aug 21, 2015 at 4:20

fullmoon's user avatar

fullmoonfullmoon

7,8115 gold badges40 silver badges58 bronze badges

Please check if you have a project path which has special characters like ! (exclamation mark).

In a similar problem that I experienced, this was the root cause — since many Java applications seem not to tolerate such special characters (For e.g. doing a ‘gradlew clean’ from the terminal would fail and throw a RunTimeException.).
None of the other solutions posted online had helped me. But, once I had removed the ! from the path and did a clean build, Android Studio magically worked.

Community's user avatar

answered Sep 17, 2015 at 1:13

Jim C's user avatar

Jim CJim C

1,7471 gold badge13 silver badges13 bronze badges

You must have encountered the error “Cannot resolve symbol R” many times while building android projects. When you first create a new activity or new class, The R is red and Android Studio says it can’t recognize the symbol R. So you may hover over the R symbol, press Alt + Enter to import missing appropriate files. But doing so doesn’t fix this error. The letter R stands for the Resource. This error occurs because of the incapability of the build process to Sync Resource files with your projects. Usually, this happens because of the improper build of the project. 

Why There is a Need to fix “cannot resolve symbol R” in Android Studio?

The error “Cannot resolve symbol R”  in Android studio has faced mostly, when you shift your code to another Computer or send the code to another person. The “R” turns red and can’t run the Application and throw the error in “Logcat”. Most often “R cannot be resolved” error appears if there is an issue with some of your resource files. Due to this error, you are unable to build your application. That’s why we need to solve this error as it not getting away by just doing a simple restart or hitting Alt+Enter.

Now the point that comes here is how we can fix “cannot resolve symbol R” in Android Studio. So in this article, we are going to discuss six different methods to fix “cannot resolve symbol R” in Android Studio. 

Method 1

Try the sync Gradle. Just follow this path: File > Sync project with Gradle Files.

Method 2

Change Gradle version: Open your build.gradle file, search for gradle version and change the version. Say suppose you are using minSdkVersion 8 then change it to 9 and then build your project and also try to change buildToolsVersion. To change it follow this path: File > Project Structure > Modules.

After selecting the appropriate version click “OK“.

Method 3

Make sure your package name is correct in AndroidManifest.xml. Because sometimes, the R file is not generated because of package name in AndroidManifest does not match with the package module that you have. You might need to check  XML Files specifically if you have followed the correct syntax for ids. Then clean the project.

Method 4

Step 1: Clean the Project

To clean project: Click on Build > Clean project   

Step 2: Rebuild Project

Click on “Build” and click on “Rebuild Project

Method 5

You may import com.example.your_project.R file in all your activities, do note that this file is not Android.R but your project R file. All you have to do is add this line to your activities import com.example.your_project.R.

Method 6

Try Invalidate caches and restart. Just click on Files from the top left and choose the “Invalidate Caches / Restart” option.

1. Class name

If you do misspell in the class name, the class won’t get imported properly, and the compiler won’t be able to find the class. These names are case sensitive and you must be specifically careful with them, or the class simply won’t get recognized by the compiler.

Another probability is that the class is not available in the current working directory. This happens after a renaming of package, so be sure that all the required imports got renamed, and check for if the CLASSPATH has ‘.’ ‘the current directory’ in it.

2. Variable name

If the issue is in a variable name, then most probably you did not declare your variable before you try to use it. This is also case sensitive, and so check for if you did misspell it.

Another chance is that the variable is written in the wrong scope. Like if you declare the variable in the method A and try to access it from method B. If you need to access the variable in both the methods, you should make it common to both by declaring it in the main method.

class A {
int a=5;
}
class B {
System.out.println(a); //a cannot be resolved
}
}

3. Methods

You might have given the method the wrong parameter datatypes, For example, calling an integer where you need a String value.

Another problem might be when you try to invoke a constructor that is not within the scope.

class A
{
private A(int a)
{
}
}

A a=new A(5); //this won’t work, unless you change the scope ‘private’ to ‘public’

A a=new A(“Hello world!”); //this won’t work, unless you change the Parameter type to String

This should be written like

class A
{
A(String a)
{
}
}

4. Missing resources R

All the files in the resources directory are assigned an integer value in a common class file called R.java, which is auto generated at build time. This acts as a pointer to all the files in the resources folder. The compiler during compilation simply checks for the R.java file and picks the correct resource from the resources directory using this R.java class file.

Once the compiler picks up the wrong value, it eventually misses the resource file and that is when missing R problem is notified. This happens if you accidentally delete the R.java file, or after a package rename if the CLASSPATH miss the ‘.current directory’.

When it comes to Android Studio or IntelliJ, a simple Invalidate and refresh of the IDE’s caches might address the problem.

  • You can safely delete the ‘build’ folder that is in the app directory.
  • Then File -> Invalidate Caches / Restart
  • After the project is reloaded and indexed, do a clean and rebuild from the build menu, so that build folder will be regenerated and the missed Resources error is gone!

It will still show “cannot resolve R” error, only if you have performed refactoring the package name before. In such case, try importing the R by pressing ALT + ENTER simultaneously.

Not only you, but many programmers are also facing a “cannot resolve symbol r” error in Android app development. An “r” turns red and can’t run the Application and throw the error in “logcat”. This problem has faced mostly the time when you shift your code to another Computer or send the code to another person.

Cannot resolve symbol r Android Studio

Where the letter R stands for the Resource. This error occurs because of the incapability of the build process to Sync Resource files with your projects. Usually, this happens because of the improper build of the project.

Solutions of Android resolve symbol r

There are many ways to remove error “cannot resolve symbol r“, Here are some:-

  • XML File – there is a problem with your XML.
  • Gradle Files – Not proper sync or different version.
  • Caching –  Invalidate caches
  • Package name – Changed the package name.
  • Libraries/Modules – Downgrade some libraries which make resource conflict.
  • Lowered compileSdkVersion and targetSdkVersion
  • OLD Android Studio
  • Clean and Rebuild Project

We will look at a solution for it one by one with a step. Chances are any one of them will solve it.

XML File

If your resource file XML file even minor miscoded then you will face this problem. Check your resource layout and other XML. Android Studio turns this file into a red color. Then check the file and code of it where is the problem.

The Gradle Files Sync

Try the sync Gradle, may this solve the “cannot resolve symbol android studio“. Follow this step:-

  • Toolbar -> File -> Sync project with Gradle
Sync project with gradle option to resolve symbol r
  • OR direct click on the icon -> Sync project with Gradle
Sync project with gradle

Note: A Screenshot is from Mac OS, so there is a chances position of the button and icon change in Linux, Windows, and other os.

Still not working check the Gradle Version and update to the latest one.

Caching

It can be a problem of “cannot resolve symbol r android studio“, let try this also.

  • File -> Invalidate caches and restart.

Invalidate caches and restart

Package name

This is a very common mistake done by a developer changing a package name in manifest or change class location package. Let’s see if I change if the package in the AndroidManifest.xml.

Changing -> com.eyehunts.myapplication to com.eyehunts.a.myapplication

Output and code structures:

package name to resolve symbol r

Libraries/Modules

A lower or upgrade version of any library can make generate this error. To prevent r error in android, check Module Grable and your project SDK and target version.

OLD Android Studio

Recently I face this cannot resolve symbol r issue, I tried all the ways mentioned in this tutorial. In the last, I did update my Android studio and the problem was solved.

You can check your Android studio version and update it:-

  • Click -> Android Studio -> Check for updates …
  • If update available -> Click -> Update & restart.
  • If the delete old version option came after installation new patch, then do delete all. It’s just taking space in your computer hard disk.

Clean and Rebuild Project

Last but can be the first option to do solve error –can not resolve symbol r.

Almost everyone suggests this process. It may work if there is no package, version, Gradle version, SDK version, etc problem.

Let’s see step to do it:-

  • Click on Build -> Clean project
Clean project android studio
  • Click on “Build” and click on “Rebuild Project”
Rebuild Project android studio

If still facing the issue – “How to solve cannot resolve symbol r in the Android Studio”, then comment below. And any other solution you have, then comment. We will update this tutorial.

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)

Coding in Java and kotlin

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Troubleshooting

Problem

When you work in Android Studio 1.1.0, the build.gradle project does not successfully compile the Mobile Quality Assurance SDK.

Symptom

In the manifest (AndroidManifest.xml) file, if you hover over the application activities for Mobile Quality Assurance, the

Cannot resolve symbol

error message displays for each subpackage of com.

For example, the error message

Cannot resolve symbol ‘ibm’

displays if you hover over the ibm subpackage of the following application activity:

<activity android:name=»com.ibm.mqa.ui.ProblemActivity»/>  

Java import statements for Mobile Quality Assurance display the same error.

For example, adding import com.ibm.mqa.MQA to the Activity class causes the

Cannot resolve symbol ‘ibm’

error message.

Cause

If the SDK is not compiled, then Mobile Quality Assurance module dependencies for the application are not located.

The failure to locate dependencies causes errors such as «Cannot resolve symbol» to display in the AndroidManifest.xml application activities and the Java import statements for Mobile Quality Assurance.

Environment

Android Studio 1.1.0

Resolving The Problem

WORKAROUND:

To circumvent the problem, change the version of the com.android.tools.build:gradle classpath entry from version 1.1.0 to 1.0.1.

To modify the classpath, complete the following steps:

  1. Expand the Gradle Scripts entry in the Android Project view.
  2. Open the Project build.gradle file.
  3. Locate the following classpath entrycom.android.tools.build:gradle:1.1.0.
  4. Modify the classpath entry version from 1.1.0 to 1.0.1.

    You now have the following classpath entry:

    com.android.tools.build:gradle:1.0.1.

  5. Sync the project by clicking Sync Now (in the top right corner of the editor)

When the synchronization completes, the packages resolve.

[{«Product»:{«code»:»SWL10″,»label»:»IBM MobileFirst Quality Assurance»},»Business Unit»:{«code»:»BU053″,»label»:»Cloud & Data Platform»},»Component»:»General Information»,»Platform»:[{«code»:»PF025″,»label»:»Platform Independent»}],»Version»:»6.3;6.3.0.1″,»Edition»:»»,»Line of Business»:{«code»:»»,»label»:»»}},{«Product»:{«code»:»SSJML5″,»label»:»IBM Mobile Quality Assurance for Bluemix»},»Business Unit»:{«code»:»BU059″,»label»:»IBM Software w/o TPS»},»Component»:»General Information»,»Platform»:[{«code»:»»,»label»:»»}],»Version»:»»,»Edition»:»»,»Line of Business»:{«code»:»LOB21″,»label»:»Public Cloud Platform»}}]

Because I’m working on starting to document my learning as I go along, I decided to start recording as I attempted to fix an error I came across in Android Studio.

I came back to work on a appproject that I had forgotten about for a while, only to find there was an cannot resolve symbol error on two different android classes.

So here’s what I did

I realized that the SDK was outdated, so I updated it to the current version in build.gradle file. And while that didn’t directly solve the error I was getting, I think it was necessary because the project’s software tools would’ve still needed to be fully up to date.

What really helped with getting rid of the error was this video on YouTube.

Basically, fixing the cannot resolve symbol error takes three steps.

  1. Click “File” ->
  2. “Invalidate Caches…”
  3. and select the “Invalidate and Restart” option

And that’s it!

If you want to watch how I went about it, check out the video.

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Cannot read property tolowercase of undefined ошибка
  • Cannot read property createobjectasync of undefined ошибка при подписании