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

Viewed 52203

I had to completely rewrite the question.

I have a react native android app. When I built the apk file with ./gradlew assembleRelease -x bundleReleaseJsAndAssets, it went fine, but after that it stopped compiling at all. Even react-native run-android is not working anymore.

What I found so far: First, the error is this

Task :app:processDebugResources FAILED
resource android:attr/fontVariationSettings not found.
resource android:attr/ttcIndex not found.

If I add these lines to gradle.properties,

android.useAndroidX=true
android.enableJetifier=true

the error changes. Now it's this

Task :@JWWon_react-native-universal-pedometer:compileDebugJavaWithJavac FAILED

error: package android.support.annotation does not exist
import android.support.annotation.Nullable;
                                 ^
cannot find symbol
  private void sendPedometerUpdateEvent(@Nullable WritableMap params) {
                                         ^
  symbol:   class Nullable
  location: class BMDPedometerModule

The problem is not with the library though. If I remove it from the project, it start complaining about another one. To get it to compile, I have to remove 7 libraries. An example:

Task :@react-native-community_netinfo:compileDebugJavaWithJavac FAILED
error: package android.support.v4.net does not exist
import android.support.v4.net.ConnectivityManagerCompat;
error: cannot find symbol
    promise.resolve(ConnectivityManagerCompat.isActiveNetworkMetered(getConnectivityManager()));
                    ^
  symbol:   variable ConnectivityManagerCompat
  location: class ConnectivityReceiver
2 errors

then if I remove another, this happens:

Task :react-native-camera-kit:compileDebugJavaWithJavac FAILED
package android.support.annotation does not exist
import android.support.annotation.ColorInt;
                                 ^
package android.support.annotation does not exist
import android.support.annotation.IntRange;
                                 ^
...
92 errors

So it will compile if I remove 7 libraries from the project. They are:

react-native-camera-kit @react-native-community_netinfo react-native-push-notification react-native-sensors @JWWon_react-native-universal-pedometer react-native-keep-awake react-native-toast-native

Without them, it compiles perfectly. So there's a bigger problem that doesn't let it work. 2 days ago, all of those libraries were working perfectly with no problem. But now something crushes it.

7 Answers

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

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 react-native@0.59.9, use the latest android setting and the magic jetifier.

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

// build.gradle

implementation "androidx.annotation:annotation:1.1.0"

// where use it

import androidx.annotation.Nullable;

Improt missing classes from androidx at all places getting error.

add

android.useAndroidX=true
android.enableJetifier=true

in gradle.properties

Okay. It was pain in the ass for last 4 days, but I got it working for now.

What I did:

added

android.useAndroidX=true android.enableJetifier=true to gradle.properties

Changed sdk to 28

compileSdkVersion 28 buildToolsVersion '28.0.3'

And removed one library react-native-camera-kit. It all compiles now at least. Jetifier doesn't work with this library I guess. For now I have to turn it off.

Related