Cannot resolve symbol 'AppCompatActivity'

336,514

Solution 1

A little addition to other answers here, for anyone having the same error while using the right lib version and the right class.

When I upgraded to

appcompat-v7:22.1.0

In which ActionBarActivity is deprecated and empty and AppCompatActivty is the way to go, due to some glitch in Android Studio, It didn't quite pick up on version change.

i.e. Even though Gradle ran without errors, the IDE itself kept saying Cannot resolve symbol 'AppCompatActivity' (and it also wasn't available through the Ctrl+N search)

I looked into the .idea/libraries folder and noticed there's no appropriate metafile for the new version of the lib.

So, using the old-reliable File->Invalidate Caches/Restart did the trick. Always try this when you feel something is magically wrong with Android Studio. And then Disable offline mode and sync.

Solution 2

If the soft methods via gradle file / "Invalidate caches" and the other IDE tools do not work, use the hard way:

  1. Exit Android Studio
  2. Navigate in your project to the .idea folder
  3. Just Rename the libraries folder
  4. Restart Android Studio. It should now recreate the libraries folder and work again.

This worked for me on

Android Studio 3.1.2
Build #AI-173.4720617, built on April 13, 2018
JRE: 1.8.0_152-release-1024-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.13.0-38-generic

Shahbaz Ali confirmed, it works also on

Android Studio 3.1.3
Build #AI-173.4819257, built on June 4, 2018
JRE: 1.8.0_152-release-1024-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.13.0-38-generic

moujib confirmed, it works on Android Studio 3.2.1

Solution 3

Today (22 April) you can use the new AppCompatActivity released with the support libraries v 22.1.0.

Just change your dependency :

compile "com.android.support:appcompat-v7:22.1.0"

Here you can find more info about this new class.

Solution 4

It is too simple.here are the steps you need to follow

1-Exit Android studio.

2-Go to your project directory.

3-Something like this in my case( F>Android->YourProjectName->.idea).

4)-delete libraries folder present in .idea folder.

5)-restart your android studio. and your issue is resolved.

Solution 5

I got it fixed by Going to build.gradle file and in dependencies the appcompat one, something like compile 'com.android.support:appcompat-v7:XX.X.X'

Changed it to compile 'com.android.support:appcompat-v7:XX.X.+'

Then click on Sync. All the red squiggly lines should go if everything else in your code is correct.

Share:
336,514

Related videos on Youtube

Lingviston
Author by

Lingviston

Updated on July 08, 2022

Comments

  • Lingviston
    Lingviston almost 2 years

    I've just tried to use Android Studio. I've created blank project and tried to create Activity which extends AppCompatActivity. Unfortunalty Android Studio "says" that it

    Cannot resolve symbol 'AppCompatActivity'

    I have compile "com.android.support:appcompat-v7:22.0.+" in dependency list of my "app" module and rebuilt project several times. However I can only use ActionBarActivity. What am I doing wrong?

    • ghost talker
      ghost talker about 9 years
      are you using right build version? is your sdk is up to date?
    • damson
      damson about 9 years
      @Lingviston you're not using the right version. You should use com.android.support:appcompat-v7:22.1.0
    • Lingviston
      Lingviston about 9 years
      Well there wasn't such version that time.
    • JY2k
      JY2k almost 9 years
      But now there is and it solves the problem :)
  • 3c71
    3c71 almost 9 years
    Doesn't work for me either, each time I upgrade the appcompat, I get into lots of problems.
  • Chris Klingler
    Chris Klingler about 8 years
    For me this error popped up when updating to Android Studio 2.0 - Clearing the Cache and Restarting as stated above worked perfectly!
  • dsdsdsdsd
    dsdsdsdsd about 8 years
    I have compile 'com.android.support:appcompat-v7:23.3.0' already in my build.gradle ... but still getting the error
  • behelit
    behelit over 7 years
    hmm I'm using a higher version but still encounter the issue compile 'com.android.support:appcompat-v7:23.1.1'
  • fWd82
    fWd82 almost 7 years
    +1, OMG this worked for me. :) BTW the + sign is for Hey Gradle if any new version of the same release is availible use is for yourself :) . I was using com.android.support:appcompat-v7:25.3.1 now this did a trick: com.android.support:appcompat-v7:25.3.+ Though AS giving me warning: Avoid using + in version numbers; can lead to unpredictable and unrepeatable builds (com.android.support:appcompat-v7:25.3.+)
  • George
    George over 6 years
    If it didn't work, try this one, worked for me: stackoverflow.com/a/36641765/3758439
  • haxpor
    haxpor over 6 years
    I use :22.0.0 and cannot find AppCompatActivity class. I have to set it at least to 22.1.0. Thanks.
  • A-Sharabiani
    A-Sharabiani about 6 years
    same in IntelliJ IDE.
  • Hassan Faghihi
    Hassan Faghihi about 6 years
    i delete gradle/cache, 4.4 & 2.2.x folders, didn't work, i did invalidate and restart in ide didn't worked, i delete .idea and .gradle folder sided in project files, and restart it, it worked just fine
  • Gabriel Ferreira
    Gabriel Ferreira about 6 years
    for me it only works when I delete the files (.idea/libraries) and re-imported the files
  • Paras Nakum
    Paras Nakum almost 6 years
    If above solutions do not work then check this stackoverflow.com/questions/34947794/…
  • Jethro
    Jethro almost 6 years
    Does not work! Delete the Libray folder in like in one a few down. It works for me stackoverflow.com/a/50129167/2121630
  • Admin
    Admin about 5 years
    Ty for the fix! Also, I would like to know if every user here was using the PC version or iOS version of Android Studio. I have some developer friends who never had any of these issues on iOS. I've had tons of issues on PC. Not sure how many times i've tried to clean/rebuild/invalidate cache...
  • Admin
    Admin about 5 years
    If you go inside the .idea folder and rename the "libraries" folder to something different, the trick works as well. If something goes terribly wrong, you only have to go back and rename the libraries folder back to it's original name and try something else, rather than try to restore the entire .idea folder.
  • Admin
    Admin about 5 years
    I would never delete the folder in case something goes terribly wrong. Simply renaming it will suffice. In the worst case scenario, that the libraries folder isn't regenerated, you can go back and restore the "libraries" folder's original name. After everything is known to work, you can then go back and delete the folder you renamed. If your studio is corrupted, it may not regenerate folders - always take caution when deleting files.
  • DBX12
    DBX12 almost 5 years
    Shahbaz Ali and I used the Linux version, moujib only commented that this solution works in 3.2.1
  • TechJ
    TechJ over 4 years
    For me the following worked : stackoverflow.com/a/54415300/5597466
  • NurShomik
    NurShomik almost 4 years
    This fixed it for me.