74

当我向参数添加 @NotNull 或 @Nullable 注释时,Android Studio 会自动帮助我添加 /lib/annotations.jar 并导入

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

但在此之后,项目将无法编译。如果我也删除注释但保留导入语句,项目仍然无法编译。但是,如果我删除 NotNull 和 Nullable 的导入语句,项目编译得很好

Android Studio 给出了一个通用错误:

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.

从 cmd运行gradlew compileDebug会给出一点提示:

: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

所以我检查了我的环境变量,它们设置为:

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

有人对此有任何想法吗?(我对 java 和 android 编程都很陌生)

4

9 回答 9

80

我想,正确的方法是在 build.gradle 依赖项中使用来自 MavenCentral 存储库的原始 JetBrains 库(本示例中的最新可用版本):

dependencies {
    implementation 'com.intellij:annotations:+@jar'
    ...
}
于 2013-12-08T17:08:18.573 回答
50
于 2014-09-11T13:04:48.320 回答
13

对于使用 Android 支持注释,如 - @Nullable、@NonNull 等。在您的项目中必须导入 android 支持注释库。只需将此行添加到gradle 文件 中的依赖项

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

并将包导入类。
对于使用 @Nullable 注释:

import android.support.annotation.Nullable;

对于@NonNull

import android.support.annotation.NonNull;

您可以在此处找到更多信息Android 开发人员

于 2016-03-10T20:06:21.783 回答
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.

于 2013-05-23T06:28:54.450 回答
5

在 2015 年,您将使用android.support.annotation包中的注释。我只是不确定他们何时添加它们,因为在文档中没有典型的句子“在 API 级别 X 中添加”,我不想阅读博客等。>]

import android.support.annotation.NonNull;

...

public void fooFighter(@NonNull Object honeyBunny){
   ...
}
于 2015-05-16T07:54:49.077 回答
4

Just import androidx.annotation.Nullable for that purpose

于 2021-01-21T16:31:16.433 回答
4

我正面临gradle-5.1.1的问题- Android Studio 3.4和类似的错误 -编译失败;有关详细信息,请参阅编译器错误输出。任务 ':app:compileDebugJavaWithJavac' 执行失败。等在这​​种情况下,我使用

implementation 'org.jetbrains:annotations:16.0.2'

最重要的是,错误将很清楚。

于 2019-05-06T10:32:08.577 回答
0

最好的方法是使用maven依赖

  1. 设置存储库路径,通过添加

    repositories {
        maven {
            url "https://repository.jboss.org/nexus/content/repositories/thirdparty-releases"
        }
    }
    
  2. 添加依赖

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

于 2013-12-08T11:34:14.403 回答
0

对于 Maven 添加依赖项:

<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>16.0.1</version>
</dependency> 
于 2019-06-13T11:17:25.880 回答