importing NotNull or Nullable and Android Studio won't compile

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)

Answers


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 {
    compile 'com.intellij:annotations:+@jar'
    ...
}

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.


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


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.


Nowadays you should use 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){
   ...
}

The easiest way to to add the following to the dependencies section of your build.gradle file.

dependencies {
    compile 'com.intellij:annotations:+@jar'
    ... ...
}

EDIT: updated to use @RichieHH's idea to use the maven repository, which is definitely the way to do it.


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!


Need Your Help

How to limit framerate when using Android's GLSurfaceView.RENDERMODE_CONTINUOUSLY?

android opengl-es

I have a C++ game running through JNI in Android. The frame rate varies from about 20-45fps due to scene complexity. Anything above 30fps is silly for the game; it's just burning battery. I'd like to

shiny 4 small textInput boxes side-by-side

r shiny

I've got a shiny server version 0.4.0 and I want to have 4 small textInput boxes to look like this: