27

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:

  1. Included the dependency in build.gradle (Module: Application):

    compile 'com.android.support:support-annotations:27.0.0'
    
  2. I have invalidated Caches and restarted
  3. Cleaning and rebuilding the project
  4. (Edit) Synched Gradle

I have no idea why this is not working and would really appreciate your help.

Andi Z.
  • 271
  • 1
  • 3
  • 4
  • Related post - [Cannot resolve symbol nonnull and notnull in looper.java](https://stackoverflow.com/q/35201143/465053) – RBT Sep 08 '18 at 02:12

7 Answers7

45

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!

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
userAbhi
  • 695
  • 6
  • 10
  • 3
    Makes me feel Android SDK vendors wants developer to build their code every day and make sure it still builds wihout errors. Seriously, just few month back successfully built code stops building now. Thanks for help anyways. – Atul May 01 '20 at 12:31
7

I faced similar issue recently with one library import and to fix this issue I had to make chances to import statement as below:

Had to change following two imports.

From:

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

To:

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Akhilesh
  • 196
  • 3
  • 5
6

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.

SMBiggs
  • 11,034
  • 6
  • 68
  • 83
1

Go to Build --> Rebuild Project. AS will refresh Gradle dependencies and your project too.

Yousef Gamal
  • 1,026
  • 2
  • 17
  • 32
Mohit patel
  • 296
  • 1
  • 8
0

Click on the red bulb icon and the select add annotation to classpath. It will resolve the issue.

Sibam
  • 23
  • 7
0

If you are using Android Studio use the Refactor -> Migrate to AndroidX option and perform the migration.

rams
  • 6,381
  • 8
  • 46
  • 65
0

Add this to build.gradle(app-level):

implementation 'org.jetbrains:annotations:16.0.2'

And use @NotNull instead of @NonNull.

Here are the imports for @Nullable and @NotNull in your Java file:

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

If this still not works then create your own NotNull or NonNull, Here is the code to create your own NonNull:

@Documented
    @Retention(RetentionPolicy.CLASS)
    @Target({METHOD, PARAMETER, ElementType.LOCAL_VARIABLE,ElementType.FIELD})
    public @interface NonNull{
        //Created by own NonNull
    }

For Nullable:

@Documented
    @Retention(RetentionPolicy.CLASS)
    @Target({METHOD, PARAMETER, ElementType.LOCAL_VARIABLE,ElementType.FIELD})
    public @interface Nullable{
        //Created by own Nullable
    }
 

Happy coding!