Home » Android Tips & Tricks » Error: Package android.support.annotation does not exist

Error: Package android.support.annotation does not exist

Android is growing every day. There are various developments in it which add new tools to it. Android Developer, Gradle, and Android Studio are some of its tools. One such tool is Android Support Annotation. In this article, we will try to learn what is android support annotation? And what is the error: package Android.support.annotation does not exist?

What is Android Support Annotation?

Android support annotation is a tool by which using annotations the users are able to avoid bugs when they are writing codes. A lint checks the annotation. If a user is having some trouble with annotations or is violating rules defined by the annotations then the lint will give notification to the user. The notification will serve as a warning to the user. If a user wants to add annotation support, then he must add the app compat. Else If a user wants to add the annotation library manually, he must add some line of codes in his Gradle dependencies. If the user is able to sync the project with the Gradle files then the annotations will be available for him. There are several annotations that can be grouped by their functionality or goal.

Error: Package android.support.annotation does not exist
Error: Package android.support.annotation does not exist

Annotations allow a user to provide a hint to detect problems in code by using Lint. Lint is a code inspection tool. The hints are added as metadata tags with parameters, variables and return values. Annotations help to find problems in code such as resource type conflicts or null pointer exceptions with code inspection tools.

Don’t miss: Error Code d0111-How to Fix?

How to add annotations to a project?

The users need to add support-annotations dependency in their library to enable annotations in their project. This annotation library is not included in the project as default. This library is shipped as a distinctive library. A code inspection must be run to check the annotations added by the user. A user can also add the annotation support library to their project. To do so, they have to add a line of code in the dependencies block of the build.gradle file. Then they have to sync the notification in the toolbar by clicking on the Sync Now option. Annotations are included as a part of the AAR (Android Archive) artifact when a user adds annotations to their library. The AAR is in the annotation.zip file in XML format. 

The user can use the auto-complete feature to display a complete list of annotations in the support repository.

The users can also start to inspect code from Android Studio. They need to validate annotations and avail Automatic Lint Checking. They can go to the Analyze option in the menu bar. Now they can click on Inspect Code. The conflict messages will be displayed by Android Studio. These messages have problems where the code of the user conflicts with their annotations. This also suggests a possible solution to the user. The users can run the lint task in the command line to enforce annotations. The lint will not enforce nullness annotation but will flag problems with the integration server. Annotations will give general warnings to the user but these warnings will not prevent the app from compiling the code.

Annotation Classes:

There are few annotation classes in the annotation support library. They are:

Nullness Annotation

These annotations are used to check the variable nullness or nullness of a parameter value or method return value. This annotation implies that the value of a variable or method return value or parameter value never null. If this is violated by any user then the Android Studio issues a warning. A nullable allows a variable to have null values and the method can also return null values. Nullable annotation performs a null check on variables or return values.

Resource Annotations

 In Android, resource annotations are passed as integers. An int pointing to a drawable can be passed to a code that is expecting a string resource id. The compiler will accept such a condition. Resource annotations allow type checking in such situations. Android resource prototype has a corresponding resource standard annotation. If there is a resource type then there will be its corresponding resource type as well. A special resource annotation is used when there is no specific resource type of a resource but it must have a resource type.

Thread Annotations

Thread annotation is used to check a method for a specific type of thread. There are four types of thread annotations supported in Android Studio: UiThread, MainThread, WorkerThread, and BinderThread. The MainThread and UiThread annotations can be exchanged the method calls are allowed from both these threads in thread annotations. If all methods in a class are called from the corresponding thread, then a class can be annotated with a thread annotation. The main thread is only allowed to call the onProgressUpdate method. If any other thread calls this method, then there will be an error message.

Value Constraints Annotations

Original annotations are used to verify passed parameters value. The validation is to check whether the parameter is under the designated range of integer values.

CheckResult Annotations

This annotation is used to build the effect of an order practiced. It can be a return value as well. This annotation is to help users to solve their confusion regarding a method having any side effects.

What is Error: package Android.support.annotation does not exist?

Error: package Android.support.annotation does not exist is an error message that the users receive when they try to migrate Android X. When a user tries to build or run any application in Android X, they get this error message from the Gradle. This issue is faced by many users and it comes from the dependency on the Butter Knife Library. When a user, uses a butter knife version less than 9.0.0 then they get this error message.

