I\'m trying to figure out why the heck my Android studio isn\'t recognizing the AppCompat v7 library correctly. The import statement below shows up as gray and says there\'s
The solution I followed is summarized in the below steps:
The best solution is definitely to go to File>Invalidate Caches & Restart
Then in the dialog menu... Click Invalidate Caches & Restart. Wait a minute or however long it takes to reset your project, then you should be good.
--
I should note that I also ran into the issue of referencing a resource file or "R" file that was inside a compileOnly library that I had inside my gradle. (i.e. compileOnly library > res > referenced xml file) I stopped referencing this file in my Java code and it helped me. So be weary of where you are referencing files.
If you use androidX instead of android, you need change
import android.support.v7.app.AppCompatActivity;
to
import androidx.appcompat.app.AppCompatActivity;
and change
<android.support.constraint.ConstraintLayout>
to
<androidx.constraintlayout.widget.ConstraintLayout>
androidX users
Change your minSdkVersion to api level 21.
like this minSdkVersion 21
or build your app with compileSdkVersion 28
and also change targetSdkVersion
to targetSdkVersion 28
and you will see v7 error will gone. After that if you face a problem with creating Toolbar or other widget. press Alt+Enter and create a method for it.
For me,
Even after upgrading to appcompat-v7:22.1.0
, in which AppCompatActivty
is added,
the problem was not resolved for me, Android Studio was giving same problem
Cannot resolve symbol 'AppCompatActivity'
Sometimes clearing the android studio caches help.
In android studio I just cleared the caches and restarted with the following option--
File->Invalidate Caches/Restart
Background info:
My IDE
Android Studio 3.1.3
Build #AI-173.4819257, built on June 4, 2018
JRE: 1.8.0_152-release-1024-b02 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1
First solution: Import the project again and don't agree to upgrade the android gradle plug-in.
Second solution: Your files should contain these fragments.
build.gradle:
buildscript {
repositories {
jcenter()
google()//this is important for gradle 4.1 and above
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3' //this android plugin for gradle requires gradle version 4.4 and above
}
}
allprojects {
//...
repositories {
jcenter()
google()//This was not added by update IDE-wizard-button.
//I need this when using the latest com.android.support:appcompat-v7:25.4.0 in app/build.gradle
}
}
Either follow the recommendation of your IDE to upgrade your gradle version to 4.4 or consider to have this in gradle/wrapper/gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Optional change buildToolsVersion in app/build.gradle:
android {
compileSdkVersion 25
buildToolsVersion '27.0.3'
app/build.gradle: comment out the dependencies and let the build fail (automatically or trigger it)
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
//compile 'com.android.support:appcompat-v7:25.1.0'
}
app/build.gradle: comment in the dependencies again. It's been advised to change them from compile to implementation, but for now it's just a warning issue.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
}
After project rebuilding, the import statement shouldn't be greyed-out anymore; try to invoke Ctrl+h on the class. But for some reason, the error markers on those class-referencing-statements are still present. To get rid of them, we need to hide and restore the project tree view or alternatively close and reopen the project.
Finally that's it.
Further Readings:
Update Gradle
Use the new dependency configurations
If you prefer a picture trail for my solution, you can visit my blog