189

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.

Android Studio inline compile errors

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.

Android Studio single line compile error

Do I need to do something more than just adding the dependency in build.gradle for my module in my project?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dan J
  • 25,433
  • 17
  • 100
  • 173
  • I had a similar issue using my libraries (not external ones), see how I manage to fix this here: https://stackoverflow.com/a/50569818/373033 – nah0y May 29 '18 at 07:23
  • Try this, it's worked for me after tried everything https://stackoverflow.com/a/64346212/8627144 – Livin Nov 17 '20 at 11:20

33 Answers33

157

The following steps helps

  1. Close your project, and in your project folder delete project/.idea
  2. Delete ~/.gradle folder.

Open the project again. Everything should be solved.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
Bioz Nguyen
  • 1,842
  • 1
  • 9
  • 6
  • 1
    Spending hours on this problem. Thanks. My mistake was that I just deleted .gradle and not .idea – M at Apr 24 '18 at 09:59
  • 2
    This worked for me deleting .gradle and .idea and then doing invalidate and restart solved my problem. and then sync with gradle. – Pankaj Apr 25 '18 at 06:20
  • I have moved the `.gradle` folder from `C:\Users\Hasnain\.gradle` to another directory. Afterward, the android studio shows error on each and each class. Then I follow the above-said steps, then the issue is resolved. – hasnain_ahmad May 10 '18 at 04:55
  • 51
    Deleting everything in `.idea` directory is a bad idea, as it might reset all project settings. Just removing `.idea/libraries` and `.idea/caches` finally solved the problem for me after trying all other solutions. – fdermishin May 17 '18 at 15:56
  • worked very well in A.S. 3.1.3. I didn't even need to invalidate caches. Let's hope A.S. will build faster now ! – Someone Somewhere Jun 14 '18 at 14:35
  • you can delete through android studio, then restart it from `File > Invalidate Caches/ Restart` and click in the `Just restart`, no need to 'close' it – Gabriel Rohden Sep 05 '18 at 15:07
  • A pain in the neck solved. In my case, invalidate and restart was required after the progress. – March3April4 Oct 08 '18 at 04:36
  • In my case, deleting .idea was enough. Since the .gradle directory includes all cache and gradle files,deletion may cause wasting time. – ilkayaktas Nov 01 '18 at 08:22
  • Completing wiping out your project data is not a reasonable solution. – Jeffrey Blattman May 01 '19 at 00:27
  • Thanks! Can't believe one still must go to the file system for this in 2020. I'm on Android Studio 3.6.3 and got this annoyance with Kotlin code after androidx migration. – kakyo May 29 '20 at 02:04
  • 1
    I followed this and ended up deleting all shelve changes stored in my Android Studio which can not be undo too. Beware if you are doing this and you know what all will get deleted. I suggest to follow @fdermishin comment – AndroDev Dec 31 '20 at 07:05
  • Is closing the project really required ? Or can I delete .idea/libraries and .idea/caches directly in Android Studio ? – toto_tata Feb 02 '22 at 10:39
  • thanks for sharing! Saved a lot time phew!! – Iram Bukhari Mar 16 '23 at 20:13
  • Removing `.idea/gradle.xml` and `~/.gradle` only worked for me. – Florian Richoux Jul 24 '23 at 01:35
124

Thanks to free3dom for getting me on the right track here.

Diagnosis

The Sync Project with Gradle Files option in Android Studio seems to keep the Project Structure libraries up to date.

gradle sync

However, in my case there were some errors:

errors found

Expanding the (not very obvious) link showed the detail. My wire-runtime library was showing in an incorrect location:

libraries

This seemed to be a hangover from my original configuration where I had imported the JAR into a libs folder.

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

I subsequently changed it to be imported directly from Maven Central (as per here), and it seems that the old setting was never overwritten with the new one.

The Fix

The fix is to delete the library dependency from the Project Structure screen and then Sync Project with Gradle Files again.

More Detail

You can also see what path is being used to the resource by opening the .idea/libraries/wire_runtime_1_2_0.xml directly.

