Improve Code Inspection with Annotations | Android Studio


Hi Devs, here is another post for all of you who wants their code to get inspected appropriately by Android studio.

For better code with reduced amount of bugs Google came up with another set of support library called Annotations. With the use of Annotations, lint able detect more appropriate bugs during lint inspection.

Android supports a variety of annotations for insertion in the methods, parameters, and return values in your code, for example:

Can be null



public void foo(@Nullable Object param){

Cannot be null.



public void foo(@NonNull Object param){

References a R.string resource.

    public abstract void setTitle(@StringRes int resId);

Calls from a UI thread.

    public abstract void setTitle(@StringRes int resId);

Calls from a worker/background thread.

And so on.

These annotations are even good to have in your API’s , so that anyone who wants to use your API can use it properly without any confusion, like passing resourceId rather than any random integer  or passing a valid string instead of null etc.

Now, How you can add this library in your Android app project ?

To add annotations to your code, first add a dependency to the Support-Annotations library:

  1. Select File > Project Structure.
  2. In the Project Structure dialog, select the desired module, click the Dependencies tab.
  3. Click the icon to include a Library dependency.
  4. In the Choose Library Dependency dialog, select support-annotations and click Ok.

The build.gradle file is updated with the support-annotations dependency.

You can also manually add this dependency to your build.gradle file, as shown in the following example.

dependencies {
    compile ''

Once you are ready with your annotations in your project, next thing will be to test how it works. Do the nullability analysis to test.

Nullability Analysis

Android Studio supports running a nullability analysis to automatically infer and insert nullness annotations in your code. A nullability analysis scans the contracts throughout the method hierarchies in your code to detect:

  • Calling methods that can return null
  • Methods that should not return null
  • Variables, such as fields, local variables, and parameters, that can be null
  • Variables, such as fields, local variables, and parameters, that cannot hold a null value

The analysis then automatically inserts the appropriate null annotations in the detected locations.

To run a nullability analysis in Android Studio, select Analyze > Infer Nullity. Android Studio inserts the Android @Nullable and @NonNull annotations in detected locations in your code. After running a null analysis, it’s good practice to verify the injected annotations.

courtesy – Google

For more information  –

Hope this will help you all to improve your code.

Do not forget the share it with your fellow developers

Happy coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *