109

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
Pavan Garre
  • 2,661
  • 1
  • 18
  • 19
Adam
  • 2,167
  • 5
  • 19
  • 33

14 Answers14

187

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

Pavan Garre
  • 2,661
  • 1
  • 18
  • 19
  • 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? – sejn Jul 22 '20 at 12:11
  • @sejn everybody has to upgrade to androidX. You can read more info here https://developer.android.com/jetpack/androidx/migrate – Pavan Garre Jul 22 '20 at 16:17
  • Why does one need to install jetifier as a dev dependency? – zr0gravity7 Mar 12 '22 at 02:47
44

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

Sarthak Singhal
  • 1,175
  • 12
  • 16
20

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'
Behrouz.M
  • 3,445
  • 6
  • 37
  • 64
  • 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.DefaultDependencyHandler. – Omar Oct 28 '19 at 16:22
  • also tried implementation instead of compile, same error – Omar Oct 28 '19 at 16:22
16

In my case I had to use

import androidx.annotation...

instead of

import android.annotation...

I migrated to AndroidX and forgot to change that.

ribbit
  • 1,183
  • 11
  • 14
16

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

SpaceX
  • 2,814
  • 2
  • 42
  • 68
  • Both refactoring and changing from `import android.support.annotation.NonNull;` to `import androidx.annotation.NonNull;` worked for me. – Andriy Makukha Jun 02 '20 at 08:15
  • Thanks you. The links to the Class and Artifact Mappings were what I have been searching for for ages. – William Dec 04 '21 at 22:32
8

For Ionic, try this:

ionic cordova plugin add cordova-plugin-androidx
ionic cordova plugin add cordova-plugin-androidx-adapter

The error comes because this app is not using androidX but these plugins solve errors.

Nazim Ali
  • 81
  • 1
  • 1
6

just install(it automatically fix)

npm install --save-dev jetifier <--------avoid if aleardy installed start from below lines
npx jetify

then run

npx cap sync

finally

  npx react-native run-android

  
Balaji
  • 9,657
  • 5
  • 47
  • 47
3

Annotations come from the support's library which are packaged in android.support.annotation.

As another option you can use @NonNull annotation which denotes that a parameter, field or method return value can never be null.
It is imported from import android.support.annotation.NonNull;

Sanjeet
  • 2,385
  • 1
  • 13
  • 22
  • 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? – Adam Nov 02 '16 at 15:05
  • By the way, these are automatic imports being resolved by Android Studio. – Adam Nov 02 '16 at 15:07
  • If the imports are resolved correctly then the error should not have thrown. – Sanjeet Nov 02 '16 at 15:23
  • 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? – Adam Nov 02 '16 at 15:47
3

You shouldn't edit any code manually jetify should do this job for you, if you are running/building from cli using react-native you dont' need to do anything but if you are running/building Andriod studio you need to run jetify as pre-build, here is how can you automate this:

1- From the above menu go to edit configurations:

enter image description here

2- Add the bottom of the screen you will find before launch click on the plus and choose Run External Tool

enter image description here

2- Fill the following information, note that the working directory is your project root directory (not the android directory):

enter image description here

3- Make sure this run before anything else, in the end, your configuration should look something like this: enter image description here

Fareed Alnamrouti
  • 30,771
  • 4
  • 85
  • 76
  • Can you run this command from the command line “npx jetify” in the Android project root directory – Fareed Alnamrouti Nov 15 '19 at 13:27
  • 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. – Sanjay Joshi Nov 15 '19 at 14:00
3

I had similar issues when migrating to androidx. this issue comes due to the Old Butter Knife library dependency.

if you are using butter knife then you should use at least butter knife version 9.0.0-SNAPSHOT or above.

implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT' annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'

Abhijeet Sharma
  • 201
  • 3
  • 5
2

You can find here the official javadoc of the support-annotationslibrary.

Error:(3, 31) error: package com.android.annotations does not exist

As you can see all the classes are in the same package android.support.annotation and not com.android.annotations.

Error:(7, 2) error: cannot find symbol class NonNullByDefault

Also the class NonNullByDefault doesn't exist in that package.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 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. – Adam Nov 02 '16 at 15:09
  • 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. – wesley franks Jun 25 '19 at 13:51
2

if error from butterknife auto generated file then update butterknife dependency version

implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
Amar Singh
  • 435
  • 5
  • 9
2

All you need to do is to replace 'import android.support.annotation.Nullable' in class imports to 'import androidx.annotation.Nullable;'

Thats a common practice..whenever any import giving issue...remove that import and simply press Alt+Enter on the related class..that will give you option to 'import class'..hint Enter and things will get resolved...

1

For me it was an old version of npm.

Run npm install npm@latest -g and then npm install

Muzikant
  • 8,070
  • 5
  • 54
  • 88