223

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
  • 813
  • 2
  • 9
  • 10
Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135
  • 79
    Every single Android Studio update breaks projects, I'm sick of it already. Why can't they hold on and release a usable version. – Konrad Morawski Feb 16 '14 at 22:54
  • 3
    Before cleaning or removing any file, read this answer first. It may solve your problem. http://stackoverflow.com/a/19223269/513413 – Hesam May 18 '14 at 10:08
  • 16
    "Invalidate Caches / Restart" worked for me. – Eagle_Eye Oct 04 '15 at 17:42
  • 1
    Had the same problem and fixed with the answer on this SO post http://stackoverflow.com/a/30828772/1550233 – johnw182 Oct 18 '15 at 14:22
  • accept this answer so users won't delete anything manually https://stackoverflow.com/a/22901534/7767664 – user924 Mar 07 '18 at 22:46
  • @user924 you can see in my question I tried that and it failed me. So my question could be reworded as "what to do when invalidate+restart does _not_ work" – Daniel Wilson Mar 07 '18 at 22:53
  • @DanielWilson it works, try again when that will happen ) – user924 Mar 07 '18 at 22:56
  • None of the answers helped me. I asked someone with a working android studio to pass me their android sdk folder, I copied it and replaced my sdk folder for theirs. That did it for me – Javier Heisecke Jan 27 '21 at 12:32

28 Answers28

315

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
  • 9,349
  • 16
  • 86
  • 148
Victor Häggqvist
  • 4,484
  • 3
  • 27
  • 35
  • 21
    For me, I had a power failure when android studios 0.8.2 was loading and it will not recognise any of my java imports and everything was just red. This worked for me, thanks! – Simon Nov 08 '14 at 17:50
  • 2
    I am using android studio 1.0.2, when i opened my project, i was full of red errors. i solved it by android studio-->file-->invalidate chaches/restart. when my project restarted it worked..no red errors – SoftEye Jan 28 '15 at 06:52
  • For some reason the line compile files('libs/core.jar') was missing in my build.gradle in the root of the project. After adding it to the dependencies section and following your hint it worked. (Android Studio 1.5.1) – earizon Dec 23 '15 at 18:36
  • This didn't work for me in 2.2 Preview 1. Any suggestion? – osrl May 27 '16 at 12:47
  • @osrl: Try reverting back to some stable update of Android studio.. These preview or Canary updates doesn't contains all the feature available.. – AnkitRox Jun 13 '16 at 06:46
  • I am on 2.1.2 and this solved my situation. My computer froze and I had to manually power it of while A-Studio was running. This solution worked perfectly. – Faiyet Jul 01 '16 at 16:42
  • Nope, doesn't work on Android Studio 2.2. Incapable of resolving basic symbols. – esilver Jul 07 '16 at 17:58
  • I just upgraded Android Studio to 3.1. I see red lines everywhere (yet the program compiles and run successfully). However, invalidating/restarting cache does not remove my red lines. – Pablo Alfonso Mar 31 '18 at 18:08
  • I just opened up my unchanged project that was working fine the other day and also got these errors. This answer fixed it for me and should be the accepted one. I'm on Android Studio 3.2.1. – Marty Apr 14 '19 at 13:55
  • After invalidate restart problem was still there. I had to close the project and open it again by go to destination folder not from the ''open recent'' option in the file menu (If the opened project after launch android studio is not the one which has the problem). Hope this will help for someone.This answer is the correct answer. – silent_27 Apr 20 '19 at 16:06
