76

When I add @NotNull or @Nullable annotations to a parameter Android Studio automatically helps me with adding /lib/annotations.jar and importing

import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable;

But after this, the project won't compile. If I also remove the annotations but keep the import statements the project still won't compile. But if I remove the import statements for NotNull and Nullable the project compiles fine!

Android Studio gives a generic error:

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Running gradlew compileDebug from cmd gives a slight hint:

:Bugtester:compileDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

So I checked my environment variables and they are set as:

JAVA_HOME=C:\Program Files (x86)\Java\jre7
JDK_HOME=C:\Program Files\Java\jdk1.7.0_21\

Anyone got any idea for this? (I'm new to both java and android programming)

Moberg
  • 5,253
  • 4
  • 38
  • 54
  • 2
    I'm not familiar with gradle, but you should probably add a dependency on `annotations.jar` to `build.gradle`. Something like `dependencies { compile files('full/path/to/annotations.jar') }` – Vladimir Mironov May 22 '13 at 12:04
  • Thanks, this fixed the issue! I thought adding the dependency was what android studio did automatically when I first wrote @NotNull and it wanted me to add something. – Moberg May 22 '13 at 12:44
  • 1
    You can find explanation for this behavior here http://stackoverflow.com/questions/16622410/how-do-i-add-a-library-project-to-the-android-studio-and-use-itsome-asked-dont/16683375#16683375 – Vladimir Mironov May 22 '13 at 12:54
  • @vmironov if you add this as an answer I can accept it. – Moberg Aug 12 '13 at 15:27

9 Answers9

83

I guess, the right way is using original JetBrains library from MavenCentral repository in your build.gradle dependencies (latest available version in this example):

dependencies {
    implementation 'com.intellij:annotations:+@jar'
    ...
}
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Serge Populov
  • 1,725
  • 16
  • 19
49

You can also use android's own @NonNull & @Nullable:

  • Add the following to build.gradle:

    dependencies {
        ...
        // For @Nullable/@NonNull
        compile 'com.android.support:support-annotations:+'
    }
    
  • Go to File / SettingProject SettingsInspections and search for "nullable".

    In Constant conditions & exceptions and @NotNull/@Nullable problems, click Configure annotations and select Android's annotations.

    You may also want to check out Suggest @Nullable annotations… under Constant conditions & exceptions, or possibly tweak other options.

squirrel
  • 5,114
  • 4
  • 31
  • 43
  • 7
    Your answer has solved my problem. Just one thing: The "Avoid using + in version number" warning is shown... i have set my version like this: `compile 'com.android.support:support-annotations:23.1.1'` – Primoz990 Nov 26 '15 at 09:26
  • Was that supposed to be on Mac Android Studio -> Preferences, but then I can't find Project Settings nor Inspections. I am on Android Studio 4. – Dean Hiller Jul 25 '20 at 01:36
13

For using Android support anotation like - @Nullable, @NonNull etc.. In your project must be imported android support annotations library. Just add this line to dependensies in gradle file

dependencies { compile 'com.android.support:support-annotations:+' }

And import package to class.
For using @Nullable annotation:

import android.support.annotation.Nullable;

For @NonNull

import android.support.annotation.NonNull;

More info you can find here Android developers

Michael Shaffer
  • 374
  • 2
  • 16
Roman Smoliar
  • 1,093
  • 13
  • 15
6

At the moment, there is no NonNull/Nullable annotations in the Android API or in the support library. You also cannot use the IntelliJ one since they are not on the compilation classpath when building with Gradle.

However, you can easily create your own. It's very simple:

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({METHOD,PARAMETER,LOCAL_VARIABLE,FIELD})
public @interface NonNull {
}

Once this is down, you can configure IntelliJ (or Android Studio) to recognize this one (and the matching @Nullable) to be the default annotation used for Null-checks.

To do this, go in the IntelliJ preferences, under Inpections, and then find the @NotNull/@Nullable problems entry under Probable Bugs in the inspection tree.

Select the item, and in the bottom right you'll have a button to "Configure Annotations". This will allow you set your own annotations as the one intelliJ will use for this inspection.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
6

Just import androidx.annotation.Nullable for that purpose

  • Thanks! As of today, this is the easiest change – Nagabhushan S N Jul 04 '21 at 06:03
  • Not sure whether it's easier than other options but it seems to be the best today; the current way the functionality is exposed. And need to add to build.gradle dependencies "implementation 'androidx.annotation:annotation:1.6.0'" or something like that. – steve Apr 14 '23 at 17:43
5

In 2015, you would have used annotations from android.support.annotation package. I am just not sure when they added them as in the docs there isn't that typical sentence "Added in API level X" and I don't feel like reading blogs, etc. >]

import android.support.annotation.NonNull;

...

public void fooFighter(@NonNull Object honeyBunny){
   ...
}
Amio.io
  • 20,677
  • 15
  • 82
  • 117
4

I am facing that problem for gradle-5.1.1 - Android Studio 3.4 and the error like that - Compilation failed; see the compiler error output for details. Execution failed for task ':app:compileDebugJavaWithJavac'. etc. In this case I use

implementation 'org.jetbrains:annotations:16.0.2'

and above all error will be clear.

Mahmudur Rahman
  • 638
  • 9
  • 25
0

The best way is to use the maven dependency

  1. set the repository path, by adding

    repositories {
        maven {
            url "https://repository.jboss.org/nexus/content/repositories/thirdparty-releases"
        }
    }
    
  2. add the dependency

    dependencies {
        compile 'org.jetbrains:annotations:7.0.2'
    }
    
  3. PROFIT!

respectTheCode
  • 42,348
  • 18
  • 73
  • 86
curioushikhov
  • 2,461
  • 2
  • 30
  • 44
0

For Maven add dependency:

<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>16.0.1</version>
</dependency> 
DeadCoon
  • 81
  • 4