error: package com.android.annotations does not exist

164,664

Solution 1

To automatically fix all android to androidx issues for React Native (prerequisite npx)

Add the following two flags to true in your gradle.properties file at ProjectFolder/android/gradle.properties

android.useAndroidX=true
android.enableJetifier=true

Execute

npm install --save-dev jetifier
npx jetify
npx react-native run-android

In your package.json add the following to scripts

  "postinstall" : "npx jetify"

More info at https://github.com/mikehardy/jetifier

Update: This is now in-built in react-native 0.60. If you migrate to react-native 0.60 you won't need this step. - https://facebook.github.io/react-native/blog/2019/07/03/version-60#androidx-support

Solution 2

Use implementation androidx.appcompat:appcompat:1.0.2 in gradle and then

change import android.support.annotation.Nullable; to import androidx.annotation.NonNull; in the classes imports

Solution 3

Open gradle.properties and use following code:

android.useAndroidX=false
android.enableJetifier=false

or U can use these dependencies too:

implementation 'androidx.appcompat:appcompat:1.0.2'
 implementation 'androidx.annotation:annotation:1.0.2'

Solution 4

I had similar issues when migrating to androidx.

If by adding the below two lines to gradle.properties did not solve the issue

android.useAndroidX=true
android.enableJetifier=true

Then try

  1. With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar (developer.android.com)

If you still run into issues with migration then manually try to replace the libraries which are causing the issue.

For example

If you have an issue with android.support.annotation.NonNull change it to androidx.annotation.NonNull as indicated in the below class mappings table.

Class Mappings

Maven Artifact Mappings

Solution 5

In my case I had to use

import androidx.annotation...

instead of

import android.annotation...

I migrated to AndroidX and forgot to change that.

Share:
164,664
Moses
Author by

Moses

Updated on July 08, 2022

Comments

  • Moses
    Moses almost 2 years

    I have the following class

    import com.android.annotations.NonNullByDefault;
    
    @NonNullByDefault
    public final class Log {
        ...
    }
    

    and here is my build.gradle file (some parts omitted)

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 25
        buildToolsVersion '24.0.1'
    
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 25
            versionCode 2
            versionName "0.2"
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    
    }
    
    dependencies {    
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:support-annotations:25.0.0'
        compile 'com.android.support:design:25.0.0'
    }
    

    In Android Studio there is no warning raised for my class

    enter image description here

    However when I try to build and run my app I get this error from gradle

    Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
    Warning:[options] bootstrap class path not set in conjunction with -source 1.7
    /home/puter/git-repos/TaskManager3/app/src/main/java/com/treemetrics/taskmanager3/util/Log.java
    Error:(3, 31) error: package com.android.annotations does not exist
    Error:(7, 2) error: cannot find symbol class NonNullByDefault
    Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    Information:BUILD FAILED
    Information:Total time: 21.021 secs
    Information:3 errors
    Information:1 warning
    Information:See complete output in console
    
  • Moses
    Moses over 7 years
    I changed compile 'com.android.support:support-annotations:25.0.0' to compile 'com.android.support:support-v4:25.0.0' in my build.gradle and the import still reads import com.android.annotations.NonNullByDefault; and the error still occurs. Is this what you're talking about?
  • Moses
    Moses over 7 years
    By the way, these are automatic imports being resolved by Android Studio.
  • Moses
    Moses over 7 years
    Android Studio is automatically resolving the import for me, so it's coming from somwhere, but now I am not sure where. It is also still a mystery as to why the error is being thrown.
  • Sanjeet
    Sanjeet over 7 years
    If the imports are resolved correctly then the error should not have thrown.
  • Moses
    Moses over 7 years
    That's my point, why is Android Studio resolving the import like this and not underlining the import in red, but gradle is throwing an error?
  • wesley franks
    wesley franks about 5 years
    This doesn't work for me. I change the import and android automatically replaces it. Causing issues because it needs to be androidx but puts android.support imports.
  • RajnishCoder
    RajnishCoder almost 5 years
    ok i have more than 100 imports in different file of library, what to do ?
  • Belal mazlom
    Belal mazlom almost 5 years
    Thank you so much, this tool helped me a lot and saved my time, I hope I can give you more than a vote
  • Ernestyno
    Ernestyno almost 5 years
    First install npm, when start from npx jetify. Worked. Thanx!
  • BMX
    BMX almost 5 years
    @RajnishCoder use jetifier which does it for you.
  • Omar Aziz
    Omar Aziz over 4 years
    tried to add them under dependencies, but getting > Could not find method compile() for arguments [androidx.appcompat:appcompat:1.0.2] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDe‌​pendencyHandler.
  • Omar Aziz
    Omar Aziz over 4 years
    also tried implementation instead of compile, same error
  • Fareed Alnamrouti
    Fareed Alnamrouti over 4 years
    Can you run this command from the command line “npx jetify” in the Android project root directory
  • Sanjay Joshi
    Sanjay Joshi over 4 years
    Yeah, I did that on the root level. For some reason, I had to run npm jetify manually in order to run the app properly. For the same directory, the android studio was throwing can't run npx jetify error on the directory. even though it was the project root repo. Thanks for your concerns.
  • Chen Li Yong
    Chen Li Yong over 4 years
    This is amazing! For some reason, the project I'm joining on is still using react-native 0.59.0, which is the source of my frustration when trying to compile some of the existing code from github and failed. While the decision to migrate to react native 0.60.0 is not within my hand, this "execute" block here is the live saver!
  • Dr. Div
    Dr. Div over 4 years
    Life Saver! Thanks so much!
  • Raphael Pinel
    Raphael Pinel over 4 years
    Is it necessary to change Nullable to NonNull, or is it just a typo?
  • Buwaneka Sudheera
    Buwaneka Sudheera about 4 years
    You saved my day. Thank you.
  • Andriy Makukha
    Andriy Makukha about 4 years
    Both refactoring and changing from import android.support.annotation.NonNull; to import androidx.annotation.NonNull; worked for me.
  • sejn
    sejn almost 4 years
    I am using RN 0.59.9. Shall I use Jettifier and Android X. But I not need to migrate AndroidX. Why it getting issue with this?
  • Pavan Garre
    Pavan Garre almost 4 years
    @sejn everybody has to upgrade to androidX. You can read more info here developer.android.com/jetpack/androidx/migrate
  • ChiragMS
    ChiragMS almost 4 years
    @PavanGarre I don't get why this is not marked as an answer. It worked like a charm for me and I must say, not in a million years I would have guessed it. Thanks man. Appreciate it.
  • William
    William over 2 years
    Thanks you. The links to the Class and Artifact Mappings were what I have been searching for for ages.
  • zr0gravity7
    zr0gravity7 over 2 years
    Why does one need to install jetifier as a dev dependency?
  • Ahmed Mahmoud
    Ahmed Mahmoud about 2 years
    Thanks a lot for your time and effort!