导入NotNull或Nullable并且Android Studio将无法编译

Mob*_*erg 55 java gradle android-studio

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

import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable;
Run Code Online (Sandbox Code Playgroud)

但在此之后,该项目将无法编译.如果我也删除注释但保留import语句,项目仍然无法编译.但是如果我删除NotNull和Nullable的import语句,项目编译就好了!

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.
Run Code Online (Sandbox Code Playgroud)

gradlew compileDebug从cmd 运行会略有提示:

: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
Run Code Online (Sandbox Code Playgroud)

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

JAVA_HOME=C:\Program Files (x86)\Java\jre7
JDK_HOME=C:\Program Files\Java\jdk1.7.0_21\
Run Code Online (Sandbox Code Playgroud)

有人对此有任何想法吗?(我是java和android编程的新手)

Ser*_*lov 60

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

dependencies {
    compile 'com.intellij:annotations:+@jar'
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 在较新的版本中,使用“实现”而不是“编译” (3认同)
  • 我使用实现 'org.jetbrains:annotations:16.0.2' - 对于 gradle-5.1.1,它对我有用。 (3认同)

squ*_*rel 43

你也可以使用android自己的@NonNull&@Nullable:

  • 将以下内容添加到build.gradle:

    dependencies {
        ...
        // For @Nullable/@NonNull
        compile 'com.android.support:support-annotations:+'
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 转到文件/设置项目设置检查并搜索"可空".

    Constant conditions&exceptions@NotNull/@Nullable问题中,单击Configure annotations并选择Android的注释.

    您可能还想在常量条件和例外情况下查看Suggest @Nullable注释...,或者可能调整其他选项.

  • 你的答案解决了我的问题.只有一件事:显示"避免在版本号中使用+"警告..​​.我已经设置了我的版本:`compile'c​​om.android.support:support-annotations:23.1.1'` (5认同)

Rom*_*iar 11

对于使用Android支持的anotation,如@Nullable,@ NonNull等.在你的项目中必须导入android支持注释库.只需将此行添加到 gradle文件中的dependensies即可

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

导入包到类.
使用@Nullable注释:

import android.support.annotation.Nullable;
Run Code Online (Sandbox Code Playgroud)

对于@NonNull

import android.support.annotation.NonNull;
Run Code Online (Sandbox Code Playgroud)

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


小智 8

只需导入androidx.annotation.Nullable即可达到此目的


Xav*_*het 5

目前,Android API或支持库中没有NonNull/Nullable注释.您也无法使用IntelliJ,因为在使用Gradle构建时它们不在编译类路径中.

但是,您可以轻松创建自己的.这很简单:

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({METHOD,PARAMETER,LOCAL_VARIABLE,FIELD})
public @interface NonNull {
}
Run Code Online (Sandbox Code Playgroud)

一旦关闭,您可以配置IntelliJ(或Android Studio)以识别这个(和匹配@Nullable)是用于Null检查的默认注释.

为此,请进入IntelliJ首选项,Inpections然后在检查树下找到该@NotNull/@Nullable problems条目Probable Bugs.

选择项目,在右下角,您将有一个"配置注释"按钮.这将允许您设置自己的注释,因为智能将用于此检查.


Ami*_*.io 5

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

import android.support.annotation.NonNull;

...

public void fooFighter(@NonNull Object honeyBunny){
   ...
}
Run Code Online (Sandbox Code Playgroud)

  • 2015年的时光已经过去了,2019年的时光已经过去了,请使用** androidx.annotation:annotation:1.xx **:P (2认同)