read also: java.net.connectexception: connection refused: connect error installing apk

How to fix Error: package Android.support.annotation does not exist error?

There are few methods which a user can try to fix the error: package Android.support.annotation does not exist error:

Note: To fix this error and try any method a user must have Android Studio version 3.5.1 and above. The track of JDK must be 1.8.com. The Gradle must have 3.5.1 and above track of migration to the Android X project.

Method 1: Check if build.gradle is configured properly

A user must add configuration to their Gradle. First, a user needs to search for the build. Then he must check if the Gradle has the keyword com.android.support:support-annotations. If it is not present then the user needs to the configuration in the Gradle. 

The line of code will be “dependencies {implementation fileTree (dir: ‘libs’, include: [‘*.jar’]) implementation “com.android.support:support-annotations:28.0.0”}

This line of code should be added under dependencies. 

Method 2: The error file must be modified

The user must view the file that has the error and modify the import package in it. The user must add the code- “ import android.annotation.IntDef; import android.annotation.WorkerThread;”.  The user has to replace the path. To open it, he can press ctrl+shift+r and import android.support annotation with import android annotation. The user can also sync the project with the Gradle Files. There is an option to sync at the top of the screen. The user has to click on it and synchronize the project sync.

Method 3: Automatic Fix for Android Issues

If a user wants to fix android to android problems automatically for react native then he can add two flags in the Gradle file. The flag will be “android.useAndroidX=true”  and “android.enableJetifier=true”. The users need to execute the package in.json and add the script as “post-install” : “npx jetify”. Now some react-native has come up with inbuilt and the user need not migrate to react native 0.60. The native code and dependencies are also migrated with react native migration to AndroidX. React Native apps can use AndroidX without any migration. 

Method 4: Use Implementation

The users can use the implementation in Gradle and change import android.support.annotation.Nullable; to import android.annotation.NonNull; These changes are to be made in the class imports. The implementation to be used in the Gradle is android.appcompat:appcompact: 1.0.2 The users can also open the Gradle and use the code- “android.useAndroidX=false” and “android.enableJetifier=false”. They can even use the dependencies as implementation ‘androidx.appcompat:appcompat:1.0.2’ or implementation ‘androidx.annotation:annotation:1.0.2’. 

The users can also add “android.useAndroidX= true” and “android.enableJetifier=true” in the gradle.properties.

Method 5: Migrate an existing project to AndroidX

The users can also migrate the project in Android Studio version 3.2 or the other higher version to AndroidX. They can do this by selecting refactor option. They will find Migrate to AndroidX option in the menu bar. The users need to click on it to migrate their project. If this does not solve the migration issues of the users, they can also do it manually. The users have to replace the libraries that are causing the migration problem. The class mappings indicate which library has to change and what should be added in its place. For example, a user can change android.support.annotation.NonNull to android.annotation.Nonnull, if the former is causing any problem. This change of the library is indicated in the class mappings.

Method 6: Use Jetify to edit codes

The users should use jetify to edit codes instead of opting to manually edit them. If a user is running code in the Android studio, jetify has to be run as a pre-build function. In the menu, the users can go to the Edit Configurations option. There will be a before launch option at the bottom of the screen. The user needs to click on the plus icon and select Run External Tool from it. The user will encounter a dialogue box. He has to fill in the information in that dialogue box like name, group, description, tool settings, program, arguments and working directory. The working directory has to be the project root directory and not the android directory. Click on Ok and run the dialogue box. The settings will be configured for the jetify to run as a pre-build.

Method 7: Update Butterknife file

If a user finds a problem in the butterknife file, then he has to update the butterknife dependency version. The user needs to replace some codes. If there is any import issue then the user can use ATL+Enter on that class. It will give an option to import the class to resolve the issues.

Conclusion:

Android support annotation is a tool by which the users are able to avoid bugs while writing codes using annotations. It can be added to a project and has five classes. When the users try to migrate their project to Android X they receive an error message as “Error: package Android.support.annotation does not exist”. This issue arises when the users use the butterknife version less than 9.0.0. to build any app in AndroidX. There are few methods to fix this error message mentioned in the article above.

Similar Posts