My broken one was:

  <component name="libraryTable">
  <library name="wire-runtime-1.2.0">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/MY_MODULE/libs/wire-runtime-1.2.0.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />

After deleting and re-adding the library it changed the root url to:

      <root url="jar://$USER_HOME$/.gradle/caches/artifacts-26/filestore/com.squareup.wire/wire-runtime/1.2.0/jar/44e7acbd5eb6f29698181dc3d67a60acf7efee80/wire-runtime-1.2.0.jar!/" />

Update

Android Studio v0.4.4 apparently fixes other bugs which have the same symptoms, so if you have an older version you may wish to upgrade.

Community
  • 1
  • 1
Dan J
  • 25,433
  • 17
  • 100
  • 173
  • 3
    Thank you Dan J. Before I try your solution, I tried this easier one (according to me) and it worked for me: Open the Gradle panel on the right side of the editor (above "Maven Projects" and "Commander") and clicking the refresh button. That refreshed the module settings. Credits to @Matteo Danieli (see: http://stackoverflow.com/a/17043001) – almighty972 Jan 23 '14 at 11:31
  • 1
    needed to delete local jar dependencies, that solved it for me, thanks for the inspiration – jhegedus Aug 05 '16 at 09:04
  • I've just clicked on "Sync project with Gradle files" and everything is fine now! Thank you very much! You've saved my day haha – Jorge Gabriel Siqueira Jun 29 '17 at 14:31
  • Didn't work for me. But deleting .gradle and .idea did work – Nabzi Sep 04 '18 at 08:45
  • Nowadays same button is shown as an elephant icon with a down arrow. – t.m. Nov 18 '19 at 12:13
  • Thank you <3 !!! – Yechiel babani Nov 08 '21 at 21:35
106

Simple Solution

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 Android Studio and restart it. Also, it will start re-indexing the project.

It resolved my problem

Go to File > Invalidate Cache/Restart > Invalidate and Restart

Source here for youtube

Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
  • This worked for me; Android Studio was showing errors everywhere after I tried to do some programming at a location with poor wifi reception. (!!!) I then made sure I had a good wifi connection and rebuilt, however, the errors remained. Even a clean/build didn't solve it. Invalidating caches fixed it. – Someone Somewhere Dec 25 '16 at 14:13
  • Using Android Studio 2.1.3 on Fedora. This solution worked for me. Added the libgdx tools and the import had an error but it compiled just fine. Closing and reopening didn't work but doing the invalidate and restart worked fine. – JoeMoe1984 Jul 07 '17 at 06:21
  • 1
    Worked in AS 3.1.4! I think this of all suggestions are the right way to handle it. It is by menu command of the Android Studio. That is definitely much better than doing the reset manually in the file environment. – Jan Bergström Sep 03 '18 at 03:59
  • Worked for me on 3.5.1 – Neela Nov 15 '19 at 15:03
  • Now in 2022 - still a good fix for weird lint-like issues. For me (and anyone else Googling the problem) Android Studio could find Kotlin classes but Java classes could not be found. The whole project still built successfully though. – Wex Sep 15 '22 at 17:16
  • Working also with `Android Studio Flamingo | 2022.2.1 Patch 2` in August of 2023. – horvoje Aug 01 '23 at 14:25
46

1) Click on the gradle icon (right side on the android studio)

enter image description here

2) Click on the refresh button and wait for the issue to be resolved :D

enter image description here

DroidDev
  • 789
  • 9
  • 19
20

For me

deleting .gradle and .idea folder

of project solved the problem.

kvadityaaz
  • 1,441
  • 1
  • 16
  • 23
19

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
GoRoS
  • 5,183
  • 2
  • 43
  • 66
  • In most cases it solves my problems. In some few others, it might be necessary to “Invalidate cache and restart”, or also change the Gradle version. – GoRoS Dec 19 '18 at 07:00
  • 1
    Last version 2021.3.1 artic fox doesn't exists libraries – Freddy Daniel Jul 31 '21 at 00:51
