react native build error: package android.support.annotation does not exist

Try with jetifier

npm install --save-dev jetifier
Or use yarn, but install it locally in your project, not globally

npx jetify
or
npx jetify -w=1 - to specify the number of parallel workers

npx react-native run-android


allprojects {
  repositories {
      bla bla bla...
  }
  subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "26.+"
            }
            if (details.requested.group == 'com.google.android.gms'
            && !details.requested.name.contains('multidex') && !details.requested.name.contains('play-services-stats')) {
                details.useVersion "12.+"
            }
            if (details.requested.group == 'com.google.android.gms'
            && !details.requested.name.contains('multidex') && details.requested.name.contains('play-services-stats')) {
                details.useVersion "+"
            }
         }
      }
   }
}

add subprojects in build.gradle (android)

dependencies {
     ...bla bla bla

    implementation "com.google.android.gms:play-services-gcm:12.+"
}

add implementation "com.google.android.gms:play-services-gcm:12.+" in build.gradle (android/app)

so you don't need to migrate to Androidx

edit 1: code format

edit 2: missing a bracket


I actually had something very similar happen and running this worked

npx jetify

When I ran it through the CI pipeline it didn't work and ended having to add

"scripts": {
 ...
    "postinstall": "jetify"
}

After npm runs install in the pipeline it then run jetify to convert to androidx and covers all the libraries that need to be converted.


try to use androidx

// build.gradle 
implementation "androidx.annotation:annotation:1.1.0"

// where use it
import androidx.annotation.Nullable;

UPDATE:

if other libraries error, maybe you can try jetifier, I know it by this helpful issue.

the full reference is below, hope helpful : )

// android/build.gradle
ext {
    buildToolsVersion = "28.0.3"
    minSdkVersion = 24
    compileSdkVersion = 28
    targetSdkVersion = 28
    supportLibVersion = "1.0.0-beta01"
}

// app/build.gradle
dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "androidx.core:core:1.0.2"
    implementation "androidx.annotation:annotation:1.1.0"
    implementation "androidx.appcompat:appcompat:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:0.59.9"  // From node_modules
}

by the way, I meet this problem(AndroidX) for a few days, at last, solve it by update [email protected], use the latest android setting and the magic jetifier.