Android Studio "cannot resolve symbol" but project compiles and works - android-studio

The project compiles fine. However, Android studio 3.0.1 shows reference to one particular library in red and displays "cannot resolve symbol". Even single stepping into that library module doesn't work.
I already tried various options like 1) Invalidate cache/restart 2) deleted build directory 3) clean and rebuild but nothing seems to be working.
Interestingly, when Android Studio is rebuilding indexes after invalidate/restart, it momentarily shows that library references in while but then again in red.

I tried Invalidate cache/restart or clean Project -> rebuild project. These didn't work for me.
The final solution was open Project window on the left side of IDE, under Project mode, delete .gradle and .idea folder, then sync, it works.

Following has resolved this for me.
remove library from gradle cache (in case of linux, it's in ~/.gradle/caches).
remove library dependency from the build.gradle and sync
restart (maybe invalidate cache)
add library dependency to build.gradle and sync
clean and rebuild

This is happen to me when I move my project to other pc or open the project with other pc and with different SDK folder location. Solution: close project, delete folder .gradle and folder .idea inside project folder then open your project to sync gradle.

In my case changing the Gradle version worked.
In gradle-wrapper.properties
changing
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
and in build.gradle (project level)
changing
classpath 'com.android.tools.build:gradle:3.3.0'
to
classpath 'com.android.tools.build:gradle:3.2.1'

Invalidate cache/restart didn't work for me.
Simple and working solution:
1: Remove all dependencies from App level Gradle and Sync.
2: Re-add all these dependencies and Sync.
It will work fine.

This has happened to me a couple of times, there are a few ways you can resolve this problem;
Sync project files with gradle in the File toolbar
Use the Invalidate/Restart option in the File toolbar to invalidate the caches.
Delete the .idea folder and restart Android Studio

Firsty try to Clean and rebuild project from Build -> Clean project & then Build -> Rebuild project.
If that doesn't work Invalidate Caches/Restart always works for me. Do it from File -> invalidate Caches/Restart
Then click on invalidate and restart
Hopefully this helps.

For me problem was that AS suggested I upgrade gradle version, (from 3.5 to 4.4), and then it started doing it.
Solution was to delete the 3.5 folder from .gradle in project

None of the answers worked for me
Fixed it by:
Removing & adding all java files via Project window on the left side of Android Studio
Commenting/un-commenting & syncing libraries in the gradle file in small groups.
And cleaning all caches before that (as part of other fixes here).

I've tried Invalidate Caches/Restart..., tried manually delete .idea and .build folders and it didn't work for my case.
I using VCS (git) and I just commit my changes, removed the whole project, and Checkout from Version Control. It fixed this problem.
Android Studio 3.6.3

I Tried the following in order to resolve the issue:
Invalidated Caches/Restart
deleting .idea, .gredle under project and build under app
done clean project and rebuild project
deleted the caches in the installed path
Eg. {installed path}.AndroidStudio4.0\system\caches
upgraded the studio to latest version
[I have installed as suggested by windows, so its installed same path. I might have tried new path.]
Non of these tries helped to resolved this issue. Finaly I have tried these two steps:
commented dataBinding.enabled true from build.gredle of your app as its obsolete and replaced with buildFeatures{dataBinding true}.
Deleted the content of the log, tmp and caches folder from the installed path manually. Eg. {installed path}.AndroidStudio4.0\system\caches, {installed path}.AndroidStudio4.0\system\log, {installed path}.AndroidStudio4.0\system\log
Restarted the android studio
Now all reference for the Binding and other classes were restored.

I ran into this problem after upgrading Android Plug In Version from 3.54 to 7.2.1 and Gradle version 5.41 to 7.3.3 . Reverting back to the earlier versions fixed the problem. Simply deleting the 5.41 folder in .gradle as suggested in another answer above did not work.

Update
classpath 'com.android.tools.build:gradle:3.X.X'
to the last version worked for me.

Tried all of the above but nothing worked.
Got Solution by:
1. Updating Android studio to latest version
2. Updating the Gradle version to latest one

Related

Error on some imports on Android Studio for AppCompatActivity and androidx.appcompat.app.AppCompatActivity

I was working in an android project and everything is ok, but when I try to open the project now, I got the some import errors. Take a look in the image below:
The code still compiles and I was able to run it in the Android emulator, but I can't use the IDE auto complete because of this error.
How can I fix that?
EDIT: the problem appears to be related to the Android AppCompat implementation in this line below
implementation 'androidx.appcompat:appcompat:1.0.0'
I try to change it to
implementation 'androidx.appcompat:appcompat:1.5.1'
But it generate some conflicts with another plugins.
The I return the implementation to the old value, but now when I try to put the version 1.5.1, it generates the following error:
Could not resolve androidx.appcompat:appcompat:1.5.0.
Required by:
project :app
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
Not sure if this would help but, I usually do any of the steps below when I encounter that kind of Android Studio issue.
clean/rebuild project
invalidate cache~restart
delete .idea folder inside the root project folder, and re-import or re-open project
delete .idea and .gradle folders inside the root project folder and re-import or re-open project
Afaik, deleting the generated folders is safe, it will just re-run all IDE and gradle configurations from the start.
I am really not sure about this problem, but i think it's IDE error.
you can Clean Project and then Rebuild project.
you will find it in Build tab. or you restarting Android Studio might solve your problem.
last option : you have to remove every dependency then sync after paste every dependency again and sync. You must need internet connection do this step.
I hope this will solve your error.

Android Studio: This file not part of the project, but the project builds successfully

I have a strange problem that suddenly appeared in android studio. I created a new cpp file, and included it in Android.mk. Then I synced the project. However, android studio still complains that the file is not part of the project and that I need to sync, BUT the whole project builds successfully.
Likewise, if I remove one of the other older files from Android.mk that it did not complain about, and resyncs and then tries to build the project, as expected the build fails, but android studio does NOT complain that that file is not part of the project anymore.
So somehow, suddenly the android studio editor is not able to correctly identify which files have been synced and are part of the project, but during compilation everything works as expected.
Does anyone know how to fix this annoying problem? I have tried clean project, invalidate caches/restart as well as updating android studio without luck (AS version 3.4).
I used Build > Refresh Linked C++ Projects menu and it worked.
I had a similar problem. Like yourself, I have tried everything.
Invalidate and Restart: Doesn't work
Manual deleting folders: .gradle .idea .ndkbuild etc. doesn't work
Clean, Rebuild, Link C++ Files: Doesn't work
One thing that kind of helped me was: I changed the NDK version. I compiled, then got a compilation error (didn't matter because it was the wrong version of NDK anyway), then I reverted to the original NDK. This appeared to solve the problem, however, it got back again.
My solution was to reset Android Studio to factory settings. If you are on Linux, you can start by deleting these folders:
rm -rf ~/.android
rm -rf ~/.AndroidStudio3.4
Then you download and run your Android Studio and not import anything from anywhere.
I suspect the problem was caused by one of the plugins I've installed.
It may be a good idea to backup those two folders from time to time and reload them from there if necessary.
EDIT: It seems that my problem persisted after the above solution after adding more .cpp files. After seeing that, I searched where that popup came from. It follows that "This file is not part of project..." popup is pushed from ndk-build. (Class name: NewCppSourceNotificationProvider - StaleCppProjectNotificationPanel). What I tried, and what worked so far; I used Android Studio 3.5 Canary13 with NDK version r19c (Stable version). I hope this helps you.
Problem Environment
Android Studio 3.5 RC 2
gradle-4.10-all
com.android.tools.build:gradle:3.2.1
Solution
Update to:
- gradle-5.5.1-all
- com.android.tools.build:gradle:3.4.2
Steps
From the project root run (note this has to be done first):
./gradlew wrapper --gradle-version 5.5.1 --distribution-type all
In root build.gradle file:
buildscript {
//...
dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
//...
}
}
I have similar problem, it could be the compatibility issue of gradle version and gradle plugin version, because my solution is replacing the old configuration
// build.gradle
classpath 'com.android.tools.build:gradle:3.2.1'
...
// gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
with the following new one by changing gradle version from 4.6 to 4.10.1.
// build.gradle
classpath 'com.android.tools.build:gradle:3.2.1'
...
// gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
After that, Android studio can index and track my new cpp files in project.
fix this by update my 'com.android.tools.build:gradle'
Had the same problem with Android Studio 4.0.1 and the latest gradle at this time (6.1.1?). The problem went away after I exited Android Studio and deleted .gradle and .idea folders in the project main directory, plus deleted .cxx and build directories in the affected module directory. I'm not sure which really helped, but most probably deleting .grade and/or .idea
I fixed with update 'com.android.tools.build:gradle' in the artic fox version 2020.3.1
For those struggling with this for me I made a small change to CMakeLists.txt (I altered the version required). This forced the CMake to regenerate, and all missing files were added. This is quicker than the other options listed here.
This applied to NDK 21, so YMMV!
Invalidate cache and restart and you should be good. However, make sure you checked the Clear file system cache and local history checkbox.

Android Studio “cannot resolve symbol R” but project compiles and runs

Android Studio displays in all java files in red : “cannot resolve symbol R” but the project compiles and runs.
I have tried every solution here:
Android Studio "cannot resolve symbol" but project compiles and works, but with no luck.
I tried to :
invalidate caches and restart android studio
delete .gradle and .idea folders
clean and rebuild the project
install a new version of android studio
mess up with gradle file then undo changes and sync project
One thing that I want to try, but I don't know how to achieve, is to reopen the project from scratch as I was opening it for the first time, but I don't know how to do this.
I also tried answers from here:
Android Studio says "cannot resolve symbol" but project compiles,
but again with no luck.
Android studio version = 3.2.1
dependency versions in gradle project file:
classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
classpath 'com.google.gms:google-services:4.0.0'
For some reason that i do not understand ,
this combination of build versions made the issue :
grade version = 4.10.1
classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
when i switched to these build versions :
grade version = 4.6
classpath 'com.android.tools.build:gradle:3.2.1'
The issue was solved !
File -> Close project
Open an existing Android Studio Project
Open you project
Hope it will work.
sometimes, R file is not generated because of package name on android manifest is not match with package module that you have.
Is any wrong syntax or spelling in your xml?
Check your layout or any xml file.
Android Studio seems to have a caching issue with R.java occasionally. I rarely have a problem with this, but when I do, I actually open R.java (double press shift and type "R.java") or navigate to R.java under "app/build/generated/source" directory, opening the file and checking if the relevant XML id has been created. If it has, it forces Android Studio to now recognise the id's that are not being resolved. It's quick to try, and doesn't require clean and rebuild.
From your programs menu, open android studio. instead of opening your project from the recent files, select to open a project from your computer and then locate the path to your project. When all else fails, sometimes this works.
Also, try commenting out the support libraries from your gradle implementation, sync your project, and after sync fails comment them back in and sync again. (not sure if that's what you tried already when you said you messed with the gradle file)
In my case,
I am using
- Android Studio version 3.3.1 at home
- Android Studio version 3.2.1 at office
When i pulled projects to my office computer that are firstly created at my home computer ,
Android Studio can not resolved R file but runs application with no error. Because projects gradle and Android Studio version are incompatible.
Only thing that you sholud do, change the gradle version in project level gradle file
classpath 'com.android.tools.build:gradle:3.3.1'
to
classpath 'com.android.tools.build:gradle:3.2.1'
I was facing same issue,
first thought the issue might because of some xml file or naming of drawable resources incorrectly. After analysing, this case wasn't applicable to me.
So
Updating Android studio from older version 3.2 to newer version 3.3.2 along with new build tool version from SDK manager resolved this issue
for me.
I've tried invalidate cache and Restart AS with clean rebuild all the options but didn't work for me.
It says cannot resolve symbol, but it can run.
In my case, I just reload the needed *.jar files to the libs folder.
File >> invalidate caches/restart
Rebuild Project
Sync
Run
Works for me!
It looks like the library did not load my "imports" properly the first time.
That's why the import methods cannot be seen in my main_activity.xml.
This happened to me when I was going back and forth between different SDK versions.
Apparently SDK manager copied all the source files but it didn't finish cleanly. As a result I could compile and run my project just fine but IDE didn't recognize the SDK and reported all java symbols unresolved.
None of the above and other solutions in SO didn't work for me, but just uninstalling/reinstalling the specific SDK version did a job.
If you renamed your package (inside java folder), make sure you change your Manifest package name to the same
The only thing that worked for me was,
replacing,
import package_name.R
with
import package_name.*
simply go to project settings : settings.gradel and change the rootProject.name to your current name
rootProject.name = "write the project name here"

Android studio location of caches/generated sources

I have switched to using Android Studio 3.1 RC2. I have detected that when I switch branches, I am running across generated sources from other branch. They still show up even if I do Build -> Clean Project. Or manually delete build folders in all my modules as well as top level build folder.
Only way to rectify this is delete .gradle folder in my project directory and my $HOME/.gradle , this is very time consuming.
Where does Android Studio keep its caches of generated files. I have tried Android Studio's clean "Invalidate Caches/Restart" without much help.
You can use
./gradlew cleanBuildCache
to clean up all caches under HOME/.gradle
See: https://developer.android.com/studio/build/build-cache.html

Error running android: Gradle project sync failed. Please fix your project and try again

Android Studio (1.2 RC0) keeps telling me
Error running android: Gradle project sync failed. Please fix your project and try again.
How can I find out what the problem is? Unfortunately the solutions from this SO thread did not help.
Goto File -> Invalidate caches / Restart
Shutdown Android Studio
Rename/remove .gradle folder in the user home directory
Restart Android Studio (It will download gradle metadata and data)
Gradle build succeed
Rebuild project. Done.
Solution is
Connect your computer to the internet
Click
Sync project with Gradle files
On toolbar
It will automatically sync the gradle.
Just ignore the error and try running the project anyway.
It will fail, but this time you will get a link in the error description. Clicking the link will trigger the automatic update/download of the outdated/missing component.
In my scenario, the project failed to launch two times, each time another API/SDK component being the culprit.
In my case I was just trying to launch a sample project I downloaded, having no clue what API level was being targeted or where to look for this information.
Download the latest Gradle package distribution for example (gradle-2.11-all.zip).
Extract this zip file in a folder as for example (D://Gradle)
Load your project in Android Studio and go to File->Settings->Gradle. Click on Use local Gradle distribution and point it to the folder where you unzipped the latest Gradle project.
Restart your project
It is a very common issue and the solution is very easy...
Just update the SDK in SDK manager(see full instructions below)
Open Android Studio and go to your SDK Manager(File>Settings>SDK
Manager)
Check for (-) icon in front of any package and click on it.
Click OK to start updating the SDK.
Thanks for asking that question :)
Delete "build" folder from project's root folder. Then do "invalidate cache and restart". This will restart your Android Studio and then build the app again, will solve the issue.
Go to
file->settings->build,execution,deployment->gradle.
(and set gradle home path from your directory)
If no error messages are shown: sometimes the command-line gradle shows them. Just go the the command-line, change to the app directory and type
gradle installDebug
or, if the details are missing
gradle --info installDebug
This can provide additional information how to debug the problem.
I had the same issue yesterday. After opening the "Android SDK Manager", updating all packages and restarting Android Studio, the project compiled again.
For me, the problem was the SDK Tools License had not been accepted. Clicking on the "Build" tab at the bottom uncovered the issue.
From there, I went into the AVD, uninstalled the tools and then reinstalled them, which prompted to accept the license.
All of these have not worked for me, what did is reading the very bottom of the log file located around here
C:\Users\madri\AppData\Local\Google\AndroidStudio2020.3\log\idea.log
and it said the following
So i fixed the problem by going into Tools > SDK Manager and installing the required SDK version
it looks different from one situation to another.
for me i did an installation and update a lot of things that the sdk manager said to then i had this problem.
all i did was:
from file -> Invalidate caches/Restart
(a dialog will pop-up) choose Invalidate and restart.
after the restart is done. everything was fixed but i needed to create a new emulator and delete the old one.
(android studio 1.3.1)
(one professional must edit this to be more understandable)
.
.
.
Here everything was fine, but your machines also needs restart because some system files are changed, to sync them we have to restart the system.
This could be due to Android Studio version conflict of v3 and v4 especially when you import a project in android stdio v4 which was previously built in v3.
Here are the few things you need to do:
remove the .gradle and .idea folders from the main Application folder.
import the project in the android studio.
after the sync finished, it will ask you to update the android studio supported version.
click update.
done.
I had the exact same error message
Error running android: Gradle project sync failed. Please fix your project and try again
Steps to solve:
Close Android Studio.
Remove the .gradle and .idea folders from the Application folder.
Open Android studio with your project.
After these steps android studio gradle will sync and download the required files to run the project (don't forget to disable offline mode).
I got this error going from Gradle 3.5.3 to 5.4.1 (Android Studio prompted and I clicked "Upgrade".)
Gradle proceeded to upgrade but yielded some errors such as:
ONFIGURE SUCCESSFUL in 1m 27s
ERROR: The Android Gradle plugin supports only Crashlytics Gradle plugin version 1.25.4 and higher.
The following dependencies do not satisfy the required version:
root project 'Android%20App' -> io.fabric.tools:gradle:1.25.1
Update plugins
Affected Modules: app
INFO: API 'variant.getExternalNativeBuildTasks()' is obsolete and has been replaced with 'variant.getExternalNativeBuildProviders()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
To determine what is calling variant.getExternalNativeBuildTasks(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information.
Affected Modules: app
INFO: The specified Android SDK Build Tools version (27.0.3) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.5.3.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '27.0.3'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
Install Build Tools 28.0.3, update version in build file and sync project
Affected Modules: app
The thing that fixed it was opening Settings --> Appearance & Behavior --> System Settings --> Android SDK and I then selected some recent versions of Android SDK like 8, 9, and 10. (I had only had 5.1, 6.0, and Android N Preview installed.)
Once that's installed, I had to modify my build.gradle file for my Module: app
Removed buildToolsVersion '27.0.3'
Then I clicked the "Sync Project with Gradle Files" button on the toolbar. The Sync output window offered an option to "Update Plugins" and once I clicked through that everything seemed to work. For good measure I again clicked "Sync Project with Gradle Files" followed by Build --> Clean Project, and finally I was able to run my project again.
Resolution is simple. Open the "Android SDK Manager", update all packages and then restart your Android Studio. After that you project should compile without any issues.
It took me some time to resolve this.
Updated SDK Build-Tool [Didn't help]
Updated CMake [Didn't help]
Updated Android SDK Platform-Tools [Didn't help]
Updated Android SDK Tools [Didn't help]
Updated NDK [Problem solved]
There was a reason, I didn't want to update NDK. When all else failed, I updated NDK and that did the trick.
If your got timeout, Check Gradle Scripts -> gradle.properties in Android Studio, I found that I do have set a socket proxy in Android Studio settings, but in this gradle.properties file, my proxy became to http, so I just removed this http proxy setting lines, and finally works.
I got this problem on Android studio, there in either sync or build square were this link like blue text about this issue, press that button and it download something that fix this problem
I ran into the same issue, but then did the following, and my issue was resolved:
updated Gradle
installed the latest version of Android studio (mine was out of date)
And that solved my problem.
Note: It also helped me to click on the event log, because it has more detailed info about errors. https://developer.android.com/studio/releases/gradle-plugin#updating-plugin also has great info.
I've installed Android SDK Command-Line Tools from SDK Manager and after, juste Sync again and all is worked well.
it was hard to solve but i did it
look go to
C:\Users(user name)
delete .gradle file
its you didn't see it it may be in hidden files so if you use windows 7
right click then click on properties and click on hidden to be not checked
then the file will appear then delete it
i hope this works
It could be that you are using gradle in offline mode.
To uncheck it go to File > Settings > Gradle, uncheck the Offline Work checkbox, and click Apply
Make sure you have internet connection and sync the project again.
Its because the gradle is not synced because of many reasons.
Go to project folder and remove .gradle folder and start sync. It will work
Apparently, some platforms could be missing. This is how I solved the problem:
Do you see an error message that is shown at the bottom of your Android Studio window? It could be a smaller window on the bottom right also.., which looks like:
Now, Click on that blue link, do as the dialog boxes say (if any)...
That solved the problem. Hope it helps...
Download grade files from this url and extract all in a folder:
 https://gradle.org/releases
Open file -> Setting in windows OS or Preferences in mac OS and in "Build, Execution, Deployment"
Click on "Gradle"
Check “Use local cradle distribution” and give that extracted folder path.
Run gradle.bat for Windows OS and gradle file for mac OS in that folder.
Then check “Offline work”.
"your extracted folder path"/gradle-4.10.2/bin/.gradle (for version 4.1.2 you can set your version)
Press OK and enjoy it.
I had the exact same error message
Error running android: Gradle project sync failed. Please fix your project and try again
, but if none of the above fixes your error, then I would highly suggest for you to check your syntax inside the AndroidManifest.xml file.
That fixed if for me. I don't understand why the error-message is so misleading, since it had nothing to do with the build gradle directly.
Please also see this SO-answer here, which has lead many of us in the right direction.
It was yelling at me about a line in my xml file (which was perfectly valid and never had problems the entire time I've worked on the project). I deleted the line and even after running again and cleaning and rebuilding it kept yelling at me about the completely blank line. I restarted android studio and removed the .gradle folder and it finally worked. Hopefully someone in my shoes that had all of the other answers in this thread fail them will see this answer and save a few hours.
I had this problem (clicked a prompt to "sync with gradle" on my perfectly valid and working project and it killed everything) and I researched for hours trying to figure out how to fix this, including this thread among many others. I finally found my solution, so hopefully, it'll help somebody (and hopefully they fix this terrible system)
I encountered this in a unique situation: had refactored the word "time" to "distance" for one of my variables. And when I did the refactor, accidentally had it change the name of one of my gradle dependencies / implementations.
This is a rare cause of the error, but a possibility. So make sure all your dependencies are properly implemented in build.gradle
Delete .gradle folder(root directory) and build folder(project directory) and then invalidate caches/restart android studio, run your project again hopefully it will work. It might take some time for the background tasks. In some countries, you may need to turn on the VPN to download.

Resources