14

I have a multitude of problems stemming from one central issue: Android Studio isn't recognizing imports.

For example, in my class "ActivityAbout" I have the following import statements:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

However, in the IDE (using the first import statement as an example), the import android.content. is appearing grayed out, and when I hover over it, it says "unused import statement" the Intent part is appearing red, and when hovering over that, it says "cannot resolve symbol Intent." This occurs for each of my import statements, with the first part being grayed out, and the last part being in red.

Now, this provides a multitude of issues in my code, as anything relating to those import statements appears red and when hovering over it, I get the message "cannot resolve symbol ____"

Here's the kicker- my app still compiles and runs upon opening it on my phone.

I suppose my main issue is that it is incredibly annoying, and auto imports doesn't work either. (I have checked off 'show import popup' in the settings, which is the answer I found for other people having issues with auto import not working). My other concern is that this would make my app ineligible for release on the play store. I'm still looking into the publishing process, so I don't know if that is actually an issue or not.

My question (if no one has an answer for how to fix this) is: is this a problem with the IDE itself or the code?

EDIT: added the other import statements

Mairead
  • 275
  • 1
  • 2
  • 10
  • Which import gives you "cannot resolve symbol"? – AbhayBohra Aug 31 '17 at 13:04
  • @AbhayBohra all of them, in every class – Mairead Aug 31 '17 at 13:04
  • Can you post those imports here – AbhayBohra Aug 31 '17 at 13:07
  • @Mairead : Are you saying that despite of the imports not resolving, you are still able to run this app from Studio? – Sanket Mendon Aug 31 '17 at 13:07
  • @SanketMendon yes – Mairead Aug 31 '17 at 13:08
  • I had faced similar issues where my imports were not getting resolved due to my compileSdkVersion and buildToolsVersion. It seems the buildToolsVersion I was using wasn't installed. So what you can do is create a new android application like Hello World and see the default compileSdkVersion and buildToolsVersion being used and then use the very same versions in your project. – Sanket Mendon Aug 31 '17 at 13:14
  • @SanketMendon new projects I create have the same issue- the default import statements are grayed out – Mairead Aug 31 '17 at 13:15
  • If this doesn't help then check your gradle dependency version for appcompat and design. To get the correct dependency version, go to Project Structure->Modules->app. Here you will see a Dependencies tab. On clicking the + button and choosing Library dependency you will get a search bar. Just write the name of the dependency you need ex: appcompat. It will give you a dependency with fully qualified name like : com.android.support:appcompat-v7:25.1.1 . This is the version you should use. – Sanket Mendon Aug 31 '17 at 13:18
  • 1
    Possible duplicate of [Android Studio suddenly cannot resolve symbols](https://stackoverflow.com/questions/21100688/android-studio-suddenly-cannot-resolve-symbols) – Myszsoda Aug 31 '17 at 15:08

2 Answers2

21

I just had this issue. I resolved it by clicking File > Invalidate Caches / Restart... > Invalidate and Restart. When Android Studio restarted and indexed all my code again, the issue was gone.

I have no idea what caused it...

camerondm9
  • 1,005
  • 15
  • 22
5

I had a similar issue with my project. I resolved it by going to File > Sync with File System and then closing the project and importing it again

You may need to resync with the gradle again but hopefully it works for you

King Keet
  • 51
  • 1
  • 4