Reputation: 25673
I've set up my project in Android Studio to use the Square Wire library, as per this question.
Build
-> Compile Project
works fine.
2:03:10 PM Compilation completed successfully in 31 sec
However, if I open one of my generated Protocol Buffer files, then the Square package is not being recognized.
Another symptom is that methods inherited from the Square Message
class are showing as "Cannot resolve method" compile errors in the editor, even though they build and run fine.
Do I need to do something more than just adding the dependency in build.gradle
for my module in my project?
Upvotes: 193
Views: 87902
Reputation: 366
Necroing this
Encountered this problem with Android Studio Flamingo. Tried to resolve for several days. Deleting .idea
, .gradle
, etcetera. Even went as far as to uninstall and hunt down every last cache and configuration file and delete them. Synced gradle files multiple times, changed gradle versions, reinstalled the JDK. Many combinations of these--No solution, no clear information on the problem. Additionally, view binding classes were not generating.
After hours of troubleshooting, I used File->Repair IDE and followed the prompts. This is on a fresh install, mind you. The solutions that the IDE executed resolved all of my problems permanently.
If you're finding this thread from Google and nothing else works--Do this. Arguably, this is the first thing you should try. Certainly as opposed to something as extreme as deleting your .idea
folder.
Upvotes: 3
Reputation: 211
For me, I was having a very old project with gradle plugin version 3.+. And I was using Android Studio Chipmunk. The solution was by upgrading the project gradle plugin version = Android Gradle Plugin version 7.2.2(at the time of this comment was written).
Upvotes: 0
Reputation: 1
I know it's an old question but maybe this will help someone as none of the answers above worked for me, except for this answer from Victor Choy that inspired the fix for my situation so I have to thank Victor for the answer.
after updating to android studio chipmunk using AGP version 7.2.0 and wrapper version 7.3.3 I encountered an issue with one of my module libraries that was basically a jar file. my editor showed errors just like the errors in the question but my app compiled fine and ran without an issue. so the problem was I had the library dependency in my build.gradle
file as follows:
dependencies{
... //other dependencies
api project(':foo-library')
...
}
but after I saw Victor's answer I changed it to this:
api fileTree(include:['foo-library.jar'], dir: '../foo-library')
and it resolved the errors that was caused by usage of this library in my code. I don't know why it happened because before updating android studio everything was ok.
you can replace api
with implementation
or any other dependency prefix that suits your needs.
Upvotes: 0
Reputation: 25491
Android Studio 4.2 in 2021 appears to still sometimes have this issue.
As a quick first attempt fix before you try the more involved ones here: exit and then restart Android Studio (i.e. the traditional, turn it off an on...) . This fixed it for me.
If that does not work, then its time to work through the more involved answers here...
Upvotes: 1
Reputation: 9
The following worked for me.
In the build.gradle file, add the below line:
implementation "androidx.activity:activity-ktx:1.2.2"
Upvotes: -1
Reputation: 876
Encountered the same issue where there's a squiggly lines under one of my modules. Turns out its coming from one of the java files from the generated folder. What I did was to exit Android Studio, delete .idea
and .gradle
directories, and NEVER open that generated folders again otherwise the red lines will come back.
Upvotes: 2
Reputation: 1491
For me
deleting .gradle and .idea folder
of project solved the problem.
Upvotes: 20
Reputation: 81
if File -> Invalidate caches/Restart -> Invalidate and Restart is Not Working then follow few step.
1.) Close Your Android Studio
2.) Then Delete Manual on C:\Users\Balbir\ .AndroidStudio4.0\system\caches Folder.
3.) Start Android Studio
It is Work For Me.
Upvotes: 7
Reputation: 13
I have only make a Clean Project and Rebuild Project before restarting Android Studio, it solves my problem. Maybe Android Studio actually do not update references in the interface
Upvotes: 0
Reputation: 390
I am fixed by below method
Method 1
File > Invalidate Cache/Restart > Invalidate and Restart
Method 2
1. Close current project 2. Go to your project path C://User/Android studio project/your project 3. Delete two files .gradle and .idea
1. Close Android studio 2. Go to the file C://Users/your username/ 3. Delete files .gradle , .cache ,.android, .AndroidStudio3.5
Upvotes: 8
Reputation: 71
None of the solutions worked for me..I resorted to uninstall java from my computer, running android studio to make sure it detected the uninstall and then installing it again.
If you choose to go this way try changing the jdk path and setting the new path in the project structure. If that doesn't work, go ahead and uninstall java.
Upvotes: 0
Reputation: 69
I got the same issue cleared using the following steps:
update the library version in gradle to any random number, eg: lib_1.0.0 -> lib_1.0.0.0
gradle sync
sync shows error
revert the version change lib_1.0.0.0 -> lib_1.0.0
gradle sync
Open the java file with imports to see the error cleared.
Upvotes: 0
Reputation: 395
All the answers above didn't work for me. My problem was that i had 3 modules, with 2 of them with the same package name.
It was ok for android studio <= 3.2.1.
For android studio > 3.2.1, ressouce was red.
I solved it like this mention here (i just made differents packages name for each modules ) : Any android Studio version > 3.2.1 show red resource but still compile
Upvotes: 0
Reputation: 4338
It seems that everyone uses a different approach for getting around these issues. I'll suggest another one that I feel is a bit less invasive than the others. When I introduce changes from an aar library that I include, I will open up the "settings.gradle" file for the project.
Then cut out the main project module from the list of projects:
include ':mainproj',
and then sync your project. Then paste it back in and re-sync your project once more. I believe that this is the same as some of the manual deletion steps, but this lets you do so from the IDE a bit quicker and less painfully.
Upvotes: 2
Reputation: 4256
For me, I refer a jar in a module library. It cannot resolve the symbol in the jar. So I find the solution for my situation. Use the provided
way.
provided fileTree(include: ['xxx.jar'], dir: '../moduleX/libs')
Upvotes: 0
Reputation: 131
Deleting the .idea/caches
and .idea/libraries
should sort out all the errors.
Upvotes: 2
Reputation: 451
Just do this on your Android studio:
File -> Invalidate caches/Restart -> Invalidate and Restart
This worked for me. No need to clear ./gradle or ./idea folder
Upvotes: 13
Reputation: 59004
build.gradle
Error Gone!
Upvotes: 2
Reputation: 5375
I was still having the same problem with Android Studio v3.2.1 constantly, and sorted out the problem just removing these two sub-folders of the .idea
folder (no need to delete the whole .idea
folder) while a gradle sync didn't work:
./idea/caches
./idea/libraries
Upvotes: 19
Reputation: 100
Below steps when the Project is succesfully compiled but with unresolved classes (All classes usage will be in red color in all files)
Step 1. Delete .gradle file
Step 2. Delete .idea file
Step 3. Open new Android studio Window with some other android project on your system and close your Project with error
Step 3 . Now Open/Import the Project once again from the studio toolbar Android Studio ->File -> Open
Note: Now when you import the project the new .gradle and new .idea folders will be autogenerated with resolved dependencies by the compiler.
Upvotes: 5
Reputation: 85
I tried most of the fixes above and what actually worked was deleting all the .AndroidStudio config folders and then restarting Android Studio.
Upvotes: 2
Reputation: 231
My hacky fix for this -
build.gradle
filedependencies{}
build.gradle
fileTried a lot of solutions and this was the only fix for me.
Upvotes: 11
Reputation: 1881
Deleting libraries folder in .idea solved the issue for me. It is located in project's root/.idea/libraries.
I'm using Android Studio 3.1.4
Upvotes: 7
Reputation: 1862
The following steps helps
project/.idea
~/.gradle
folder.Open the project again. Everything should be solved.
Upvotes: 158
Reputation: 1561
In my case, for some reason, Android Plugin Version wasn't set. I did try clearing cache and removing .gradle and .idea as well, but it didn't work out and after finally wandering for hours, I found this.
Upvotes: 2
Reputation: 6621
Simple Solution
Go through the link and it worked for me.
File
option from menu on left top side of android studio.Invalidate Cache/Restart
It will open an dialog.Invalidate and Restart
It resolved my problem
Go to File > Invalidate Cache/Restart > Invalidate and Restart
Upvotes: 106
Reputation: 2465
I had this problem after upgrading to Android Studio 3.1. I solved the issue by upgrading all the components I use. So I changed the following entries in my build.gradle (app):
Upgrade SDK (from "compileSdkVersion 25" to "compileSdkVersion 26" or maybe higher when you read this note):
//compileSdkVersion 25
compileSdkVersion 26
Comment: This change forced program to download/install new sdk during next sync.
Upgrade appcompat-v7:
//implementation 'com.android.support:appcompat-v7:25.4.0'
implementation 'com.android.support:appcompat-v7:26.1.0'
Upgrade firebase:
//implementation 'com.google.firebase:firebase-ads:11.4.2'
implementation 'com.google.firebase:firebase-core:12.0.1'
implementation 'com.google.firebase:firebase-ads:12.0.1'
In PROJECT build.gradle, upgrade google-services:
//classpath 'com.google.gms:google-services:3.1.0'
classpath 'com.google.gms:google-services:3.2.0'
Upgrade gradle (from 4.4 to 4.6). From gradle-wrapper.properties: distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip
After these changes, clean project, invalidate cache/restart, remove .idea/libraries content and resync (not sure about the order but ALL these activities solved the issue for me).
Upvotes: 7
Reputation: 789
1) Click on the gradle icon (right side on the android studio)
2) Click on the refresh button and wait for the issue to be resolved :D
Upvotes: 46
Reputation: 1176
If all above answers don't work for you. Just try update your dependencies to latest version. It worked for me.
Upvotes: 2