13

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

11

My hacky fix for this -

  1. Open your build.gradle file
  2. Cut out everything inside dependencies{}
  3. Paste in notepad for super safe temporary keeping
  4. Choose 'Sync Now' in the build.gradle file
  5. Wait for Sync to fail
  6. Paste your dependencies back in
  7. 'Sync Now' again

Tried a lot of solutions and this was the only fix for me.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
jnutttzzz
  • 231
  • 3
  • 6
8

I am fixed by below method

Method 1

File > Invalidate Cache/Restart > Invalidate and Restart

Method 2

  • If you are facing this error only for particular project, Then do this
   1. Close current project
   2. Go to your project path C://User/Android studio project/your project
   3. Delete two files .gradle and .idea
  • If you are facing this error for all projects, Then do this
1. Close Android studio
2. Go to the file C://Users/your username/
3. Delete files .gradle , .cache ,.android, .AndroidStudio3.5
7

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):

  1. 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.

  2. Upgrade appcompat-v7:

    //implementation 'com.android.support:appcompat-v7:25.4.0'

    implementation 'com.android.support:appcompat-v7:26.1.0'

  3. 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'

  4. 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'

  5. 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).

Pablo Alfonso
  • 2,317
  • 1
  • 18
  • 24
7

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

Kamen Stoykov
  • 1,715
  • 3
  • 17
  • 31
7

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.

Balbir Singh
  • 81
  • 1
  • 2
5

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.

Sumith.Pattar
  • 100
  • 1
  • 8
2

If all above answers don't work for you. Just try update your dependencies to latest version. It worked for me.

Cuong Nguyen
  • 1,166
  • 1
  • 11
  • 20
2

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.

File > Project Structure

Jainam Jhaveri
  • 1,479
  • 1
  • 18
  • 31
2

I tried most of the fixes above and what actually worked was deleting all the .AndroidStudio config folders and then restarting Android Studio.

  • Location: C:\Users\your.username
Diego Feder
  • 79
  • 1
  • 4
2

I found easy way

  • Open app level build.gradle
  • Remove dependency which is showing error & Sync.
  • Again add dependency & Sync.

Error Gone!

Before

Before

After

After

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
2

Deleting the .idea/caches and .idea/libraries should sort out all the errors.

2

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.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
2

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.

Jim Ovejera
  • 739
  • 6
  • 10
1

These solutions didn't help me with the same problem in Android Studio 0.5.8.

I described my answer to the same question (I guess): https://stackoverflow.com/a/23891829/534698 I just deleted ~/.AndroidStudioPreview directory (in Ubuntu). I missed all my Android Studio settings but it works.

Community
  • 1
  • 1
DmitryDzz
  • 416
  • 4
  • 7
1

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...

Mick
  • 24,231
  • 1
  • 54
  • 120
0

I had a similar issue before, invalidating cache and restarting didn't resolve it. I just did a Gradle Sync and everything worked fine.

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
0

Solved the issue by cleaning project Build->Clean Project

Ganesh P
  • 196
  • 1
  • 14
0

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')
Victor Choy
  • 4,006
  • 28
  • 35
0

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

Z3nk
  • 365
  • 4
  • 17
0

I got the same issue cleared using the following steps:

  1. update the library version in gradle to any random number, eg: lib_1.0.0 -> lib_1.0.0.0

  2. gradle sync

  3. sync shows error

  4. revert the version change lib_1.0.0.0 -> lib_1.0.0

  5. gradle sync

Open the java file with imports to see the error cleared.

Deepak PK
  • 59
  • 6
0

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.

Don Louis
  • 61
  • 1
  • 7
0

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

0

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.

0

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).

Cousin Roy
  • 191
  • 1
  • 6
0

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.

hcl2000
  • 16
  • 4
-1

The following worked for me.

In the build.gradle file, add the below line:

implementation "androidx.activity:activity-ktx:1.2.2"
Cristik
  • 30,989
  • 25
  • 91
  • 127