问题
I recently updated Android Studio. Afterwards it seems to no longer being able to resolve some support annotations, such as @NonNull or @Nullable. It did work just fine before updating. However, everything is still compiling and I am able to execute Code, but still it shows the symbol cannot be resolved message at my imports.
What I have already tried so far:
Included the dependency in build.gradle (Module: Application):
compile 'com.android.support:support-annotations:27.0.0'- I have invalidated Caches and restarted
- Cleaning and rebuilding the project
- (Edit) Synched Gradle
I have no idea why this is not working and would really appreciate your help.
回答1:
As of android api 29 ('targetSdkVersion' 29) com.android.support has been moved to 'androidx' library.
In your build.gradle file(app level), dependencies block use...
dependencies{
...
implementation 'androidx.annotation:annotation:1.1.0'
}
And in your .java files use import as follows:
import androidx.annotation.NonNull;
Again build your project!
回答2:
Go to Build --> Rebuild Project. AS will refresh Gradle dependencies and your project too.
回答3:
Same thing happened to me. And I was stuck for quite some time--what a shitty trick to play on someone for keeping Android Studio up-to-date. And simply rebuilding the project was NOT even close to working.
Apparently some changes were made to gradle that prevents older programs from compiling properly.
I had to create a new project and copy all my files into the new project. Yeah, it bites big hairy green ones, but it was what I had to do to get it to work.
I hope someone comes up with a better answer than this! (And I'd love to "talk" for a few minutes with the idiot that made this change! This is not the first time this has happened--nor the 2nd, the 3rd, 4th, etc etc.)
!!! NEW INFO !!!
Just started a new project and wham, this happened again. And it was using the master/design template. Yup, exactly as Android Studio produced the code there are errors in ItemListActivity.java and other files because of the NonNull not importing correctly.
Sooooo, I went to the app build.gradle file and looked at all the dependency warnings. Why did the project generator use old dependencies? Hmmm. So I updated all the dependencies to use the latest. "Sync Now." Wow, it worked!
Here are the lines I updated:
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
Of course, you'll have to change the numbers to reflect the current versions.
Again, it boggles the imagination that straight-from-the-box templates have such glaring errors in them. Someday I'm going to take a journey to Mountainview and tie some shoelaces together.
回答4:
Include this one it will fix the error
annotationProcessor 'androidx.annotation:annotation:1.1.0'
来源:https://stackoverflow.com/questions/50470474/cannot-resolve-symbol-nonnull-after-android-studio-update