Android Annotations

Updated 30 August 2020

Save

We are going to learn Android annotation in this blog, You have to need a basic knowledge for android app development.

We will cove this blog by some below points -:

  1. Why we use Android annotations?
  2. How can use Android annotation in our program?
  3. Create custom annotations?

 

 

Why we use Android annotations?

Annotations allow you to provide hints to code inspections tools like Lint, to help detect these more fine code problems. They are added as metadata tags that you attach to variables, parameters, and return values to inspect method return values, passed parameters, local variables, and fields.

When used with code inspections tools, annotations can help you detect problems, such as null pointer exceptions and resource type conflicts.

How can we use Android annotations?

Note: If a module has a dependency on an annotation processor, you must use the ‘annotation processor’ dependency configuration to add that dependency. To learn more, read Use the annotation processor dependency configuration.

 

To enable annotations in your project, add the support-annotations dependency to your library or app. Any annotations you add then get checked when you run a code inspection or lint task.

Add the support annotations library dependency

The Support Annotations library is published on Google’s Maven Repository. To add the Support Annotations library to your project, include the following line in the dependencies block of your build.gradle file.

 

Note: If you’re using the appcompat library, you do not need to add the support-annotations dependency. Because the appcompat library already depends on the annotations library, you have access to the annotations.

Run code inspections

To start a code inspection from Android Studio, which includes validating annotations and automatic Lint checking, select Analyze > Inspect Code from the menu bar. Android Studio displays conflict messages to flag potential problems where your code conflicts with annotations and to suggest possible resolutions.

Note that although annotation conflicts generate warnings, these warnings do not prevent your app from compiling.

Nullness annotations

 

The above example attaches the @NonNull annotation to the context and attrs parameters to check that the passed parameter values are not null. It also checks that the onCreateView() method itself does not return null.

Please note that for Kotlin, we do not need to use the @NonNull annotation because it will be automatically added to the generated bytecode when we specify a non-nullable type.

Resource annotations

Validating resource types can be useful because Android references to resources, such as drawable and string resources are passed as integers.

Code that expects a parameter to reference a specific type of resource, for example, Drawabless can be passed the expected reference type of int but actually reference a different type of resource such as an R.string resource.

For example, add @StringRes annotations to check that a resource parameter contains an R.string reference, as shown here.

 

Annotations for the other resource types, such as @DrawableRes, @DimenRes, @ColorRes, and @InterpolatorRes can be added using the same annotation format and run during the code inspection.

Thread annotations

Thread annotations check if a method is called from a specific type of thread. The following thread annotations are supported -:

If all methods in a class share the same threading requirement, you can add a single thread annotation to the class to verify that all methods in the class are called from the same type of thread.

A common use of the thread annotation is to validate method overrides in the Async Task class because this class performs background operations and publishes results only on the UI thread.

Same we can useĀ  Value constraint annotations, Permission annotations, Return value annotations, etc.

Creating Custom Annotations in Android

For Example, Let’s create @Status annotation
Create a Status interface like below

 

@Target specifies where annotation can be placed. If you don’t specify this, the annotation can be placed anywhere. Here are some valid targets-:

ElementType.TYPE (class, interface, enum)
ElementType.FIELD (instance variable)
ElementType.METHOD
ElementType.PARAMETER
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE

@Retention – defines how long the annotation should be kept around. Here are some valid retention policies.

RetentionPolicy.SOURCE – Discard during the compile step. These annotations don’t make any sense after the compilation has completed, so they do not need to be turned into bytecode. Examples: @Override, @SuppressWarnings.

RetentionPolicy.CLASS – Discard during the class load. Useful when doing bytecode-level post-processing. Somewhat surprisingly, this is the default.

RetentionPolicy.RUNTIME – Do not discard. This annotation should be available for reflection at runtime.

Use your custom annotation in ABC.java class

 

 

Now you can print the status of all the methods like below

 

Check out our latest blogs – https://mobikul.com/blog/

Reference


Thank you for read

 

author
. . .

Leave a Comment

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


2 comments

  • divs3122
    • Neeraj Sharma (Moderator)
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home