236

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?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Lingviston
  • 5,479
  • 5
  • 35
  • 67

38 Answers38

376

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.

Akshu Gautam
  • 71
  • 1
  • 3
Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
  • 3
    Doesn't work for me either, each time I upgrade the appcompat, I get into lots of problems. – 3c71 Aug 18 '15 at 13:59
  • For me this error popped up when updating to Android Studio 2.0 - Clearing the Cache and Restarting as stated above worked perfectly! – Chris Klingler Apr 10 '16 at 03:34
  • I have `compile 'com.android.support:appcompat-v7:23.3.0'` already in my `build.gradle` ... but still getting the error – dsdsdsdsd Apr 23 '16 at 14:58
  • 4
    If it didn't work, try this one, worked for me: https://stackoverflow.com/a/36641765/3758439 – George Sep 26 '17 at 21:09
  • same in IntelliJ IDE. – A-Sharabiani Feb 24 '18 at 20:03
  • 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 – Hassan Faghihi May 05 '18 at 09:36
  • for me it only works when I delete the files (.idea/libraries) and re-imported the files – Gabriel Ferreira May 17 '18 at 19:55
  • If above solutions do not work then check this https://stackoverflow.com/questions/34947794/cant-resolve-appcompatactivity/50897316#50897316 – Paras Nakum Jun 17 '18 at 13:56
  • Does not work! Delete the Libray folder in like in one a few down. It works for me https://stackoverflow.com/a/50129167/2121630 – Jethro Jun 23 '18 at 20:54
  • For me the following worked : https://stackoverflow.com/a/54415300/5597466 – TechJ Feb 06 '20 at 10:08
170

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

DBX12
  • 2,041
  • 1
  • 15
  • 25
  • 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... –  Feb 27 '19 at 08:15
  • Shahbaz Ali and I used the Linux version, moujib only commented that this solution works in 3.2.1 – DBX12 Jul 31 '19 at 05:07
73

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 7
    hmm I'm using a higher version but still encounter the issue `compile 'com.android.support:appcompat-v7:23.1.1'` – behelit Oct 19 '16 at 09:37
37

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.

Community
  • 1
  • 1
Abdul Mateen
  • 1,139
  • 1
  • 14
  • 21
  • 2
    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. –  Feb 27 '19 at 08:22
  • This fixed it for me. – NurShomik Aug 05 '20 at 19:24
  • 1
    After deleting the library folder, come back to android studio. "Do Sync Project with Gradle files" – sabari Jul 21 '22 at 03:56
28

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.

  • 7
    +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.+)** – fWd82 Jul 08 '17 at 07:08
15

Check for typos too, i wasted my whole day looking for solutions when i had actually typed AppCompactActivity instead of AppCompatActivity

Victor Michael
  • 223
  • 3
  • 7
15

Cannot resolve symbol AppCompatActivity Issue.

Do the Simple Step to resolve the problem.

i) Exit the Android studio.

ii) Go to your project directory.

iii) Find the .idea folder in your project directory.

iv) Delete .idea folder.

v) Restart your android studio.

vi) The Issue Will be resolved.

  • 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. –  Feb 27 '19 at 08:17
14

When you add AndroidX support in Gradle.properties file e.g.

android.useAndroidX=true
android.enableJetifier=true

If you are using Gradle version greater than 3.2, the conventional Appcompat dependancy e.g.

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

wont work. Replace this dependancy with:

implementation 'androidx.appcompat:appcompat:1.1.0'

Moreover, change the AppCompatActivity import in your class file from this:

import android.support.v7.app.AppCompatActivity;

to this:

import androidx.appcompat.app.AppCompatActivity;

Done

13

I got the Cannot resolve symbol error for AppCompatActivity and many other classes & methods after updating Android Studio from 2.2.3 to 2.3, updating Android Gradle plugin to 2.3.0, and updating Gradle to 3.3.

File -> Invalidate Caches / Restart did not resolve the issue.

Preferences -> Build, Execution, Deployment -> Gradle showed Use local gradle distribution selected with Gradle home: /Applications/Android Studio.app/Contents/gradle/gradle-2.14.1 and a warning displayed saying the gradle location was incorrect.

Resolved issue by selecting Use default gradle wrapper (recommended).
Probably just pointing to the new local distribution location would also work.

jk7
  • 1,958
  • 1
  • 22
  • 31
10

Try this. In Android Studio, after putting the dependency in build.gradle. Go to Build --> Clean Project.

It worked for me.

MobileAppDeveloper
  • 1,048
  • 2
  • 16
  • 27
7

Thats really insane, i tried everything, synced with Gradle files, invalidated and restarted android studio. Still the problem persisted. Last resort was deleting .idea/libraries folder and it worked as charm.

Raghav Sharma
  • 780
  • 10
  • 18
6

You can use new AppCompatActivity appcompat-v7:22.1.+ Just add this to your gradle:

compile 'com.android.support:appcompat-v7:22.1.1'

Sergey Zabelnikov
  • 1,855
  • 1
  • 14
  • 24
6

For me the issue resolved when i updated the appcompact v7 to latest..

compile 'com.android.support:appcompat-v7:25.3.0'

Hope it helps...:)

Nihas Nizar
  • 619
  • 8
  • 15
6

So my collegue pushed their local.properties which broke everything. I copied my sdk path from another app and did:

File -> Sync Project with Gradle Files
Seabass77
  • 187
  • 5
  • 13
5

I was getting this same problem with Android SDK 23, while developing on a Mac OS X Yosemite Machine. It turned out that I did not have Java SDK 7.

Once I installed Java SDK 7 and pointed the SDK from with the Android Studio to the new SDK, everything worked for me.

Here are the steps that I followed:

1) Shutdown Android SDK

2) Install Java SDK for Mac OS X from http://www.oracle.com/technetwork/java/javase/downloads/index.html

3) Start the Android SDK and point the SDK for this project to the new 1.7 Java SDK by going to File -> Project Structure -> JDK Location

4) Restart Android Studio

Hope this help

AsgharHussain
  • 51
  • 1
  • 1
4

none of below solved my issue

  • Restart Android
  • File >> Synch Project with Gradle Files
  • Build >> Clean Project
  • Build >> Rebuild Project
  • File >> Invalidate Caches / Restart

Instead, I solved it by updating the version of appcompat & design dependencies to the recent version To do so: go to build.grade (Module:app) >> dependencies section and then hit ALT + ENTER on both appcompat & design dependencies then select the shown version in my case it's 24.2.1 as shown in the picture enter image description here

Zain
  • 37,492
  • 7
  • 60
  • 84
3

Remember to press Alt+Enter or add the import.

import android.support.v7.app.AppCompatActivity; 
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
3

I updated my Gradle 2.3.3 to 4.4 and got an error on AppCompatActivity and also Cardview. I tried with clean project and rebuild project. It won't work, then I go to Project Folder -> .idea -> create backup of libraries folder and remove it -> then Rebuild Project that solved my issue.

manojm
  • 41
  • 6
3

Lets get going step by step: first clean project by using

Build->Clean

if this doesn't helps then use your second step

File>Invalidate Caches/Restart...

But the real problem begins when all the above options doesn't works so use your ultimate solution is to close project and go to project location directory and delete

.idea

You can open your project now again.

shashank chandak
  • 544
  • 5
  • 13
2

After upgrading to the latest support library, I had to upgrade to the latest Android studio (beta) version and gradle version before the IDE recognized AppCompatActivity again (invalidating caches and restart did not do the trick)

Divisible by Zero
  • 2,616
  • 28
  • 31
2

Add this dependency in project build .gradle.

Follow the example below.

dependencies {

    compile 'com.android.support:appcompat-v7:22.1.0'
}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • I use `:22.0.0` and cannot find `AppCompatActivity` class. I have to set it at least to `22.1.0`. Thanks. – haxpor Feb 03 '18 at 01:39
2

Easist Way

  • Open app level build.gradle and remove appcompact-v7 dependency & Sync.
  • Add dependency again & Sync.

Error gone!

Before

before

After

after

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

I solved it adding:

import androidx.appcompat.app.AppCompatActivity;

in the "import" zone of the main .java file. It worked for me.

Hope it helps!

Zain
  • 37,492
  • 7
  • 60
  • 84
Rorro
  • 1,227
  • 1
  • 11
  • 17
1

You Have to just Do some change in your Gradle App File by adding some dependency

compile "com.android.support:appcompat-v7:XX:YY:ZZ"

while XX:YY:ZZ is the version code that you are using in your gradle file, otherwise if you set this version lower or higher than that you are using , then your app will face some problems like it will crash sometimes

Nadeem Bhat
  • 1,032
  • 7
  • 10
1

i just update my android studio from 2.3.2 to 3.1.2 version. and this error occurs.

i clean project, rebuild project, invalidate cache/restart but nothing works.

i resolve this error by updating appcompat library from

compile 'com.android.support:appcompat-v7:23.4.0'

to

compile 'com.android.support:appcompat-v7:25.1.1'

in app build.gradle file.

mehmoodnisar125
  • 1,469
  • 18
  • 14
1

If Invalidating caches not helps, just close the project then re-open it. Steps:

  • File > Close Project
  • Remove the project out of the starting project list
  • Close Android Studio
  • Open Android Studio
  • Open existed Project with this project
Nguyen Minh Hien
  • 455
  • 7
  • 10
1

This happens because of one the following reasons:

  1. You do not have the SDK API installed (this was my problem)
  2. Your project/Android Studio doesn’t know where the needed files are
  3. Your project references an outdated compile version that does not support AppCompatActivity
  4. There is a typo

Possible solutions:

  1. Check your .gradle file to make sure you’re not referencing an outdated version. AppCompatActivity was added in version 25.1.0 and belongs to Maven artifact com.android.support:appcompat-v7:28.0.0-alpha1, so do not use any version earlier than this. In your build.gradle (Module: app) file you should have the dependency listed:

    dependencies { compile 'com.android.support:appcompat-v7:25.1.0' }

    You may be using a different version, but just make sure you have listed the dependency.

  2. Open the SDK manager and download every API 7 or newer. If you were missing the needed API it will fix that issue, and downloading all the newer API’s can save you some hassle later on as well.

  3. Check for typos in your import statement. Make sure it doesn’t say “AppCompactActivity” instead of “AppCompatActivity”.
  4. Resync your project (File >Sync project with Gradle files)
  5. Rebuild your project (Build >rebuild)
  6. Clean your project (Build >clean project)
  7. Close and restart Android Studio
  8. Update Android Studio
  9. Regenerate your libraries folder – In Android Studio, view your files in project view. Find the folder YourProjectName >.idea >libraries. Rename the folder to “libraries_delete_me”. Close and reopen Android Studio. Open your project again and the libraries folder should be regenerated with all of the files; you can now delete the “libraries_delete_me” folder.
SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54
0

For me, the issue resolved when I updated the Gradle build version. Don't know why?

Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
0

I'll tell you how did i solved this problem in eclipse. Download zip file from this link.https://github.com/dandar3/android-support-v7-appcompat then extract this file,open project, in lib folder you will see one jar file named "android-support-v7-appcompat" , add this jar file into your project. Thats it. enter image description here

Däñish Shärmà
  • 2,891
  • 2
  • 25
  • 43
0

This can might be in the version difference in the app level gradle check it once and then re-build

Kamal
  • 245
  • 2
  • 3
0

For me worked no one of the solutions. But I found out that all the libraries of the support libraries were missing at my external libraries. And in other projects the AppCompatActivity was resolved without problems.

For me it worked to create a new project and copy all the stuff of the old src folder into the new project.

I don't know exact why, but if nothing works, it could be the last possibility. Just if someone has the same trouble as me.

user7174483
  • 73
  • 1
  • 9
0

By trying soft ways it wont works, use the hard way: Exit Current Android Studio Screen and goto your project to the .idea folder Rename the libraries->lib_pre folder.

Restart Android Studio.It will now recreate the libraries folder and work again Smoothly. Android Studio 3.1.3

Nadeem Bhat
  • 1,032
  • 7
  • 10
0

This is the lifesaver code I found somewhere(sorry don't remember the origin). Add it to build.gradle (Module:app). You can replace the version to the relevant.

configurations.all {

    resolutionStrategy.eachDependency { DependencyResolveDetails details -
        def requested = details.requested

 if (requested.group == 'com.android.support') {

            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        } else if (requested.group == "com.google.android.gms") {
            details.useVersion '15.0.0'
        } else if (requested.group == "com.google.firebase") {
            details.useVersion '15.0.0'
        }
    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
Victoria Klimova
  • 2,795
  • 1
  • 11
  • 12
0

If all else has failed, make sure you have added the import statement. I had removed it as it was showing error and forgot to add it back and was trying everything else to fix:

import android.support.v7.app.AppCompatActivity;

Srihari Karanth
  • 2,067
  • 2
  • 24
  • 34
0

After trying literally every solution, I realised that the project I had been working on was previously using the latest Android Studio which was 3.2 at the time and the current pc I was using was running 2.2 after updating android studio this seemed to fix the issue completely for me.

Solution: Android Studio -> Check For Updates and then install latest build

Louis Ferraiolo
  • 409
  • 4
  • 18
0

The error came when i added firebase to the project with all dependencies updated to latest version on API 28 although my google services were using 4.1.0 version.

I removed the error, when i updated it to 4.2.0.

Lovish-Pandey
  • 135
  • 1
  • 15
0

In addition to other answers, adding below dependencyResolutionManagement in settings.gradle solved the problem.

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

Note: I was migrating the old android project to AndroidX. After following all mandatory steps ended at this issue Cannot resolve symbol 'AppCompatActivity'. After trying all the other solutions mentioned here issue was not resolved. When checked with settings.gradle and adding the above code solved the issue.

Anand
  • 857
  • 1
  • 12
  • 18
0

Upgrade your gradle version to solve this issue

Goto your project level build.gradle

If you are using an old gradle version, upgrade to latest

 classpath 'com.android.tools.build:gradle:4.0.2'

gradle-wrapper.properties

change distributionUrl to latest version

distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159