Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you import the NonNull annotation correctly into android studio?

I want to use annotations for NonNull, Nullable and UnsopportedAppUsage in my Handler-file, but the 2nd, 3rd and 4th line gives me an error. How do I fix this?

package android.os;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.util.Log;
import android.util.Printer;
like image 955
EdwardV Avatar asked Feb 28 '26 07:02

EdwardV


1 Answers

The answer is two-fold. For @NonNull and @Nullable do the following:

If you use androidx you need to add

implementation 'androidx.annotation:annotation:1.0.2'

or if you use the legacy support library (your imports are for this version)

implementation 'com.android.support:support-annotations:28.0.0'

to your dependencies in the build.gradle file. The version numbers might be different for you.

Note that there is also the android.annotation.Nullable class, but it's internal API of Android-OS (causing build errors if used).

For @UnsupportedAppUsage: This annotation denotes that something that is internal to the Android SDK and shouldn't be used is actually in use. Using this annotation in your code doesn't make a whole lot of sense. Additionally the annotation is hidden in the SDK and cannot be used by you.

like image 98
Jannik Avatar answered Mar 01 '26 20:03

Jannik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!