240

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.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Wow that answer was beautiful thanks so damn much it seems to be working now \O/ :D – Daniel Wilson Jan 13 '14 at 20:46
  • One more comment I find I am having to do this every time I am changing machines now which is interesting - not a big deal but an annoyance all the same, no idea what the actual issue could be – Daniel Wilson Jan 15 '14 at 12:57
  • 3
    This worked for me. I was going nuts with this, a full day of trying to figure it out! Thanks for the answer. – Patrick Kafka Jan 18 '14 at 23:51
  • 25
    For me it was enough to delete the libraries folder in .idea and the press sync project with gradle. – Gabriel Ittner Jan 22 '14 at 20:36
  • The 0.4.6 release fixed this for me (though I never got around to trying the above steps as it was building fine from Gradle). – mm2001 Feb 28 '14 at 17:02
  • Worked for me, Android Studio 0.8.1, just have to re-enable VCS. – Peter Arandorenko Jul 16 '14 at 15:22
  • 2
    I have the same problem with 0.8.9 and tried all solutions listed here with no luck :( – Rémy DAVID Oct 07 '14 at 12:20
  • AS 1.0.1 and had similar problem (after extending a JAR classpath to a submodule). This solved my problem. The answer is not 100% satisfactory in that this is a recurring problem and it does not explain why it occurs - but it worked! – Laurent Jan 22 '15 at 07:26
  • 3
    Didn't work for me in AndroidStudio 1.2 RC. This is a nightmare. – RED_ Apr 22 '15 at 13:42
  • I just restarted Android Studio And it back normally – Basheer AL-MOMANI Jan 16 '16 at 20:50
  • Still works at AS 1.5.1, saved tons of time for me, thanks. – Unu Mar 17 '16 at 19:21
  • 1
    AS 2.2 Preview 3: removed *.iml file from project explorer and restarted AS. This solution worked for me – lingareddyk Jun 16 '16 at 04:55
  • Worked for me after installing latest version – Himanshu Mori Jul 26 '16 at 13:12
  • 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 > **Go to File -> Invalidate Cache/ Restart -> Invalidate and Restart** Source https://www.youtube.com/watch?v=FX_gCTpqhwM – Manmohan Soni Aug 16 '16 at 12:32
  • Deleting .idea and .iml or deleting libraries inside .idea did not work for me. Also invalidate cache/Restart did not work for me – abggcv Apr 08 '17 at 12:01
  • 7
    This is still happening in 2018 and your answer was helpful. I wish this was fixed though... Happened in AS 3.1.1. – Demogorii Apr 23 '18 at 17:32
  • 1
    You are a life saver!! I was stuck on this for a day and tried everything from updating libraries to updating studio and cache clean restart but nothing helps. But your this solution still works after 4 years. – Vivek Mishra Apr 29 '18 at 07:38
  • AS 3.1.3 "Invalidate and Restart" did not help, but removing `.idea` folder and `.iml` files did – Krzysiek Jun 28 '18 at 09:06
  • "Invalidate Caches/Restart" and "clean project" did not help, remove .idea folder worked. – wrkwrk Jul 16 '18 at 06:16
  • In the first step, in place of closing the entire IDE I just closed the project instead. Then, kept all the other steps same and it still worked. I did so just to save some time. I'm on v3.1.3 on Android Studio. – RBT Aug 06 '18 at 03:24
  • Same here Android studio 3.1.4. This can drive you mad - Literally – blackpanther Sep 20 '18 at 10:59
  • Hit the **Sync Project With Gradle Files** button if you encounter any problem after this. – Fatih Santalu Sep 21 '18 at 13:07
67

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
  • 3,747
  • 4
  • 27
  • 53
Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
16

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.

Mathieu de Brito
  • 2,536
  • 2
  • 26
  • 30
10

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.

Post Impatica
  • 14,999
  • 9
  • 67
  • 78
  • Worked for me! Thanks! Using canary build 1.0RC 1. Using `minifyEnabled true` or `minifyEnabled false` and not defining `proguardFiles`. – Kalem Nov 25 '14 at 22:18
  • Also don't forget that AndroidLibrary projects don't run proguard http://stackoverflow.com/a/10992604/665823 . – Kalem Nov 25 '14 at 22:46
5

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

Aenon
  • 346
  • 1
  • 3
  • 7
3

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.

Amir Uval
  • 14,425
  • 4
  • 50
  • 74
2

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.

David T.
  • 22,301
  • 23
  • 71
  • 123
2

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.

CKP78
  • 630
  • 8
  • 19
1

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.

Kevin Grant
  • 2,311
  • 23
  • 17
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!

DmitryDzz
  • 416
  • 4
  • 7
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

Panos
  • 11
  • 1
1

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

I´m using Android Studio 0.8.6.

Manu
  • 4,410
  • 6
  • 43
  • 77
1

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]\src\main\java\[yourpackage]\[yourclass.java]

Rémy DAVID
  • 4,343
  • 6
  • 25
  • 27
1

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

JDOaktown
  • 4,262
  • 7
  • 37
  • 52
1

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
  • 1
  • 1
whitebrow
  • 2,015
  • 21
  • 24
1

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.

Blaze Gawlik
  • 337
  • 3
  • 11
0

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

Gagandeep Singh
  • 17,273
  • 1
  • 17
  • 18
0

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

varghesekutty
  • 997
  • 2
  • 11
  • 23
0

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.

Adrien Cadet
  • 1,311
  • 2
  • 15
  • 21
0

try to change your build.gradle with these value:

android { compileSdkVersion 18 buildToolsVersion '21.0.1'

defaultConfig {
    minSdkVersion 18
    targetSdkVersion 18
}
Fakher
  • 2,098
  • 3
  • 29
  • 45
0

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

surlac
  • 2,961
  • 2
  • 22
  • 31
0

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.

Eduardo Cobuci
  • 5,621
  • 4
  • 25
  • 27
0

Another very subtle cause:

Multi-flavor library should be compiled in specific way than a normal single-flavored. Otherwise it silently produces cannot resolve symbols error.

Multi flavor app based on multi flavor library in Android Gradle

Community
  • 1
  • 1
WindRider
  • 11,958
  • 6
  • 50
  • 57
0

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.

0

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.

hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
-1

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

import android.content.SharedPreferences.Editor;

and symbol is cool now.

fullmoon
  • 8,030
  • 5
  • 43
  • 58
-1

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
  • 1
  • 1
Jim C
  • 1,785
  • 1
  • 13
  • 13